Skip to content

[Backport 7.77.x] [CWS] Fix panic on closed channel during APIServer shutdown#47073

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 1 commit into7.77.xfrom
backport-47047-to-7.77.x
Mar 2, 2026
Merged

[Backport 7.77.x] [CWS] Fix panic on closed channel during APIServer shutdown#47073
gh-worker-dd-mergequeue-cf854d[bot] merged 1 commit into7.77.xfrom
backport-47047-to-7.77.x

Conversation

@dd-octo-sts
Copy link
Copy Markdown
Contributor

@dd-octo-sts dd-octo-sts Bot commented Feb 27, 2026

Backport 9ee6410 from #47047.


What does this PR do?

Wait for the APIServer.start() goroutine to finish before stopping pipeline providers, preventing a race where events are sent on a closed logChan.

Motivation

During system-probe shutdown, CWSConsumer.Stop() calls c.cancelFnc() to cancel the Go context, and then c.apiServer.Stop(). c.apiServer.Stop() then calls a.stopper.Stop() to close the logChan channel. However, the APIServer.start() goroutine was not tracked by any synchronization primitive, so after the Go context was cancelled the goroutine could still be in the middle of a dequeue cycle while the logChan channel was already closed.

This resulted in the following panic:

panic: send on closed channel
goroutine 781 [running]:
    github.com/DataDog/datadog-agent/pkg/security/reporter.(*RuntimeReporter).ReportRaw(0x4000e84e20, {0x4003c26000, 0x3d43, 0x4000}, {0x0, 0x0}, {0x40009f7b48?, 0x1a15a98?, 0x0?}, {0x400209a880, ...})
    github.com/DataDog/datadog-agent/pkg/security/reporter/reporter.go:39 +0x1d0
    github.com/DataDog/datadog-agent/pkg/security/module.(*DirectEventMsgSender).Send(0x400278a2a0, 0x400295d7c0, 0x3a?)
    github.com/DataDog/datadog-agent/pkg/security/module/msg_sender.go:100 +0x138
    github.com/DataDog/datadog-agent/pkg/security/module.(*APIServer).start.func1(0x40016c3e00, 0x1)
    github.com/DataDog/datadog-agent/pkg/security/module/server.go:386 +0x400
    github.com/DataDog/datadog-agent/pkg/security/module.(*APIServer).dequeue.func1(...)
    github.com/DataDog/datadog-agent/pkg/security/module/server.go:256
    github.com/DataDog/datadog-agent/pkg/security/module.slicesDeleteUntilFalse(...)
    github.com/DataDog/datadog-agent/pkg/security/module/server.go:280
    github.com/DataDog/datadog-agent/pkg/security/module.(*APIServer).dequeue(0x40019afa20, {0x4003e2bf20?, 0x48e7fd6f8e4?, 0x401af60?}, 0x40009f7f30)
    github.com/DataDog/datadog-agent/pkg/security/module/server.go:248 +0x1d8
    github.com/DataDog/datadog-agent/pkg/security/module.(*APIServer).start(0x40019afa20, {0x2ba4608, 0x400225d0e0})
    github.com/DataDog/datadog-agent/pkg/security/module/server.go:337 +0x80
created by github.com/DataDog/datadog-agent/pkg/security/module.(*APIServer).Start in goroutine 1
    github.com/DataDog/datadog-agent/pkg/security/module/server.go:412 +0x1d0

Describe how you validated your changes

Additional Notes

…### What does this PR do?

Wait for the `APIServer.start()` goroutine to finish before stopping pipeline providers, preventing a race where events are sent on a closed `logChan`.

### Motivation

During system-probe shutdown, `CWSConsumer.Stop()` calls `c.cancelFnc()` to cancel the Go context, and then `c.apiServer.Stop()`. `c.apiServer.Stop()` then calls  `a.stopper.Stop()` to close the `logChan` channel. However, the `APIServer.start()` goroutine was not tracked by any synchronization primitive, so after the Go context was cancelled the goroutine could still be in the middle of a dequeue cycle while the `logChan` channel was already closed.

This resulted in the following panic:

```
panic: send on closed channel
goroutine 781 [running]:
    github.com/DataDog/datadog-agent/pkg/security/reporter.(*RuntimeReporter).ReportRaw(0x4000e84e20, {0x4003c26000, 0x3d43, 0x4000}, {0x0, 0x0}, {0x40009f7b48?, 0x1a15a98?, 0x0?}, {0x400209a880, ...})
    github.com/DataDog/datadog-agent/pkg/security/reporter/reporter.go:39 +0x1d0
    github.com/DataDog/datadog-agent/pkg/security/module.(*DirectEventMsgSender).Send(0x400278a2a0, 0x400295d7c0, 0x3a?)
    github.com/DataDog/datadog-agent/pkg/security/module/msg_sender.go:100 +0x138
    github.com/DataDog/datadog-agent/pkg/security/module.(*APIServer).start.func1(0x40016c3e00, 0x1)
    github.com/DataDog/datadog-agent/pkg/security/module/server.go:386 +0x400
    github.com/DataDog/datadog-agent/pkg/security/module.(*APIServer).dequeue.func1(...)
    github.com/DataDog/datadog-agent/pkg/security/module/server.go:256
    github.com/DataDog/datadog-agent/pkg/security/module.slicesDeleteUntilFalse(...)
    github.com/DataDog/datadog-agent/pkg/security/module/server.go:280
    github.com/DataDog/datadog-agent/pkg/security/module.(*APIServer).dequeue(0x40019afa20, {0x4003e2bf20?, 0x48e7fd6f8e4?, 0x401af60?}, 0x40009f7f30)
    github.com/DataDog/datadog-agent/pkg/security/module/server.go:248 +0x1d8
    github.com/DataDog/datadog-agent/pkg/security/module.(*APIServer).start(0x40019afa20, {0x2ba4608, 0x400225d0e0})
    github.com/DataDog/datadog-agent/pkg/security/module/server.go:337 +0x80
created by github.com/DataDog/datadog-agent/pkg/security/module.(*APIServer).Start in goroutine 1
    github.com/DataDog/datadog-agent/pkg/security/module/server.go:412 +0x1d0
```

### Describe how you validated your changes

### Additional Notes

Co-authored-by: yoann.ghigoff <yoann.ghigoff@datadoghq.com>
(cherry picked from commit 9ee6410)

___

Co-authored-by: Yoann Ghigoff <yoann.ghigoff@datadoghq.com>
@dd-octo-sts dd-octo-sts Bot requested a review from a team as a code owner February 27, 2026 18:44
@dd-octo-sts dd-octo-sts Bot added changelog/no-changelog No changelog entry needed component/system-probe internal Identify a non-fork PR qa/done QA done before merge and regressions are covered by tests short review PR is simple enough to be reviewed quickly team/agent-security labels Feb 27, 2026
@agent-platform-auto-pr
Copy link
Copy Markdown
Contributor

agent-platform-auto-pr Bot commented Feb 27, 2026

Static quality checks

✅ Please find below the results from static quality gates
Comparison made with ancestor 6a99406
📊 Static Quality Gates Dashboard
🔗 SQG Job

31 successful checks with minimal change (< 2 KiB)
Quality gate Current Size
agent_deb_amd64 756.410 MiB
agent_deb_amd64_fips 715.088 MiB
agent_heroku_amd64 323.756 MiB
agent_msi 622.544 MiB
agent_rpm_amd64 756.394 MiB
agent_rpm_amd64_fips 715.072 MiB
agent_rpm_arm64 734.517 MiB
agent_rpm_arm64_fips 696.212 MiB
agent_suse_amd64 756.394 MiB
agent_suse_amd64_fips 715.072 MiB
agent_suse_arm64 734.517 MiB
agent_suse_arm64_fips 696.212 MiB
docker_agent_amd64 817.431 MiB
docker_agent_arm64 820.290 MiB
docker_agent_jmx_amd64 1008.343 MiB
docker_agent_jmx_arm64 999.983 MiB
docker_cluster_agent_amd64 203.000 MiB
docker_cluster_agent_arm64 217.455 MiB
docker_cws_instrumentation_amd64 7.135 MiB
docker_cws_instrumentation_arm64 6.689 MiB
docker_dogstatsd_amd64 38.500 MiB
docker_dogstatsd_arm64 36.812 MiB
dogstatsd_deb_amd64 29.724 MiB
dogstatsd_deb_arm64 27.881 MiB
dogstatsd_rpm_amd64 29.724 MiB
dogstatsd_suse_amd64 29.724 MiB
iot_agent_deb_amd64 42.617 MiB
iot_agent_deb_arm64 39.723 MiB
iot_agent_deb_armhf 40.447 MiB
iot_agent_rpm_amd64 42.618 MiB
iot_agent_suse_amd64 42.618 MiB
On-wire sizes (compressed)
Quality gate Change Size (prev → curr → max)
agent_deb_amd64 -30.62 KiB (0.02% reduction) 185.674 → 185.644 → 186.090
agent_deb_amd64_fips +6.27 KiB (0.00% increase) 176.307 → 176.314 → 180.330
agent_heroku_amd64 neutral 87.104 MiB → 88.440
agent_msi -4.0 KiB (0.00% reduction) 149.379 → 149.375 → 154.470
agent_rpm_amd64 -2.13 KiB (0.00% reduction) 187.575 → 187.572 → 189.170
agent_rpm_amd64_fips +48.41 KiB (0.03% increase) 178.416 → 178.463 → 181.060
agent_rpm_arm64 +23.45 KiB (0.01% increase) 169.836 → 169.859 → 170.020
agent_rpm_arm64_fips neutral 162.470 MiB → 164.130
agent_suse_amd64 -2.13 KiB (0.00% reduction) 187.575 → 187.572 → 189.170
agent_suse_amd64_fips +48.41 KiB (0.03% increase) 178.416 → 178.463 → 181.060
agent_suse_arm64 +23.45 KiB (0.01% increase) 169.836 → 169.859 → 170.020
agent_suse_arm64_fips neutral 162.470 MiB → 164.130
docker_agent_amd64 neutral 277.932 MiB → 279.410
docker_agent_arm64 neutral 265.209 MiB → 267.960
docker_agent_jmx_amd64 -2.09 KiB (0.00% reduction) 346.589 → 346.587 → 348.040
docker_agent_jmx_arm64 +2.09 KiB (0.00% increase) 329.850 → 329.852 → 332.560
docker_cluster_agent_amd64 neutral 71.131 MiB → 71.920
docker_cluster_agent_arm64 neutral 66.773 MiB → 67.220
docker_cws_instrumentation_amd64 neutral 2.995 MiB → 3.330
docker_cws_instrumentation_arm64 neutral 2.726 MiB → 3.090
docker_dogstatsd_amd64 neutral 14.901 MiB → 15.820
docker_dogstatsd_arm64 neutral 14.237 MiB → 14.830
dogstatsd_deb_amd64 -2.23 KiB (0.03% reduction) 7.856 → 7.853 → 8.790
dogstatsd_deb_arm64 neutral 6.743 MiB → 7.710
dogstatsd_rpm_amd64 neutral 7.867 MiB → 8.800
dogstatsd_suse_amd64 neutral 7.867 MiB → 8.800
iot_agent_deb_amd64 neutral 11.237 MiB → 12.040
iot_agent_deb_arm64 +2.9 KiB (0.03% increase) 9.601 → 9.604 → 10.450
iot_agent_deb_armhf neutral 9.802 MiB → 10.620
iot_agent_rpm_amd64 neutral 11.256 MiB → 12.060
iot_agent_suse_amd64 neutral 11.256 MiB → 12.060

@cit-pr-commenter-54b7da
Copy link
Copy Markdown

cit-pr-commenter-54b7da Bot commented Feb 27, 2026

Regression Detector

Regression Detector Results

Metrics dashboard
Target profiles
Run ID: 119f05db-ddc2-4853-be5d-8efa69099179

Baseline: 69849fb
Comparison: 62f24b7
Diff

Optimization Goals: ✅ No significant changes detected

Experiments ignored for regressions

Regressions in experiments with settings containing erratic: true are ignored.

perf experiment goal Δ mean % Δ mean % CI trials links
docker_containers_cpu % cpu utilization +0.32 [-2.81, +3.44] 1 Logs

Fine details of change detection per experiment

perf experiment goal Δ mean % Δ mean % CI trials links
ddot_metrics_sum_cumulative memory utilization +0.73 [+0.58, +0.89] 1 Logs
docker_containers_cpu % cpu utilization +0.32 [-2.81, +3.44] 1 Logs
ddot_metrics_sum_delta memory utilization +0.21 [+0.01, +0.41] 1 Logs
otlp_ingest_metrics memory utilization +0.06 [-0.10, +0.22] 1 Logs
quality_gate_idle_all_features memory utilization +0.04 [+0.00, +0.08] 1 Logs bounds checks dashboard
file_to_blackhole_100ms_latency egress throughput +0.02 [-0.02, +0.07] 1 Logs
file_to_blackhole_0ms_latency egress throughput +0.02 [-0.47, +0.50] 1 Logs
uds_dogstatsd_to_api ingress throughput +0.01 [-0.11, +0.14] 1 Logs
quality_gate_idle memory utilization +0.01 [-0.04, +0.05] 1 Logs bounds checks dashboard
uds_dogstatsd_to_api_v3 ingress throughput -0.00 [-0.13, +0.13] 1 Logs
tcp_dd_logs_filter_exclude ingress throughput -0.00 [-0.09, +0.08] 1 Logs
file_to_blackhole_500ms_latency egress throughput -0.03 [-0.41, +0.36] 1 Logs
file_to_blackhole_1000ms_latency egress throughput -0.05 [-0.48, +0.38] 1 Logs
ddot_metrics_sum_cumulativetodelta_exporter memory utilization -0.19 [-0.42, +0.04] 1 Logs
ddot_logs memory utilization -0.20 [-0.26, -0.14] 1 Logs
tcp_syslog_to_blackhole ingress throughput -0.28 [-0.35, -0.22] 1 Logs
docker_containers_memory memory utilization -0.29 [-0.36, -0.22] 1 Logs
file_tree memory utilization -0.39 [-0.43, -0.34] 1 Logs
uds_dogstatsd_20mb_12k_contexts_20_senders memory utilization -0.46 [-0.51, -0.40] 1 Logs
otlp_ingest_logs memory utilization -0.46 [-0.56, -0.37] 1 Logs
ddot_metrics memory utilization -0.52 [-0.74, -0.31] 1 Logs
quality_gate_logs % cpu utilization -1.04 [-2.55, +0.48] 1 Logs bounds checks dashboard
quality_gate_metrics_logs memory utilization -1.21 [-1.41, -1.00] 1 Logs bounds checks dashboard

Bounds Checks: ✅ Passed

perf experiment bounds_check_name replicates_passed links
docker_containers_cpu simple_check_run 10/10
docker_containers_memory memory_usage 10/10
docker_containers_memory simple_check_run 10/10
file_to_blackhole_0ms_latency lost_bytes 10/10
file_to_blackhole_0ms_latency memory_usage 10/10
file_to_blackhole_1000ms_latency lost_bytes 10/10
file_to_blackhole_1000ms_latency memory_usage 10/10
file_to_blackhole_100ms_latency lost_bytes 10/10
file_to_blackhole_100ms_latency memory_usage 10/10
file_to_blackhole_500ms_latency lost_bytes 10/10
file_to_blackhole_500ms_latency memory_usage 10/10
quality_gate_idle intake_connections 10/10 bounds checks dashboard
quality_gate_idle memory_usage 10/10 bounds checks dashboard
quality_gate_idle_all_features intake_connections 10/10 bounds checks dashboard
quality_gate_idle_all_features memory_usage 10/10 bounds checks dashboard
quality_gate_logs intake_connections 10/10 bounds checks dashboard
quality_gate_logs lost_bytes 10/10 bounds checks dashboard
quality_gate_logs memory_usage 10/10 bounds checks dashboard
quality_gate_metrics_logs cpu_usage 10/10 bounds checks dashboard
quality_gate_metrics_logs intake_connections 10/10 bounds checks dashboard
quality_gate_metrics_logs lost_bytes 10/10 bounds checks dashboard
quality_gate_metrics_logs memory_usage 10/10 bounds checks dashboard

Explanation

Confidence level: 90.00%
Effect size tolerance: |Δ mean %| ≥ 5.00%

Performance changes are noted in the perf column of each table:

  • ✅ = significantly better comparison variant performance
  • ❌ = significantly worse comparison variant performance
  • ➖ = no significant change in performance

A regression test is an A/B test of target performance in a repeatable rig, where "performance" is measured as "comparison variant minus baseline variant" for an optimization goal (e.g., ingress throughput). Due to intrinsic variability in measuring that goal, we can only estimate its mean value for each experiment; we report uncertainty in that value as a 90.00% confidence interval denoted "Δ mean % CI".

For each experiment, we decide whether a change in performance is a "regression" -- a change worth investigating further -- if all of the following criteria are true:

  1. Its estimated |Δ mean %| ≥ 5.00%, indicating the change is big enough to merit a closer look.

  2. Its 90.00% confidence interval "Δ mean % CI" does not contain zero, indicating that if our statistical model is accurate, there is at least a 90.00% chance there is a difference in performance between baseline and comparison variants.

  3. Its configuration does not mark it "erratic".

CI Pass/Fail Decision

Passed. All Quality Gates passed.

  • quality_gate_idle_all_features, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_idle_all_features, bounds check intake_connections: 10/10 replicas passed. Gate passed.
  • quality_gate_metrics_logs, bounds check cpu_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_metrics_logs, bounds check intake_connections: 10/10 replicas passed. Gate passed.
  • quality_gate_metrics_logs, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_metrics_logs, bounds check lost_bytes: 10/10 replicas passed. Gate passed.
  • quality_gate_idle, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_idle, bounds check intake_connections: 10/10 replicas passed. Gate passed.
  • quality_gate_logs, bounds check intake_connections: 10/10 replicas passed. Gate passed.
  • quality_gate_logs, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_logs, bounds check lost_bytes: 10/10 replicas passed. Gate passed.

@gh-worker-dd-mergequeue-cf854d gh-worker-dd-mergequeue-cf854d Bot merged commit 99215d2 into 7.77.x Mar 2, 2026
516 checks passed
@gh-worker-dd-mergequeue-cf854d gh-worker-dd-mergequeue-cf854d Bot deleted the backport-47047-to-7.77.x branch March 2, 2026 08:54
@github-actions github-actions Bot added this to the 7.77.0 milestone Mar 2, 2026
AliDatadog added a commit that referenced this pull request Mar 9, 2026
Add project-specific review guidelines for AI code reviewers (Codex,
Claude Code), covering: E2E coverage with fakeintake, branch-conditional
CI blind spots, multi-platform divergence, concurrency lifecycle,
graceful degradation, documentation freshness, and PR template adherence.

Also add a maintenance section encouraging organic growth of guidelines.

Derived from analysis of backport bug fixes: #47251, #47185, #47073,
#47509, #47028, #46978, #46979, #47003, #46955, #47256.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport bot category/bugfix changelog/no-changelog No changelog entry needed component/system-probe internal Identify a non-fork PR qa/done QA done before merge and regressions are covered by tests short review PR is simple enough to be reviewed quickly team/agent-security

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants