Skip to content

[AppSec] Dispose the WAF context when a non-web local root span closes - #9057

Open
dromanol wants to merge 4 commits into
masterfrom
dani/dispose-waf-context-non-web-root-span
Open

[AppSec] Dispose the WAF context when a non-web local root span closes#9057
dromanol wants to merge 4 commits into
masterfrom
dani/dispose-waf-context-non-web-root-span

Conversation

@dromanol

@dromanol dromanol commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Summary of changes

The WAF (additive) context is now disposed when the last span of the trace segment closes, not only when a local root span of type web closes. Creation and disposal of that context are also serialized so they can't race.

Reason for change

Creation and release of the WAF context are asymmetric:

  • Creation happens through SecurityCoordinator.TryGet, which only requires CoreHttpContextStore.Instance.Get() to be non-null — it does not check the local root span type. Call sites that reach it without a web root span: EventTrackingSdk.RunSecurityChecksAndReport, EventTrackingSdkV2.RunSecurityChecksAndReport (both carry the comment "Biggest part of this method will only work in a web context" but don't enforce it) and UserManagerCreateIntegration on the ASP.NET Core Identity signup path.
  • Release happened only under span.IsRootSpan && span.Type == SpanTypes.Web.

So a TrackUserLoginSuccess/SetUser/Identity-signup call made under a non-web local root span (background worker, queue consumer, Task.Run continuation…) created a WAF context that nothing ever disposed. Each libddwaf context owns a monotonic_buffer_resource (src/context.hpp) that never reuses freed memory and releases it all only on context destruction, plus the managed Context._encodeResults list which is only drained in Dispose. That memory is native and invisible to the GC, so it showed up as RSS growth with a flat managed heap, recovered only if and when ~Context() happened to run.

Implementation details

  • TraceContext.CloseSpan disposes the context in the _openSpans == 0 branch, i.e. when the trace segment is complete. Waiting for the last span rather than for the local root span matters because a child span or a captured continuation can outlive the root on a non-web trace, and it would otherwise lose WAF coverage.
  • Web traces keep releasing it as soon as their local root span closes (unchanged behaviour). Context.Dispose is idempotent, so the second call is a no-op.
  • AppSecRequestContext.GetOrCreateAdditiveContext and DisposeAdditiveContext now run under the existing _sync lock: without it, a context being created concurrently with the disposal could be assigned just after the only disposal point and stay orphaned until finalization — exactly the leak this PR is about. No new lock ordering, _sync is released before the WAF is run and nothing under Context.Dispose/Context.Run calls back into AppSecRequestContext.

Test coverage

Two new tests in AppSecContextTests, both verified to fail without the corresponding change:

  • GivenAWafContext_WhenTheLocalRootSpanCloses_ThenItIsDisposed — theory over span types null, web and custom: creates a real WAF additive context, closes the span, asserts the context is gone. The null/custom cases fail on master.
  • GivenAWafContextOnANonWebSpan_WhenTheRootClosesBeforeItsChildren_ThenItSurvivesUntilTheyClose — the context stays usable while a child span is still open and is disposed when it closes.

Also ran, on net8.0: the full Datadog.Trace.Security.Unit.Tests suite (1047 passed) and the TraceContext/AppSec/PartialFlush/Tracer tests of Datadog.Trace.Tests (786 passed).

Other details

Found while investigating native memory growth with AppSec enabled (SCRS-2370). #8989 removed one route into this (stale HttpContext surviving in a captured ExecutionContext), but not the asymmetry itself, which is still reachable on master through the call sites listed above.

A complementary hardening would be to also refuse to create the context outside a web root span; that changes detection behaviour, so it is deliberately left out of this PR.

A WAF context can be created on any local root span (the user events SDKs and
the ASP.NET Core Identity integrations don't check the span type), but it was
only disposed when the local root span was of type web, so those contexts kept
their native memory until the finalizer ran, if ever.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@dromanol
dromanol requested review from a team as code owners August 14, 2026 09:52

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8efb27cc5f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tracer/src/Datadog.Trace/TraceContext.cs Outdated
Comment thread tracer/src/Datadog.Trace/TraceContext.cs Outdated
Disposing at local root span close cut off any child span still open on a
non-web trace. Dispose when the last span of the segment closes instead, and
serialize creation and disposal so a context created concurrently with the
disposal can't be orphaned.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@pr-commenter

pr-commenter Bot commented Aug 14, 2026

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-08-14 15:11:40

Comparing candidate commit c3c64ca in PR branch dani/dispose-waf-context-non-web-root-span with baseline commit 57f3327 in branch master.

📊 Benchmarking dashboard

Found 0 performance improvements and 1 performance regressions! Performance is the same for 71 metrics, 0 unstable metrics, 65 known flaky benchmarks, 61 flaky benchmarks without significant changes.

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 ----------------------------------'

scenario:Benchmarks.Trace.DbCommandBenchmark.ExecuteNonQuery net472

  • 🟥 throughput [-34843.126op/s; -31981.617op/s] or [-9.814%; -9.008%]

Known flaky benchmarks

These benchmarks are marked as flaky and will not trigger a failure. Modify FLAKY_BENCHMARKS_REGEX to control which benchmarks are marked as flaky.

scenario:Benchmarks.Trace.ActivityBenchmark.StartStopWithChild net472

  • 🟥 throughput [-8674.873op/s; -8174.874op/s] or [-10.286%; -9.693%]

scenario:Benchmarks.Trace.ActivityBenchmark.StartStopWithChild netcoreapp3.1

  • 🟥 throughput [-8922.147op/s; -7651.211op/s] or [-9.072%; -7.780%]

scenario:Benchmarks.Trace.AgentWriterBenchmark.WriteAndFlushEnrichedTraces net472

  • 🟥 allocated_mem [+1.541KB; +1.541KB] or [+46.817%; +46.831%]
  • 🟥 execution_time [+310.830ms; +314.399ms] or [+154.245%; +156.016%]
  • 🟥 throughput [-60.177op/s; -55.444op/s] or [-10.827%; -9.975%]

scenario:Benchmarks.Trace.AgentWriterBenchmark.WriteAndFlushEnrichedTraces net6.0

  • 🟥 allocated_mem [+1.013KB; +1.013KB] or [+37.563%; +37.575%]
  • 🟥 execution_time [+377.459ms; +380.223ms] or [+298.216%; +300.400%]
  • 🟩 throughput [+62.442op/s; +66.973op/s] or [+8.233%; +8.830%]

scenario:Benchmarks.Trace.AgentWriterBenchmark.WriteAndFlushEnrichedTraces netcoreapp3.1

  • 🟥 allocated_mem [+1.090KB; +1.090KB] or [+40.417%; +40.429%]
  • 🟥 execution_time [+396.820ms; +399.982ms] or [+351.171%; +353.969%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.AllCycleMoreComplexBody net472

  • 🟥 allocated_mem [+4.693KB; +4.694KB] or [+98.806%; +98.821%]
  • 🟥 throughput [-61008.542op/s; -60576.086op/s] or [-47.468%; -47.131%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.AllCycleMoreComplexBody net6.0

  • 🟥 allocated_mem [+3.816KB; +3.816KB] or [+80.699%; +80.711%]
  • 🟩 execution_time [-15.759ms; -11.543ms] or [-7.360%; -5.391%]
  • 🟥 throughput [-60845.480op/s; -58080.688op/s] or [-44.414%; -42.396%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.AllCycleMoreComplexBody netcoreapp3.1

  • 🟥 allocated_mem [+4.544KB; +4.544KB] or [+98.261%; +98.274%]
  • 🟥 throughput [-48936.173op/s; -46682.519op/s] or [-44.244%; -42.206%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.AllCycleSimpleBody net472

  • 🟥 allocated_mem [+1.316KB; +1.316KB] or [+106.475%; +106.490%]
  • 🟥 throughput [-266108.159op/s; -261613.979op/s] or [-27.171%; -26.712%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.AllCycleSimpleBody net6.0

  • 🟥 allocated_mem [+479 bytes; +480 bytes] or [+39.212%; +39.221%]
  • 🟩 execution_time [-25.779ms; -20.873ms] or [-11.496%; -9.309%]
  • 🟥 throughput [-90838.773op/s; -66961.473op/s] or [-9.704%; -7.154%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.AllCycleSimpleBody netcoreapp3.1

  • 🟥 allocated_mem [+1.280KB; +1.280KB] or [+105.947%; +105.963%]
  • 🟥 throughput [-167383.019op/s; -150923.808op/s] or [-24.050%; -21.685%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorMoreComplexBody net472

  • 🟥 allocated_mem [+3.378KB; +3.378KB] or [+89.003%; +89.017%]
  • 🟥 throughput [-72704.069op/s; -71915.322op/s] or [-48.929%; -48.398%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorMoreComplexBody net6.0

  • 🟥 allocated_mem [+3.336KB; +3.336KB] or [+88.150%; +88.161%]
  • 🟥 throughput [-73597.963op/s; -70703.056op/s] or [-46.829%; -44.987%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorMoreComplexBody netcoreapp3.1

  • 🟥 allocated_mem [+3.264KB; +3.264KB] or [+88.493%; +88.506%]
  • 🟥 throughput [-55807.656op/s; -53191.045op/s] or [-44.458%; -42.374%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorSimpleBody net6.0

  • 🟩 throughput [+281365.901op/s; +323842.591op/s] or [+9.382%; +10.798%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorSimpleBody netcoreapp3.1

  • 🟩 execution_time [-18.700ms; -14.372ms] or [-8.620%; -6.625%]

scenario:Benchmarks.Trace.Asm.AppSecEncoderBenchmark.EncodeArgs net472

  • 🟩 allocated_mem [-13.759KB; -13.756KB] or [-42.324%; -42.316%]
  • 🟥 execution_time [+299.649ms; +300.434ms] or [+149.724%; +150.117%]
  • 🟩 throughput [+947.534op/s; +969.726op/s] or [+10.465%; +10.711%]

scenario:Benchmarks.Trace.Asm.AppSecEncoderBenchmark.EncodeArgs net6.0

  • 🟩 allocated_mem [-13.722KB; -13.718KB] or [-42.341%; -42.329%]
  • 🟥 execution_time [+300.332ms; +303.439ms] or [+151.458%; +153.025%]
  • 🟩 throughput [+2370.921op/s; +2585.699op/s] or [+18.134%; +19.777%]

scenario:Benchmarks.Trace.Asm.AppSecEncoderBenchmark.EncodeArgs netcoreapp3.1

  • 🟩 allocated_mem [-13.722KB; -13.718KB] or [-42.341%; -42.329%]
  • 🟥 execution_time [+300.970ms; +303.421ms] or [+151.605%; +152.840%]
  • 🟩 throughput [+1773.179op/s; +1898.598op/s] or [+17.119%; +18.330%]

scenario:Benchmarks.Trace.Asm.AppSecEncoderBenchmark.EncodeLegacyArgs net472

  • 🟥 execution_time [+296.299ms; +297.223ms] or [+145.530%; +145.984%]
  • 🟩 throughput [+540.723op/s; +548.923op/s] or [+14.335%; +14.552%]

scenario:Benchmarks.Trace.Asm.AppSecEncoderBenchmark.EncodeLegacyArgs net6.0

  • 🟥 execution_time [+297.102ms; +299.690ms] or [+145.242%; +146.507%]
  • 🟩 throughput [+2649.715op/s; +2684.459op/s] or [+38.495%; +39.000%]

scenario:Benchmarks.Trace.Asm.AppSecEncoderBenchmark.EncodeLegacyArgs netcoreapp3.1

  • 🟥 execution_time [+301.310ms; +302.109ms] or [+150.594%; +150.994%]
  • 🟩 throughput [+1316.985op/s; +1335.874op/s] or [+26.141%; +26.516%]

scenario:Benchmarks.Trace.Asm.AppSecWafBenchmark.RunWafRealisticBenchmark net472

  • 🟩 execution_time [-143.673µs; -139.183µs] or [-29.498%; -28.576%]
  • 🟩 throughput [+825.498op/s; +856.450op/s] or [+40.205%; +41.713%]

scenario:Benchmarks.Trace.Asm.AppSecWafBenchmark.RunWafRealisticBenchmark net6.0

  • 🟩 execution_time [-134.935µs; -108.256µs] or [-30.947%; -24.829%]
  • 🟩 throughput [+819.870op/s; +942.410op/s] or [+35.645%; +40.972%]

scenario:Benchmarks.Trace.Asm.AppSecWafBenchmark.RunWafRealisticBenchmark netcoreapp3.1

  • 🟩 execution_time [-141.711µs; -119.509µs] or [-30.362%; -25.605%]
  • 🟩 throughput [+766.673op/s; +853.175op/s] or [+35.391%; +39.384%]

scenario:Benchmarks.Trace.Asm.AppSecWafBenchmark.RunWafRealisticBenchmarkWithAttack net472

  • 🟩 execution_time [-125.222µs; -120.574µs] or [-33.809%; -32.554%]
  • 🟩 throughput [+1313.998op/s; +1371.543op/s] or [+48.664%; +50.796%]

scenario:Benchmarks.Trace.Asm.AppSecWafBenchmark.RunWafRealisticBenchmarkWithAttack net6.0

  • 🟩 execution_time [-102.766µs; -79.184µs] or [-32.808%; -25.279%]
  • 🟩 throughput [+1191.661op/s; +1392.860op/s] or [+37.148%; +43.420%]

scenario:Benchmarks.Trace.Asm.AppSecWafBenchmark.RunWafRealisticBenchmarkWithAttack netcoreapp3.1

  • 🟩 execution_time [-131.463µs; -103.165µs] or [-35.963%; -28.222%]
  • 🟩 throughput [+1167.617op/s; +1400.768op/s] or [+41.901%; +50.268%]

scenario:Benchmarks.Trace.AspNetCoreBenchmark.SendRequest net472

  • 🟥 execution_time [+299.509ms; +300.310ms] or [+149.485%; +149.885%]

scenario:Benchmarks.Trace.AspNetCoreBenchmark.SendRequest net6.0

  • unstable execution_time [+332.882ms; +389.906ms] or [+361.690%; +423.649%]
  • 🟩 throughput [+825.047op/s; +986.510op/s] or [+6.780%; +8.106%]

scenario:Benchmarks.Trace.AspNetCoreBenchmark.SendRequest netcoreapp3.1

  • 🟥 execution_time [+370.734ms; +374.313ms] or [+281.495%; +284.212%]

scenario:Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark.WriteAndFlushEnrichedTraces net472

  • unstable execution_time [+348.175ms; +406.173ms] or [+160.087%; +186.754%]
  • 🟥 throughput [-538.263op/s; -487.285op/s] or [-48.772%; -44.153%]

scenario:Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark.WriteAndFlushEnrichedTraces net6.0

  • unstable execution_time [+209.901ms; +343.155ms] or [+89.451%; +146.238%]
  • 🟥 throughput [-670.831op/s; -587.320op/s] or [-44.745%; -39.174%]

scenario:Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark.WriteAndFlushEnrichedTraces netcoreapp3.1

  • 🟥 execution_time [+346.356ms; +357.792ms] or [+207.161%; +214.001%]
  • 🟥 throughput [-425.090op/s; -386.587op/s] or [-29.598%; -26.917%]

scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSlice net472

  • 🟩 execution_time [-112.748µs; -100.757µs] or [-5.674%; -5.071%]
  • 🟩 throughput [+26.958op/s; +30.304op/s] or [+5.357%; +6.022%]

scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSliceWithPool net6.0

  • 🟩 execution_time [-113.980µs; -65.167µs] or [-10.571%; -6.044%]
  • 🟩 throughput [+64.374op/s; +121.506op/s] or [+6.941%; +13.101%]

scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSliceWithPool netcoreapp3.1

  • unstable execution_time [-212.909µs; -19.685µs] or [-11.406%; -1.055%]
  • unstable throughput [+17.018op/s; +141.772op/s] or [+3.177%; +26.463%]

scenario:Benchmarks.Trace.CharSliceBenchmark.OriginalCharSlice net6.0

  • 🟩 execution_time [-195.307µs; -169.102µs] or [-9.894%; -8.566%]
  • 🟩 throughput [+47.907op/s; +55.844op/s] or [+9.457%; +11.024%]

scenario:Benchmarks.Trace.ElasticsearchBenchmark.CallElasticsearch net472

  • 🟥 execution_time [+302.897ms; +305.181ms] or [+152.533%; +153.684%]

scenario:Benchmarks.Trace.ElasticsearchBenchmark.CallElasticsearch net6.0

  • 🟥 execution_time [+301.107ms; +303.812ms] or [+150.885%; +152.241%]

scenario:Benchmarks.Trace.ElasticsearchBenchmark.CallElasticsearch netcoreapp3.1

  • 🟥 execution_time [+299.456ms; +302.717ms] or [+150.434%; +152.072%]

scenario:Benchmarks.Trace.ElasticsearchBenchmark.CallElasticsearchAsync net472

  • 🟥 execution_time [+301.609ms; +303.202ms] or [+151.458%; +152.258%]

scenario:Benchmarks.Trace.ElasticsearchBenchmark.CallElasticsearchAsync net6.0

  • 🟥 execution_time [+297.016ms; +299.542ms] or [+146.861%; +148.110%]

scenario:Benchmarks.Trace.ElasticsearchBenchmark.CallElasticsearchAsync netcoreapp3.1

  • 🟥 execution_time [+301.134ms; +305.397ms] or [+152.628%; +154.789%]

scenario:Benchmarks.Trace.GraphQLBenchmark.ExecuteAsync net472

  • 🟥 execution_time [+301.781ms; +304.613ms] or [+151.467%; +152.888%]

scenario:Benchmarks.Trace.GraphQLBenchmark.ExecuteAsync net6.0

  • 🟥 execution_time [+296.649ms; +299.146ms] or [+147.853%; +149.097%]
  • 🟩 throughput [+45608.443op/s; +51440.222op/s] or [+9.056%; +10.214%]

scenario:Benchmarks.Trace.GraphQLBenchmark.ExecuteAsync netcoreapp3.1

  • 🟥 execution_time [+300.322ms; +302.839ms] or [+149.408%; +150.660%]

scenario:Benchmarks.Trace.ILoggerBenchmark.EnrichedLog net6.0

  • 🟩 execution_time [-15.667ms; -12.019ms] or [-7.285%; -5.589%]
  • 🟩 throughput [+18578.620op/s; +25576.433op/s] or [+5.097%; +7.016%]

scenario:Benchmarks.Trace.Iast.StringAspectsBenchmark.StringConcatAspectBenchmark net472

  • unstable execution_time [+21.173µs; +66.161µs] or [+5.230%; +16.342%]
  • 🟥 throughput [-342.442op/s; -124.462op/s] or [-13.780%; -5.008%]

scenario:Benchmarks.Trace.Iast.StringAspectsBenchmark.StringConcatAspectBenchmark net6.0

  • 🟩 allocated_mem [-20.961KB; -20.938KB] or [-7.646%; -7.638%]
  • unstable execution_time [-42.044µs; +14.815µs] or [-8.310%; +2.928%]

scenario:Benchmarks.Trace.Iast.StringAspectsBenchmark.StringConcatAspectBenchmark netcoreapp3.1

  • unstable execution_time [-73.394µs; -12.059µs] or [-12.719%; -2.090%]

scenario:Benchmarks.Trace.Iast.StringAspectsBenchmark.StringConcatBenchmark net6.0

  • unstable execution_time [+7.239µs; +12.411µs] or [+17.112%; +29.337%]
  • 🟥 throughput [-5295.528op/s; -3318.538op/s] or [-22.293%; -13.970%]

scenario:Benchmarks.Trace.Iast.StringAspectsBenchmark.StringConcatBenchmark netcoreapp3.1

  • unstable execution_time [-13.114µs; -4.348µs] or [-20.346%; -6.746%]
  • unstable throughput [+1169.933op/s; +3046.907op/s] or [+7.178%; +18.694%]

scenario:Benchmarks.Trace.Log4netBenchmark.EnrichedLog net472

  • 🟥 execution_time [+302.238ms; +303.782ms] or [+152.768%; +153.548%]

scenario:Benchmarks.Trace.Log4netBenchmark.EnrichedLog net6.0

  • 🟥 execution_time [+301.520ms; +304.700ms] or [+153.473%; +155.091%]

scenario:Benchmarks.Trace.Log4netBenchmark.EnrichedLog netcoreapp3.1

  • 🟥 execution_time [+299.728ms; +301.882ms] or [+150.051%; +151.129%]

scenario:Benchmarks.Trace.SerilogBenchmark.EnrichedLog net472

  • 🟥 execution_time [+298.694ms; +300.624ms] or [+148.872%; +149.834%]

scenario:Benchmarks.Trace.SerilogBenchmark.EnrichedLog net6.0

  • 🟥 execution_time [+300.602ms; +302.843ms] or [+150.948%; +152.073%]

scenario:Benchmarks.Trace.SerilogBenchmark.EnrichedLog netcoreapp3.1

  • 🟥 execution_time [+303.262ms; +305.334ms] or [+153.795%; +154.846%]

scenario:Benchmarks.Trace.SingleSpanAspNetCoreBenchmark.SingleSpanAspNetCore net472

  • 🟥 execution_time [+300.073ms; +301.115ms] or [+149.678%; +150.198%]
  • 🟩 throughput [+65889842.210op/s; +66205860.707op/s] or [+47.985%; +48.215%]

scenario:Benchmarks.Trace.SingleSpanAspNetCoreBenchmark.SingleSpanAspNetCore net6.0

  • unstable execution_time [+342.008ms; +400.975ms] or [+425.347%; +498.684%]

scenario:Benchmarks.Trace.SingleSpanAspNetCoreBenchmark.SingleSpanAspNetCore netcoreapp3.1

  • 🟥 execution_time [+300.336ms; +301.568ms] or [+149.801%; +150.415%]
  • 🟩 throughput [+17927267.829op/s; +18908071.655op/s] or [+7.941%; +8.375%]

scenario:Benchmarks.Trace.SpanBenchmark.StartFinishSpan net6.0

  • 🟩 throughput [+65588.096op/s; +102575.164op/s] or [+5.077%; +7.940%]

scenario:Benchmarks.Trace.SpanBenchmark.StartFinishSpan netcoreapp3.1

  • 🟩 throughput [+64674.189op/s; +74495.350op/s] or [+6.423%; +7.399%]

scenario:Benchmarks.Trace.SpanBenchmark.StartFinishTwoScopes net6.0

  • 🟩 throughput [+42188.416op/s; +50445.597op/s] or [+7.661%; +9.160%]

Known flaky benchmarks without significant changes:

  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.ActivityBenchmark.StartSpan net472
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.ActivityBenchmark.StartSpan net6.0
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.ActivityBenchmark.StartSpan netcoreapp3.1
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.ActivityBenchmark.StartSpan_AddEvent_Sampled net472
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.ActivityBenchmark.StartSpan_AddEvent_Sampled net6.0
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.ActivityBenchmark.StartSpan_AddEvent_Sampled netcoreapp3.1
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.ActivityBenchmark.StartSpan_GetContext_Sampled net472
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.ActivityBenchmark.StartSpan_GetContext_Sampled net6.0
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.ActivityBenchmark.StartSpan_GetContext_Sampled netcoreapp3.1
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.ActivityBenchmark.StartSpan_SetAttributes_Sampled net472
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.ActivityBenchmark.StartSpan_SetAttributes_Sampled net6.0
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.ActivityBenchmark.StartSpan_SetAttributes_Sampled netcoreapp3.1
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.ActivityBenchmark.StartSpan_SetStatus_Sampled net472
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.ActivityBenchmark.StartSpan_SetStatus_Sampled net6.0
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.ActivityBenchmark.StartSpan_SetStatus_Sampled netcoreapp3.1
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.ActivityBenchmark.StartSpan_UpdateName_Sampled net472
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.ActivityBenchmark.StartSpan_UpdateName_Sampled net6.0
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.ActivityBenchmark.StartSpan_UpdateName_Sampled netcoreapp3.1
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan net472
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan net6.0
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan netcoreapp3.1
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan_AddEvent_Sampled net472
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan_AddEvent_Sampled net6.0
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan_AddEvent_Sampled netcoreapp3.1
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan_GetContext_Sampled net472
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan_GetContext_Sampled net6.0
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan_GetContext_Sampled netcoreapp3.1
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan_RecordException_Sampled net472
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan_RecordException_Sampled net6.0
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan_RecordException_Sampled netcoreapp3.1
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan_SetAttributes_Sampled net472
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan_SetAttributes_Sampled net6.0
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan_SetAttributes_Sampled netcoreapp3.1
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan_SetStatus_Sampled net472
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan_SetStatus_Sampled net6.0
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan_SetStatus_Sampled netcoreapp3.1
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan_UpdateName_Sampled net472
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan_UpdateName_Sampled net6.0
  • scenario:Benchmarks.OpenTelemetry.InstrumentedApi.Trace.TelemetrySpanBenchmark.StartSpan_UpdateName_Sampled netcoreapp3.1
  • scenario:Benchmarks.Trace.ActivityBenchmark.StartStopWithChild net6.0
  • scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorSimpleBody net472
  • scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSlice net6.0
  • scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSlice netcoreapp3.1
  • scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSliceWithPool net472
  • scenario:Benchmarks.Trace.CharSliceBenchmark.OriginalCharSlice net472
  • scenario:Benchmarks.Trace.CharSliceBenchmark.OriginalCharSlice netcoreapp3.1
  • scenario:Benchmarks.Trace.ILoggerBenchmark.EnrichedLog net472
  • scenario:Benchmarks.Trace.ILoggerBenchmark.EnrichedLog netcoreapp3.1
  • scenario:Benchmarks.Trace.Iast.StringAspectsBenchmark.StringConcatBenchmark net472
  • scenario:Benchmarks.Trace.RedisBenchmark.SendReceive net472
  • scenario:Benchmarks.Trace.RedisBenchmark.SendReceive net6.0
  • scenario:Benchmarks.Trace.RedisBenchmark.SendReceive netcoreapp3.1
  • scenario:Benchmarks.Trace.SpanBenchmark.StartFinishScope net472
  • scenario:Benchmarks.Trace.SpanBenchmark.StartFinishScope net6.0
  • scenario:Benchmarks.Trace.SpanBenchmark.StartFinishScope netcoreapp3.1
  • scenario:Benchmarks.Trace.SpanBenchmark.StartFinishSpan net472
  • scenario:Benchmarks.Trace.SpanBenchmark.StartFinishTwoScopes net472
  • scenario:Benchmarks.Trace.SpanBenchmark.StartFinishTwoScopes netcoreapp3.1
  • scenario:Benchmarks.Trace.TraceAnnotationsBenchmark.RunOnMethodBegin net472
  • scenario:Benchmarks.Trace.TraceAnnotationsBenchmark.RunOnMethodBegin net6.0
  • scenario:Benchmarks.Trace.TraceAnnotationsBenchmark.RunOnMethodBegin netcoreapp3.1

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@dd-trace-dotnet-ci-bot

dd-trace-dotnet-ci-bot Bot commented Aug 14, 2026

Copy link
Copy Markdown

Execution-Time Benchmarks Report ⏱️

Execution-time results for samples comparing This PR (9057) and master.

✅ No regressions detected

📄 View the full report (charts + all metrics) →

@dromanol

Copy link
Copy Markdown
Contributor Author

@codex review

Removed commented-out code regarding WAF context disposal.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6f650d6462

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

// this is the point where nothing can use it anymore. Disposing twice is a no-op.
// This has to happen while holding the lock, or AddSpan could reopen the segment
// in between and we would dispose the context of a span that is still running.
_appSecRequestContext?.DisposeAdditiveContext();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve the WAF context when a captured continuation starts late

When Task.Run captures a non-web root scope but starts its child only after the root closes, _openSpans reaches zero here and permanently disposes the additive context. The continuation's later StartActive still reuses the captured parent's TraceContext (Tracer.CreateSpanContext), and AddSpan permits the zero count to reopen, but GetOrCreateAdditiveContext now returns null, silently skipping user-event or Identity WAF checks. Fresh evidence beyond the prior thread is that AddSpan has no terminal-state check; holding the lock merely orders this disposal before the late AddSpan and therefore does not protect a captured continuation.

Useful? React with 👍 / 👎.

// this is the point where nothing can use it anymore. Disposing twice is a no-op.
// This has to happen while holding the lock, or AddSpan could reopen the segment
// in between and we would dispose the context of a span that is still running.
_appSecRequestContext?.DisposeAdditiveContext();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Serialize lazy request-context creation with trace closure

When the first WAF check races with the last span closing before _appSecRequestContext has been initialized, this null-conditional call can observe null and do nothing; immediately afterward the AppSecRequestContext getter can install a new object and GetOrCreateAdditiveContext can allocate its native context. With no spans left, there is no subsequent disposal point, so the leak remains. Fresh evidence beyond the resolved creation/disposal thread is that the lazy Interlocked.CompareExchange in the TraceContext.AppSecRequestContext getter occurs outside the _rootSpan lock, so the new per-context _sync cannot serialize this race because the object does not exist yet.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant