Skip to content

fix(profiling): abort profile upload on socket timeout#9217

Merged
szegedi merged 1 commit into
masterfrom
fix/profiling-upload-timeout-hang
Jul 6, 2026
Merged

fix(profiling): abort profile upload on socket timeout#9217
szegedi merged 1 commit into
masterfrom
fix/profiling-upload-timeout-hang

Conversation

@szegedi

@szegedi szegedi commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Makes the profiling agent exporter enforce its upload timeout by destroying the request on the socket 'timeout' event, and guards the request callback so it runs at most once.

Motivation

The exporter sets options.timeout on the upload request (this.#backoffTime * 2 ** attempt) but never registered a 'timeout' handler. Per the Node http docs the 'timeout' event is informational only — it does not abort the socket. So a stalled upload (a connection that hangs on a loaded/shared host's network stack, even to a local agent) never emitted 'error': the retry/backoff in AgentExporter.export never ran and the returned promise never settled.

In practice this surfaced as a flaky/hung profile upload on shutdown: a worker's final beforeExit flush would block indefinitely on the stalled request, keeping the process alive until it was force-killed and losing that profile. The trace exporter (exporters/common/request.js) already aborts on timeout; only the profiling exporter was missing the equivalent.

This essentially brings the behavior in line with how the trace exporter behaves.

NB: Node.js profiler is the only profiler that bothers with retries; other language profilers nonchalantly drop the profile if the first communication attempt with the agent fails. We could rethink if we want to go in that direction, but for now fixing the bug in existing retrying behavior feels like a good incremental fix.

Changes

  • req.on('timeout', () => req.destroy(...)) in sendRequest — the destroy surfaces as an 'error', which drives the existing retry/backoff (and eventual rejection), instead of hanging forever.
  • Guard the request callback so it runs at most once. The 'timeout' can fire after a response was already received (e.g. on a lingering keep-alive socket once it goes idle); without the guard that invokes the callback a second time and corrupts the retry accounting.
  • Regression test: asserts the export rejects after retrying rather than hanging when the agent never responds (guarded by the mocha timeout — it hangs without the fix).
  • Adjusted the existing "should backoff up to the uploadTimeout" test: its uploadTimeout was unrealistically small (50ms first attempt), which — now that the timeout is actually enforced — raced the in-process test agent's response. Bumped it so the HTTP-error retries it means to exercise aren't pre-empted by a socket timeout.

Testing

packages/dd-trace/test/profiling/exporters/agent.spec.js: 8 passing, ESLint clean; the new timeout test and the backoff test are stable across repeated runs.

Jira: PROF-15326

The agent profile exporter sets `options.timeout` on the upload request but
never handled the request's 'timeout' event. Per the Node http docs the
timeout event is informational only — it does not abort the socket — so a
stalled upload (e.g. a connection that hangs on a loaded/shared host's
network stack) never emitted 'error', the retry/backoff in
AgentExporter.export never ran, and the export promise never settled. The
worker process then stays alive on its shutdown flush until the container
is force-killed, and the profile is lost.

Handle 'timeout' by destroying the request, which surfaces as an 'error'
and drives the existing retry/backoff — matching the trace exporter in
exporters/common/request.js, which already aborts on timeout.

Also guard the request callback so it runs at most once: the timeout can
fire after a response was already received (e.g. on a lingering keep-alive
socket once it goes idle), which would otherwise invoke the callback twice
and corrupt the retry accounting.

Adds a regression test asserting the export rejects (after retrying)
instead of hanging when the agent never responds. Also bumps the
uploadTimeout in the existing backoff test so its (now enforced) per-attempt
socket timeout doesn't race the in-process test agent's response.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@dd-octo-sts

dd-octo-sts Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Overall package size

Self size: 6.53 MB
Deduped: 7.19 MB
No deduping: 7.19 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 3.3.0 | 117.14 kB | 432.47 kB | | opentracing | 0.14.7 | 194.81 kB | 194.81 kB | | dc-polyfill | 0.1.11 | 25.74 kB | 25.74 kB |

🤖 This report was automatically generated by heaviest-objects-in-the-universe

@datadog-datadog-prod-us1

datadog-datadog-prod-us1 Bot commented Jul 6, 2026

Copy link
Copy Markdown

Pipelines  Tests

Fix all issues with BitsAI

⚠️ Warnings

🚦 1 Pipeline job failed

DataDog/apm-reliability/dd-trace-js | benchmark-serverless   View in Datadog   GitLab

ℹ️ Info

No other issues found (see more)

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
Patch Coverage: 100.00%
Overall Coverage: 87.86% (+0.00%)

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 47037f5 | Docs | Datadog PR Page | Give us feedback!

@codecov

codecov Bot commented Jul 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.65%. Comparing base (02cb1a1) to head (47037f5).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9217      +/-   ##
==========================================
- Coverage   93.65%   93.65%   -0.01%     
==========================================
  Files         899      899              
  Lines       52473    52480       +7     
  Branches    12352    12353       +1     
==========================================
+ Hits        49145    49151       +6     
- Misses       3328     3329       +1     
Flag Coverage Δ
aiguard 35.00% <ø> (-0.06%) ⬇️
aiguard-integration 41.98% <ø> (ø)
apm-bucket-0 34.84% <ø> (-0.08%) ⬇️
apm-bucket-1 40.51% <ø> (-0.07%) ⬇️
apm-bucket-2 37.50% <ø> (-0.07%) ⬇️
apm-capabilities-tracing 48.92% <0.00%> (-0.01%) ⬇️
apm-integrations-aerospike 33.13% <ø> (-0.08%) ⬇️
apm-integrations-confluentinc-kafka-javascript 40.03% <ø> (-0.17%) ⬇️
apm-integrations-couchbase 33.41% <ø> (-0.08%) ⬇️
apm-integrations-http 42.05% <ø> (-0.07%) ⬇️
apm-integrations-kafkajs 40.26% <ø> (-0.12%) ⬇️
apm-integrations-next 29.63% <ø> (-0.08%) ⬇️
apm-integrations-prisma 35.18% <ø> (-0.07%) ⬇️
apm-integrations-tedious 34.04% <ø> (-0.08%) ⬇️
appsec 57.90% <ø> (-0.08%) ⬇️
appsec-express_fastify_graphql 53.84% <ø> (-0.09%) ⬇️
appsec-integration 35.62% <ø> (ø)
appsec-kafka_ldapjs_lodash 43.75% <ø> (-0.07%) ⬇️
appsec-mongodb-core_mongoose_mysql 48.90% <ø> (-0.06%) ⬇️
appsec-next 28.04% <ø> (-0.07%) ⬇️
appsec-node-serialize_passport_postgres 47.96% <ø> (-0.23%) ⬇️
appsec-sourcing_stripe_template 45.64% <ø> (-0.06%) ⬇️
debugger 44.63% <ø> (-0.05%) ⬇️
instrumentations-bucket-0 28.16% <ø> (-0.08%) ⬇️
instrumentations-bucket-1 37.57% <ø> (-0.07%) ⬇️
instrumentations-bucket-10 40.51% <ø> (-0.07%) ⬇️
instrumentations-bucket-11 27.96% <ø> (-0.08%) ⬇️
instrumentations-bucket-12 28.79% <ø> (-0.08%) ⬇️
instrumentations-bucket-13 27.79% <ø> (-0.08%) ⬇️
instrumentations-bucket-2 30.36% <ø> (-0.08%) ⬇️
instrumentations-bucket-3 36.06% <ø> (-0.08%) ⬇️
instrumentations-bucket-4 28.57% <ø> (-0.08%) ⬇️
instrumentations-bucket-5 36.42% <ø> (-0.07%) ⬇️
instrumentations-bucket-6 38.42% <ø> (-0.08%) ⬇️
instrumentations-bucket-7 36.07% <ø> (-0.16%) ⬇️
instrumentations-bucket-8 37.11% <ø> (-0.07%) ⬇️
instrumentations-bucket-9 39.61% <ø> (-0.07%) ⬇️
instrumentations-instrumentation-couchbase 46.48% <ø> (ø)
instrumentations-integration-esbuild 24.47% <ø> (ø)
llmobs-ai_anthropic_bedrock 39.68% <ø> (-0.07%) ⬇️
llmobs-google-genai_langchain_vertex-ai 36.98% <ø> (-0.06%) ⬇️
llmobs-openai 39.74% <ø> (-0.07%) ⬇️
llmobs-sdk 43.59% <ø> (-0.08%) ⬇️
master-coverage 93.65% <100.00%> (?)
openfeature 37.94% <ø> (ø)
openfeature-unit 50.47% <ø> (ø)
platform-core_esbuild_instrumentations-misc 22.92% <ø> (ø)
platform-integration 47.58% <ø> (ø)
platform-shimmer_unit-guardrails_webpack 18.44% <ø> (ø)
plugins-bucket-0 36.52% <ø> (-0.07%) ⬇️
plugins-bucket-1 39.78% <ø> (ø)
plugins-bucket-11 38.63% <ø> (-0.08%) ⬇️
plugins-bucket-17 39.22% <ø> (-0.07%) ⬇️
plugins-bucket-18 42.17% <ø> (-0.07%) ⬇️
plugins-bucket-19 39.97% <ø> (-0.07%) ⬇️
plugins-bucket-20 43.43% <ø> (-0.07%) ⬇️
plugins-bucket-4 37.88% <ø> (-0.07%) ⬇️
plugins-bullmq_cassandra_cookie 39.93% <ø> (-0.07%) ⬇️
plugins-cookie-parser_crypto_dd-trace-api 33.22% <ø> (-0.08%) ⬇️
plugins-fetch_fs_generic-pool 36.20% <ø> (-0.08%) ⬇️
plugins-google-cloud-pubsub_grpc_handlebars 43.08% <ø> (-0.07%) ⬇️
plugins-hapi_hono_ioredis 37.94% <ø> (-0.07%) ⬇️
plugins-jest_knex_langgraph 32.61% <ø> (-0.08%) ⬇️
plugins-ldapjs_light-my-request_limitd-client 27.86% <ø> (-0.08%) ⬇️
plugins-lodash_mariadb_memcached 35.17% <ø> (-0.08%) ⬇️
plugins-mongodb_mongodb-core_mongoose 36.46% <ø> (-0.07%) ⬇️
plugins-multer_mysql_mysql2 35.15% <ø> (-0.08%) ⬇️
plugins-nats_node-serialize_opensearch 37.31% <ø> (-0.08%) ⬇️
plugins-passport-http_pino_postgres 35.53% <ø> (-0.08%) ⬇️
plugins-process_pug_redis 34.26% <ø> (-0.08%) ⬇️
plugins-undici_url_valkey 36.03% <ø> (-0.07%) ⬇️
plugins-vm_winston_ws 37.72% <ø> (-0.08%) ⬇️
profiling 43.83% <100.00%> (-0.05%) ⬇️
serverless-aws-sdk-aws-sdk 33.32% <ø> (-0.07%) ⬇️
serverless-aws-sdk-bedrockruntime 32.19% <ø> (-0.07%) ⬇️
serverless-aws-sdk-client 37.16% <ø> (ø)
serverless-aws-sdk-dynamodb 34.19% <ø> (-0.02%) ⬇️
serverless-aws-sdk-eventbridge 27.36% <ø> (-0.07%) ⬇️
serverless-aws-sdk-kinesis 37.55% <ø> (-0.07%) ⬇️
serverless-aws-sdk-lambda 34.64% <ø> (-0.07%) ⬇️
serverless-aws-sdk-s3 32.63% <ø> (-0.07%) ⬇️
serverless-aws-sdk-serverless-peer-service 39.65% <ø> (-0.08%) ⬇️
serverless-aws-sdk-sns 38.40% <ø> (-0.21%) ⬇️
serverless-aws-sdk-sqs 38.14% <ø> (-0.07%) ⬇️
serverless-aws-sdk-stepfunctions 33.22% <ø> (-0.07%) ⬇️
serverless-aws-sdk-util 47.95% <ø> (ø)
serverless-bucket-0 39.63% <ø> (ø)
serverless-lambda 34.34% <ø> (-0.09%) ⬇️
test-optimization-cucumber 52.55% <ø> (+0.11%) ⬆️
test-optimization-cypress 49.63% <ø> (+0.06%) ⬆️
test-optimization-jest 55.70% <ø> (+0.04%) ⬆️
test-optimization-mocha 53.62% <ø> (+0.08%) ⬆️
test-optimization-playwright-playwright-atr 43.53% <ø> (+0.08%) ⬆️
test-optimization-playwright-playwright-efd 43.82% <ø> (+0.08%) ⬆️
test-optimization-playwright-playwright-final-status 43.94% <ø> (+0.08%) ⬆️
test-optimization-playwright-playwright-impacted-tests 43.36% <ø> (ø)
test-optimization-playwright-playwright-reporting 43.47% <ø> (+0.08%) ⬆️
test-optimization-playwright-playwright-test-management 44.94% <ø> (+0.08%) ⬆️
test-optimization-playwright-playwright-test-span 44.72% <ø> (+0.07%) ⬆️
test-optimization-selenium 45.49% <ø> (+0.06%) ⬆️
test-optimization-testopt 46.81% <ø> (+0.08%) ⬆️
test-optimization-vitest 52.80% <ø> (+0.09%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@szegedi
szegedi marked this pull request as ready for review July 6, 2026 11:26
@szegedi
szegedi requested a review from a team as a code owner July 6, 2026 11:26
@szegedi
szegedi enabled auto-merge (squash) July 6, 2026 12:24
@pr-commenter

pr-commenter Bot commented Jul 6, 2026

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-07-06 12:32:59

Comparing candidate commit 47037f5 in PR branch fix/profiling-upload-timeout-hang with baseline commit 02cb1a1 in branch master.

📊 Benchmarking dashboard

Found 0 performance improvements and 0 performance regressions! Performance is the same for 2252 metrics, 34 unstable metrics.

Explanation

This is an A/B test comparing a candidate commit's performance against that of a baseline commit. Performance changes are noted in the tables below as:

  • 🟩 = significantly better candidate vs. baseline
  • 🟥 = significantly worse candidate vs. baseline

We compute a confidence interval (CI) over the relative difference of means between metrics from the candidate and baseline commits, considering the baseline as the reference.

If the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD), the change is considered significant.

Feel free to reach out to #apm-benchmarking-platform on Slack if you have any questions.

More details about the CI and significant changes

You can imagine this CI as a range of values that is likely to contain the true difference of means between the candidate and baseline commits.

CIs of the difference of means are often centered around 0%, because often changes are not that big:

---------------------------------(------|---^--------)-------------------------------->
                              -0.6%    0%  0.3%     +1.2%
                                 |          |        |
         lower bound of the CI --'          |        |
sample mean (center of the CI) -------------'        |
         upper bound of the CI ----------------------'

As described above, a change is considered significant if the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD).

For instance, for an execution time metric, this confidence interval indicates a significantly worse performance:

----------------------------------------|---------|---(---------^---------)---------->
                                       0%        1%  1.3%      2.2%      3.1%
                                                  |   |         |         |
       significant impact threshold --------------'   |         |         |
                      lower bound of CI --------------'         |         |
       sample mean (center of the CI) --------------------------'         |
                      upper bound of CI ----------------------------------'

Unstable benchmarks

These benchmarks have a confidence interval too wide to call a change; treat them as noise rather than signal.

scenario:appsec-appsec-enabled-24

  • unstable execution_time [-180.670ms; +183.529ms] or [-6.836%; +6.944%]

scenario:appsec-appsec-enabled-26

  • unstable execution_time [-211.215ms; +206.213ms] or [-8.393%; +8.194%]

scenario:appsec-appsec-enabled-with-attacks-26

  • unstable execution_time [-156.856ms; +151.331ms] or [-5.462%; +5.269%]

scenario:appsec-control-20

  • unstable execution_time [-107.235ms; +112.985ms] or [-6.619%; +6.974%]

scenario:appsec-control-24

  • unstable execution_time [-103.106ms; +76.831ms] or [-8.572%; +6.388%]

scenario:appsec-control-26

  • unstable execution_time [-106.795ms; +103.423ms] or [-9.004%; +8.720%]

scenario:appsec-iast-no-vulnerability-iast-enabled-default-config-20

  • unstable execution_time [-15470.259µs; +15574.815µs] or [-6.189%; +6.231%]

scenario:debugger-line-probe-with-snapshot-default-24

  • unstable cpu_user_time [-1.226s; +4.160s] or [-13.176%; +44.705%]
  • unstable execution_time [-1.217s; +4.162s] or [-12.156%; +41.581%]
  • unstable instructions [-10.9G instructions; +36.0G instructions] or [-14.264%; +47.229%]
  • unstable max_rss_usage [-4.754MB; +17.692MB] or [-2.949%; +10.976%]
  • unstable throughput [-1179.480op/s; +343.713op/s] or [-35.264%; +10.276%]

scenario:debugger-line-probe-with-snapshot-default-26

  • unstable cpu_user_time [-2172.086ms; +3378.419ms] or [-23.268%; +36.191%]
  • unstable execution_time [-2258.252ms; +3453.326ms] or [-22.527%; +34.448%]
  • unstable instructions [-18.8G instructions; +29.7G instructions] or [-24.251%; +38.235%]
  • unstable max_rss_usage [-6.758MB; +10.724MB] or [-4.252%; +6.748%]
  • unstable throughput [-745.399op/s; +501.701op/s] or [-22.703%; +15.281%]

scenario:debugger-line-probe-with-snapshot-minimal-24

  • unstable cpu_user_time [-2953.516ms; +1773.455ms] or [-30.953%; +18.586%]
  • unstable execution_time [-3007.732ms; +1809.478ms] or [-29.405%; +17.690%]
  • unstable instructions [-25.4G instructions; +15.3G instructions] or [-32.326%; +19.483%]
  • unstable max_rss_usage [-12.494MB; +7.771MB] or [-7.745%; +4.818%]
  • unstable throughput [-511.110op/s; +827.767op/s] or [-15.586%; +25.243%]

scenario:debugger-line-probe-without-snapshot-24

  • unstable cpu_user_time [-180.980ms; +765.825ms] or [-2.295%; +9.712%]
  • unstable execution_time [-148.792ms; +771.931ms] or [-1.737%; +9.012%]
  • unstable instructions [-1.7G instructions; +7.2G instructions] or [-2.679%; +11.176%]
  • unstable throughput [-347.309op/s; +65.091op/s] or [-9.230%; +1.730%]

scenario:debugger-line-probe-without-snapshot-26

  • unstable cpu_user_time [-1894.627ms; +554.539ms] or [-20.381%; +5.965%]
  • unstable execution_time [-1929.831ms; +538.794ms] or [-19.306%; +5.390%]
  • unstable instructions [-17.1G instructions; +5.1G instructions] or [-22.231%; +6.560%]
  • unstable throughput [-114.239op/s; +448.360op/s] or [-3.472%; +13.626%]

scenario:dogstatsd-with-tags-20

  • unstable cpu_user_time [-424.994ms; +271.837ms] or [-8.715%; +5.574%]
  • unstable execution_time [-429.106ms; +272.545ms] or [-8.665%; +5.503%]
  • unstable throughput [-94303.959op/s; +146623.634op/s] or [-5.560%; +8.644%]

scenario:plugin-graphql-long-with-depth-off-20

  • unstable max_rss_usage [-5.489MB; +9.116MB] or [-4.258%; +7.071%]

@szegedi
szegedi merged commit eec5c70 into master Jul 6, 2026
655 of 656 checks passed
@szegedi
szegedi deleted the fix/profiling-upload-timeout-hang branch July 6, 2026 12:35
This was referenced Jul 6, 2026
juan-fernandez pushed a commit that referenced this pull request Jul 8, 2026
The agent profile exporter sets `options.timeout` on the upload request but
never handled the request's 'timeout' event. Per the Node http docs the
timeout event is informational only — it does not abort the socket — so a
stalled upload (e.g. a connection that hangs on a loaded/shared host's
network stack) never emitted 'error', the retry/backoff in
AgentExporter.export never ran, and the export promise never settled. The
worker process then stays alive on its shutdown flush until the container
is force-killed, and the profile is lost.

Handle 'timeout' by destroying the request, which surfaces as an 'error'
and drives the existing retry/backoff — matching the trace exporter in
exporters/common/request.js, which already aborts on timeout.

Also guard the request callback so it runs at most once: the timeout can
fire after a response was already received (e.g. on a lingering keep-alive
socket once it goes idle), which would otherwise invoke the callback twice
and corrupt the retry accounting.

Adds a regression test asserting the export rejects (after retrying)
instead of hanging when the agent never responds. Also bumps the
uploadTimeout in the existing backoff test so its (now enforced) per-attempt
socket timeout doesn't race the in-process test agent's response.
juan-fernandez pushed a commit that referenced this pull request Jul 8, 2026
The agent profile exporter sets `options.timeout` on the upload request but
never handled the request's 'timeout' event. Per the Node http docs the
timeout event is informational only — it does not abort the socket — so a
stalled upload (e.g. a connection that hangs on a loaded/shared host's
network stack) never emitted 'error', the retry/backoff in
AgentExporter.export never ran, and the export promise never settled. The
worker process then stays alive on its shutdown flush until the container
is force-killed, and the profile is lost.

Handle 'timeout' by destroying the request, which surfaces as an 'error'
and drives the existing retry/backoff — matching the trace exporter in
exporters/common/request.js, which already aborts on timeout.

Also guard the request callback so it runs at most once: the timeout can
fire after a response was already received (e.g. on a lingering keep-alive
socket once it goes idle), which would otherwise invoke the callback twice
and corrupt the retry accounting.

Adds a regression test asserting the export rejects (after retrying)
instead of hanging when the agent never responds. Also bumps the
uploadTimeout in the existing backoff test so its (now enforced) per-attempt
socket timeout doesn't race the in-process test agent's response.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants