Skip to content

Switch Activity handling to use WeakReference<T>#8549

Merged
andrewlock merged 6 commits into
masterfrom
andrew/activity-weak-reference
May 6, 2026
Merged

Switch Activity handling to use WeakReference<T>#8549
andrewlock merged 6 commits into
masterfrom
andrew/activity-weak-reference

Conversation

@andrewlock

@andrewlock andrewlock commented May 1, 2026

Copy link
Copy Markdown
Member

Summary of changes

Wraps the reference to Activity in our ActivityHandlerCommon with a WeakReference<T> to avoid leaking them if user's never stop the Activity

Reason for change

We have seen this happening in practice, and while it's a coding error, it manifests as a memory leak that's hard to diagnose without taking a memory dump.

This PR attempts to sidestep the issue by using WeakReference, to avoid keeping an Activity alive for longer than it otherwise would. It then adds a "reconciliation" process that cleans up and removes any Activity object that is no longer reachable, and closes and finishes the corresponding Span, so that we don't keep those around for ever (actually the bigger issue WRT memory leaks)

Additionally, we already had an StopActivitySlow process which was doing something similar, looping through the whole dictionary, trying to find any closed Activity instances where the Span was still open. This was looping through every item in the dictionary, Every time a span was closed (which in the pathological case would be very slow), so this PR moves that to the reconciliation background task too).

Implementation details

  • Change ActivityMapping to use WeakReference<object> for the Activity instead of object
  • On app start, start call StartReconciliationLoop to have a process that loops the dictionary to look for missing references or Activity instances which are stopped but for which we didn't close the Span (shouldn't happen as I understand it)
  • When we do need to stop a Span, set some basic properties to try to make them "sensible".
    • Set SpanKind, OperationName,OtelStatusCode, ServiceName, ResourceName, Type if not currently set
    • Add a tag closed_reason=garbage_collected to the span, so we/customers can identify spans like this
  • Add some logging when we find these cases
    • We could potentially add telemetry, but didn't seem worth it initially, we can always add it later

Test coverage

  • Existing tests ensure that existing behaviour works as expected
  • Add tests for the reconciliation loop specifically

Also created a pathological app that creates (and never stops) an Activity, and then hammer it in a loop

  • Without instrumentation (OTel only), there's no significant increase in memory (expected, ~50MB in my sample)
  • Before the fix, constant increase in memory use (I stopped at ~1GB)
  • After the fix, memory stabilizes (~150MB)

@andrewlock
andrewlock requested review from a team as code owners May 1, 2026 15:38
@andrewlock andrewlock added the type:performance Performance, speed, latency, resource usage (CPU, memory) label May 1, 2026
@andrewlock
andrewlock requested a review from zacharycmontoya May 1, 2026 15:38
@andrewlock andrewlock added type:reliability area:opentelemetry OpenTelemetry support AI Generated Largely based on code generated by an AI or LLM. This label is the same across all dd-trace-* repos labels May 1, 2026
try
{
if (activity.Instance is not null)
if (activity.Instance is null)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The bulk of this method is just whitespace changes, just changed to early return

return;
}

StopActivitySlow(sourceName, activity);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This doesn't happen here now, happens in reconciliation loop

return new ReconciliationSweepResult(gcCollected, missedStop, iterated);
}

private static void CloseActivityScope<TInner>(TInner activity, Scope scope)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This hasn't changed, just made it a method instead of local function for reuse

@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: 20e01202c0

ℹ️ 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 on lines +290 to +294
// No matching entry — either Start was never observed, or the periodic
// reconciliation sweep already cleaned this one up. Nothing to do here.
if (Log.IsEnabled(LogEventLevel.Information))
{
Log.Information("DefaultActivityHandler.ActivityStopped: MISSING SCOPE [Source={SourceName}, Id={Id}, RootId={RootId}, OperationName={OperationName}, StartTimeUtc={StartTimeUtc}, Duration={Duration}]", new object[] { sourceName, activity.Id, activity.RootId, activity.OperationName!, activity.StartTimeUtc, activity.Duration });

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Restore same-context cleanup on missed Activity stops

When a Stop callback arrives for an Activity whose exact key lookup misses, this now only logs and relies on the timer-based reconciliation. For the key-mismatch cases that the previous StopActivitySlow handled, the Datadog scope is still active in the caller's AsyncLocal context; the later timer sweep runs in a different ExecutionContext, and AsyncLocalScopeManager.Close() will not clear that original active scope. This can leave subsequent spans in the same request parented to the stale Activity span even after the sweep finishes/removes it.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If we don't have a lookup key, I'm not sure that this is going to be the case. Right now, as I understand it, we don't have a good scenario where we expect to hit this, and doing the reconciliation in this hot path has performance impacts I believe 🤔

Comment on lines +430 to +434
if (activity6.Duration != TimeSpan.Zero
&& mappings.TryRemove(kvp.Key, out var owned)
&& owned.Scope is { } scope)
{
if (activity4.Duration != TimeSpan.Zero)
{
CloseActivityScope(sourceName, activity4, scope);
hasClosed = true;
}
CloseActivityScope(activity6, scope);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid treating SetEndTime as a missed Stop

The new sweep is periodic, so Duration != TimeSpan.Zero is no longer only evaluated while handling a Stop callback. Activity.SetEndTime() sets Duration without stopping the Activity, so a live Activity that sets an end time and continues for more than ReconciliationInterval will be removed and finished here before its actual Stop callback, dropping tags/events added afterward and making the real Stop report a missing scope. Use an actual stopped signal where available, or avoid sweeping live entries based only on Duration.

Useful? React with 👍 / 👎.

Comment thread tracer/src/Datadog.Trace/Activity/Handlers/ActivityHandlerCommon.cs Outdated
Comment thread tracer/test/Datadog.Trace.Tests/Activity/ReconciliationSweepTests.cs Outdated

@zacharycmontoya zacharycmontoya left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, and great tests!

@dd-trace-dotnet-ci-bot

dd-trace-dotnet-ci-bot Bot commented May 1, 2026

Copy link
Copy Markdown

Execution-Time Benchmarks Report ⏱️

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

✅ No regressions detected - check the details below

Full Metrics Comparison

FakeDbCommand

Metric Master (Mean ± 95% CI) Current (Mean ± 95% CI) Change Status
.NET Framework 4.8 - Baseline
duration72.48 ± (72.63 - 73.07) ms72.88 ± (72.76 - 73.06) ms+0.6%✅⬆️
.NET Framework 4.8 - Bailout
duration77.04 ± (76.93 - 77.25) ms78.33 ± (78.38 - 78.91) ms+1.7%✅⬆️
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1070.40 ± (1069.43 - 1075.47) ms1077.43 ± (1076.11 - 1081.57) ms+0.7%✅⬆️
.NET Core 3.1 - Baseline
process.internal_duration_ms22.76 ± (22.70 - 22.83) ms22.49 ± (22.43 - 22.54) ms-1.2%
process.time_to_main_ms86.33 ± (85.95 - 86.71) ms84.18 ± (83.98 - 84.39) ms-2.5%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.91 ± (10.91 - 10.92) MB10.91 ± (10.91 - 10.92) MB+0.0%✅⬆️
runtime.dotnet.threads.count12 ± (12 - 12)12 ± (12 - 12)+0.0%
.NET Core 3.1 - Bailout
process.internal_duration_ms22.37 ± (22.32 - 22.42) ms22.74 ± (22.67 - 22.80) ms+1.6%✅⬆️
process.time_to_main_ms86.86 ± (86.63 - 87.09) ms88.59 ± (88.29 - 88.90) ms+2.0%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.93 ± (10.93 - 10.94) MB10.94 ± (10.94 - 10.95) MB+0.1%✅⬆️
runtime.dotnet.threads.count13 ± (13 - 13)13 ± (13 - 13)+0.0%
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms214.96 ± (214.14 - 215.79) ms212.77 ± (211.86 - 213.67) ms-1.0%
process.time_to_main_ms529.96 ± (528.53 - 531.39) ms525.81 ± (524.41 - 527.20) ms-0.8%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed47.89 ± (47.86 - 47.92) MB47.99 ± (47.96 - 48.02) MB+0.2%✅⬆️
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)+0.0%✅⬆️
.NET 6 - Baseline
process.internal_duration_ms21.44 ± (21.40 - 21.49) ms21.18 ± (21.15 - 21.22) ms-1.2%
process.time_to_main_ms75.13 ± (74.84 - 75.43) ms73.51 ± (73.33 - 73.69) ms-2.2%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.61 ± (10.61 - 10.61) MB10.63 ± (10.62 - 10.63) MB+0.2%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 6 - Bailout
process.internal_duration_ms21.51 ± (21.46 - 21.56) ms21.17 ± (21.13 - 21.21) ms-1.6%
process.time_to_main_ms76.73 ± (76.45 - 77.00) ms74.90 ± (74.69 - 75.10) ms-2.4%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.73 ± (10.73 - 10.73) MB10.74 ± (10.74 - 10.74) MB+0.1%✅⬆️
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms381.24 ± (379.27 - 383.20) ms378.83 ± (376.46 - 381.19) ms-0.6%
process.time_to_main_ms530.63 ± (529.24 - 532.01) ms530.69 ± (529.46 - 531.92) ms+0.0%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed49.27 ± (49.25 - 49.30) MB49.43 ± (49.40 - 49.45) MB+0.3%✅⬆️
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)-0.4%
.NET 8 - Baseline
process.internal_duration_ms19.59 ± (19.55 - 19.63) ms19.92 ± (19.86 - 19.97) ms+1.7%✅⬆️
process.time_to_main_ms73.07 ± (72.88 - 73.26) ms75.66 ± (75.38 - 75.94) ms+3.5%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.67 ± (7.67 - 7.68) MB7.67 ± (7.66 - 7.67) MB-0.1%
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 8 - Bailout
process.internal_duration_ms19.89 ± (19.84 - 19.94) ms19.62 ± (19.59 - 19.66) ms-1.4%
process.time_to_main_ms76.32 ± (76.07 - 76.57) ms74.82 ± (74.65 - 74.99) ms-2.0%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.74 ± (7.73 - 7.74) MB7.71 ± (7.70 - 7.71) MB-0.4%
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms299.70 ± (297.68 - 301.73) ms299.81 ± (297.80 - 301.83) ms+0.0%✅⬆️
process.time_to_main_ms490.33 ± (489.13 - 491.52) ms493.62 ± (492.54 - 494.70) ms+0.7%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed36.64 ± (36.62 - 36.67) MB36.56 ± (36.53 - 36.59) MB-0.2%
runtime.dotnet.threads.count27 ± (27 - 27)27 ± (27 - 27)-0.2%

HttpMessageHandler

Metric Master (Mean ± 95% CI) Current (Mean ± 95% CI) Change Status
.NET Framework 4.8 - Baseline
duration204.61 ± (204.54 - 205.52) ms202.23 ± (202.26 - 203.21) ms-1.2%
.NET Framework 4.8 - Bailout
duration211.55 ± (211.06 - 212.16) ms206.09 ± (206.35 - 207.48) ms-2.6%
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1212.66 ± (1213.95 - 1222.15) ms1209.24 ± (1209.41 - 1217.80) ms-0.3%
.NET Core 3.1 - Baseline
process.internal_duration_ms200.99 ± (200.44 - 201.54) ms199.77 ± (199.26 - 200.28) ms-0.6%
process.time_to_main_ms87.36 ± (87.10 - 87.62) ms86.58 ± (86.28 - 86.89) ms-0.9%
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed15.96 ± (15.94 - 15.97) MB15.99 ± (15.97 - 16.00) MB+0.2%✅⬆️
runtime.dotnet.threads.count20 ± (20 - 20)20 ± (20 - 20)+0.1%✅⬆️
.NET Core 3.1 - Bailout
process.internal_duration_ms200.19 ± (199.65 - 200.73) ms198.74 ± (198.18 - 199.30) ms-0.7%
process.time_to_main_ms88.66 ± (88.40 - 88.91) ms87.98 ± (87.69 - 88.26) ms-0.8%
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed16.03 ± (16.01 - 16.05) MB16.00 ± (15.98 - 16.01) MB-0.2%
runtime.dotnet.threads.count21 ± (21 - 21)21 ± (21 - 21)-0.7%
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms395.24 ± (393.74 - 396.75) ms401.00 ± (399.21 - 402.79) ms+1.5%✅⬆️
process.time_to_main_ms538.30 ± (536.87 - 539.73) ms541.12 ± (539.52 - 542.72) ms+0.5%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed58.28 ± (58.11 - 58.45) MB58.47 ± (58.35 - 58.60) MB+0.3%✅⬆️
runtime.dotnet.threads.count30 ± (30 - 30)30 ± (30 - 30)-0.3%
.NET 6 - Baseline
process.internal_duration_ms204.40 ± (203.82 - 204.98) ms205.11 ± (204.51 - 205.71) ms+0.3%✅⬆️
process.time_to_main_ms75.44 ± (75.19 - 75.70) ms75.39 ± (75.11 - 75.66) ms-0.1%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed16.25 ± (16.23 - 16.26) MB16.31 ± (16.29 - 16.33) MB+0.4%✅⬆️
runtime.dotnet.threads.count19 ± (19 - 19)19 ± (19 - 19)+0.3%✅⬆️
.NET 6 - Bailout
process.internal_duration_ms202.69 ± (202.14 - 203.24) ms204.99 ± (204.36 - 205.61) ms+1.1%✅⬆️
process.time_to_main_ms76.12 ± (75.88 - 76.36) ms76.86 ± (76.62 - 77.11) ms+1.0%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed16.25 ± (16.24 - 16.27) MB16.33 ± (16.31 - 16.35) MB+0.4%✅⬆️
runtime.dotnet.threads.count20 ± (20 - 20)20 ± (20 - 20)+0.3%✅⬆️
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms598.51 ± (596.33 - 600.69) ms596.29 ± (593.75 - 598.82) ms-0.4%
process.time_to_main_ms538.85 ± (537.65 - 540.05) ms542.63 ± (541.23 - 544.03) ms+0.7%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed61.16 ± (61.06 - 61.25) MB61.16 ± (61.05 - 61.27) MB+0.0%✅⬆️
runtime.dotnet.threads.count31 ± (31 - 31)31 ± (31 - 31)+0.2%✅⬆️
.NET 8 - Baseline
process.internal_duration_ms202.24 ± (201.50 - 202.98) ms202.90 ± (202.35 - 203.44) ms+0.3%✅⬆️
process.time_to_main_ms74.43 ± (74.14 - 74.72) ms74.67 ± (74.37 - 74.97) ms+0.3%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.58 ± (11.56 - 11.59) MB11.67 ± (11.66 - 11.69) MB+0.8%✅⬆️
runtime.dotnet.threads.count19 ± (19 - 19)19 ± (19 - 19)+0.0%✅⬆️
.NET 8 - Bailout
process.internal_duration_ms201.64 ± (200.94 - 202.33) ms200.40 ± (199.87 - 200.93) ms-0.6%
process.time_to_main_ms75.42 ± (75.15 - 75.70) ms75.31 ± (75.04 - 75.57) ms-0.2%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.63 ± (11.62 - 11.65) MB11.72 ± (11.71 - 11.73) MB+0.7%✅⬆️
runtime.dotnet.threads.count20 ± (20 - 20)20 ± (20 - 20)-0.2%
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms522.00 ± (517.15 - 526.85) ms520.56 ± (517.42 - 523.71) ms-0.3%
process.time_to_main_ms503.86 ± (502.73 - 504.98) ms494.24 ± (493.13 - 495.35) ms-1.9%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed50.30 ± (50.24 - 50.36) MB50.33 ± (50.29 - 50.37) MB+0.1%✅⬆️
runtime.dotnet.threads.count30 ± (30 - 30)30 ± (30 - 30)-0.3%
Comparison explanation

Execution-time benchmarks measure the whole time it takes to execute a program, and are intended to measure the one-off costs. Cases where the execution time results for the PR are worse than latest master results are highlighted in **red**. The following thresholds were used for comparing the execution times:

  • Welch test with statistical test for significance of 5%
  • Only results indicating a difference greater than 5% and 5 ms are considered.

Note that these results are based on a single point-in-time result for each branch. For full results, see the dashboard.

Graphs show the p99 interval based on the mean and StdDev of the test run, as well as the mean value of the run (shown as a diamond below the graph).

Duration charts
FakeDbCommand (.NET Framework 4.8)
gantt
    title Execution time (ms) FakeDbCommand (.NET Framework 4.8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8549) - mean (73ms)  : 71, 75
    master - mean (73ms)  : 70, 76

    section Bailout
    This PR (8549) - mean (79ms)  : 76, 82
    master - mean (77ms)  : 76, 79

    section CallTarget+Inlining+NGEN
    This PR (8549) - mean (1,079ms)  : 1040, 1118
    master - mean (1,072ms)  : 1029, 1116

Loading
FakeDbCommand (.NET Core 3.1)
gantt
    title Execution time (ms) FakeDbCommand (.NET Core 3.1)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8549) - mean (114ms)  : 110, 117
    master - mean (116ms)  : 110, 123

    section Bailout
    This PR (8549) - mean (119ms)  : 113, 124
    master - mean (116ms)  : 113, 119

    section CallTarget+Inlining+NGEN
    This PR (8549) - mean (775ms)  : 750, 800
    master - mean (783ms)  : 755, 810

Loading
FakeDbCommand (.NET 6)
gantt
    title Execution time (ms) FakeDbCommand (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8549) - mean (101ms)  : 97, 105
    master - mean (103ms)  : 97, 109

    section Bailout
    This PR (8549) - mean (103ms)  : 98, 107
    master - mean (105ms)  : 99, 111

    section CallTarget+Inlining+NGEN
    This PR (8549) - mean (935ms)  : 900, 971
    master - mean (940ms)  : 898, 983

Loading
FakeDbCommand (.NET 8)
gantt
    title Execution time (ms) FakeDbCommand (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8549) - mean (104ms)  : 99, 109
    master - mean (101ms)  : 97, 104

    section Bailout
    This PR (8549) - mean (102ms)  : 99, 105
    master - mean (104ms)  : 99, 110

    section CallTarget+Inlining+NGEN
    This PR (8549) - mean (824ms)  : 788, 861
    master - mean (823ms)  : 788, 857

Loading
HttpMessageHandler (.NET Framework 4.8)
gantt
    title Execution time (ms) HttpMessageHandler (.NET Framework 4.8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8549) - mean (203ms)  : 196, 210
    master - mean (205ms)  : 199, 211

    section Bailout
    This PR (8549) - mean (207ms)  : 198, 215
    master - mean (212ms)  : 204, 220

    section CallTarget+Inlining+NGEN
    This PR (8549) - mean (1,214ms)  : 1154, 1273
    master - mean (1,218ms)  : 1156, 1280

Loading
HttpMessageHandler (.NET Core 3.1)
gantt
    title Execution time (ms) HttpMessageHandler (.NET Core 3.1)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8549) - mean (296ms)  : 284, 309
    master - mean (298ms)  : 287, 308

    section Bailout
    This PR (8549) - mean (297ms)  : 283, 310
    master - mean (299ms)  : 287, 311

    section CallTarget+Inlining+NGEN
    This PR (8549) - mean (982ms)  : 954, 1010
    master - mean (972ms)  : 943, 1000

Loading
HttpMessageHandler (.NET 6)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8549) - mean (290ms)  : 278, 301
    master - mean (289ms)  : 278, 300

    section Bailout
    This PR (8549) - mean (291ms)  : 279, 303
    master - mean (289ms)  : 278, 300

    section CallTarget+Inlining+NGEN
    This PR (8549) - mean (1,168ms)  : 1125, 1211
    master - mean (1,165ms)  : 1127, 1203

Loading
HttpMessageHandler (.NET 8)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8549) - mean (289ms)  : 277, 302
    master - mean (288ms)  : 271, 306

    section Bailout
    This PR (8549) - mean (287ms)  : 277, 297
    master - mean (288ms)  : 273, 302

    section CallTarget+Inlining+NGEN
    This PR (8549) - mean (1,047ms)  : 998, 1097
    master - mean (1,063ms)  : 983, 1143

Loading

}
}

span.SetTag("closed_reason", "garbage_collected");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The PR description mentions "is_incomplete=true". Do we set that tag?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah, we changed that based on feedback 🙂 Will update the description!

Comment thread tracer/test/Datadog.Trace.Tests/Activity/ReconciliationSweepTests.cs Outdated
Comment thread tracer/src/Datadog.Trace/Activity/Handlers/ActivityHandlerCommon.cs Outdated
// Activity is still alive. A non-zero Duration on an entry that's still in
// the dictionary means Stop() was called but our listener callback didn't
// handle it — close it using the real end time.
if (activityObject.TryDuckCast<IActivity6>(out var activity6))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should we add a close_reason for these cases too?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not sure, but I don't think so 🤔 We added the close_reason to the other one because it technically "shouldn't" have been closed, whereas these should be closed, and they're closed "correctly", unlike the other ones. I think adding close_reason here could be more confusing than anything else 🤔 Also, we currently have this code path today (we just run it much more often), and didn't see the need for the tag before. But I'm not sure, WDYT @zacharycmontoya?

@NachoEchevarria NachoEchevarria left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Very nice! Thanks!

@andrewlock
andrewlock enabled auto-merge (squash) May 6, 2026 10:15
@pr-commenter

pr-commenter Bot commented May 6, 2026

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-05-06 11:20:21

Comparing candidate commit aa87116 in PR branch andrew/activity-weak-reference with baseline commit a60cc68 in branch master.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 27 metrics, 0 unstable metrics, 59 known flaky benchmarks, 28 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 ----------------------------------'

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 [-8599.550op/s; -8146.130op/s] or [-10.197%; -9.659%]

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

  • 🟥 throughput [-8750.633op/s; -7444.183op/s] or [-8.897%; -7.569%]

scenario:Benchmarks.Trace.AgentWriterBenchmark.WriteAndFlushEnrichedTraces net472

  • 🟥 execution_time [+311.784ms; +317.688ms] or [+154.718%; +157.648%]
  • 🟥 throughput [-43.143op/s; -39.537op/s] or [-7.762%; -7.113%]

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

  • 🟥 execution_time [+376.638ms; +377.961ms] or [+297.567%; +298.612%]
  • 🟩 throughput [+97.785op/s; +100.094op/s] or [+12.893%; +13.197%]

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

  • 🟥 execution_time [+391.033ms; +392.978ms] or [+346.049%; +347.771%]

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

  • 🟥 allocated_mem [+1.308KB; +1.308KB] or [+27.529%; +27.541%]

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

  • 🟥 allocated_mem [+471 bytes; +472 bytes] or [+9.977%; +9.987%]
  • 🟩 execution_time [-15.904ms; -11.698ms] or [-7.428%; -5.463%]

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

  • 🟥 allocated_mem [+1.272KB; +1.272KB] or [+27.502%; +27.510%]

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

  • 🟥 allocated_mem [+1.307KB; +1.307KB] or [+105.746%; +105.759%]
  • 🟥 throughput [-269770.972op/s; -266263.836op/s] or [-27.545%; -27.187%]

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

  • 🟥 allocated_mem [+471 bytes; +472 bytes] or [+38.558%; +38.566%]
  • 🟩 execution_time [-27.344ms; -16.905ms] or [-12.194%; -7.539%]

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

  • 🟥 allocated_mem [+1.272KB; +1.272KB] or [+105.292%; +105.304%]
  • 🟥 throughput [-140507.183op/s; -124188.296op/s] or [-20.188%; -17.843%]

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

  • 🟩 throughput [+270385.758op/s; +296967.880op/s] or [+9.016%; +9.902%]

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

  • 🟩 execution_time [-18.605ms; -14.236ms] or [-8.576%; -6.562%]
  • 🟩 throughput [+157903.849op/s; +212318.999op/s] or [+6.268%; +8.428%]

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

  • 🟥 execution_time [+298.326ms; +299.516ms] or [+149.063%; +149.658%]

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

  • 🟥 execution_time [+299.606ms; +302.745ms] or [+151.092%; +152.675%]

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

  • 🟥 execution_time [+300.313ms; +302.701ms] or [+151.274%; +152.477%]

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

  • 🟥 execution_time [+296.281ms; +297.133ms] or [+145.521%; +145.940%]

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

  • 🟥 execution_time [+299.914ms; +301.529ms] or [+146.617%; +147.406%]

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

  • 🟥 execution_time [+300.165ms; +301.295ms] or [+150.022%; +150.587%]

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

  • 🟥 execution_time [+27.621µs; +54.614µs] or [+6.335%; +12.526%]
  • 🟥 throughput [-263.466op/s; -142.218op/s] or [-11.454%; -6.183%]

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

  • 🟥 execution_time [+25.765µs; +49.370µs] or [+8.226%; +15.761%]
  • 🟥 throughput [-455.978op/s; -257.369op/s] or [-14.214%; -8.023%]

scenario:Benchmarks.Trace.AspNetCoreBenchmark.SendRequest net472

  • 🟥 execution_time [+299.832ms; +300.487ms] or [+149.647%; +149.974%]

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

  • 🟥 execution_time [+411.071ms; +415.877ms] or [+446.646%; +451.868%]
  • 🟩 throughput [+1155.837op/s; +1291.777op/s] or [+9.498%; +10.615%]

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

  • unstable execution_time [+219.779ms; +260.862ms] or [+166.876%; +198.070%]
  • 🟩 throughput [+647.807op/s; +857.784op/s] or [+6.271%; +8.304%]

scenario:Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark.WriteAndFlushEnrichedTraces net472

  • unstable execution_time [+336.090ms; +369.759ms] or [+154.531%; +170.011%]
  • 🟥 throughput [-505.695op/s; -473.892op/s] or [-45.821%; -42.939%]

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

  • unstable execution_time [+211.364ms; +344.565ms] or [+90.074%; +146.839%]
  • 🟥 throughput [-666.973op/s; -583.543op/s] or [-44.487%; -38.922%]

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

  • 🟥 execution_time [+337.302ms; +346.643ms] or [+201.746%; +207.333%]
  • 🟥 throughput [-397.282op/s; -361.822op/s] or [-27.662%; -25.193%]

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

  • 🟥 execution_time [+161.104µs; +252.184µs] or [+11.067%; +17.324%]
  • 🟥 throughput [-90.431op/s; -56.659op/s] or [-13.163%; -8.247%]

scenario:Benchmarks.Trace.ElasticsearchBenchmark.CallElasticsearch net472

  • 🟥 execution_time [+303.534ms; +305.201ms] or [+152.854%; +153.693%]

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

  • 🟥 execution_time [+301.818ms; +303.279ms] or [+151.242%; +151.974%]

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

  • 🟥 execution_time [+301.907ms; +305.046ms] or [+151.665%; +153.242%]

scenario:Benchmarks.Trace.ElasticsearchBenchmark.CallElasticsearchAsync net472

  • 🟥 execution_time [+302.617ms; +304.030ms] or [+151.964%; +152.674%]

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

  • 🟥 execution_time [+298.351ms; +300.032ms] or [+147.521%; +148.352%]

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

  • 🟥 execution_time [+305.749ms; +309.130ms] or [+154.967%; +156.681%]

scenario:Benchmarks.Trace.GraphQLBenchmark.ExecuteAsync net472

  • 🟥 execution_time [+301.237ms; +302.773ms] or [+151.194%; +151.965%]

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

  • 🟥 execution_time [+300.539ms; +303.049ms] or [+149.791%; +151.042%]
  • 🟩 throughput [+57139.464op/s; +62046.164op/s] or [+11.346%; +12.320%]

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

  • 🟥 execution_time [+300.731ms; +303.501ms] or [+149.611%; +150.989%]

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

  • 🟩 execution_time [-15.962ms; -12.264ms] or [-7.423%; -5.703%]
  • 🟩 throughput [+19000.663op/s; +25575.162op/s] or [+5.212%; +7.016%]

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

  • unstable execution_time [+10.206µs; +51.813µs] or [+2.521%; +12.798%]

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

  • 🟩 allocated_mem [-20.220KB; -20.200KB] or [-7.376%; -7.368%]
  • unstable execution_time [-21.031µs; +58.532µs] or [-4.157%; +11.568%]
  • unstable throughput [-90.626op/s; +135.131op/s] or [-4.522%; +6.743%]

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

  • 🟥 allocated_mem [+8.190KB; +8.196KB] or [+16.663%; +16.674%]

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

  • 🟥 execution_time [+6.502µs; +10.717µs] or [+15.368%; +25.332%]
  • 🟥 throughput [-4776.298op/s; -2991.389op/s] or [-20.107%; -12.593%]

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

  • unstable execution_time [-13.847µs; -6.781µs] or [-21.483%; -10.521%]
  • 🟩 throughput [+1686.032op/s; +3169.121op/s] or [+10.344%; +19.444%]

scenario:Benchmarks.Trace.Log4netBenchmark.EnrichedLog net472

  • 🟥 execution_time [+303.194ms; +304.227ms] or [+153.251%; +153.773%]

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

  • 🟥 execution_time [+302.994ms; +305.182ms] or [+154.223%; +155.337%]

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

  • 🟥 execution_time [+298.263ms; +300.867ms] or [+149.317%; +150.621%]

scenario:Benchmarks.Trace.SerilogBenchmark.EnrichedLog net472

  • 🟥 execution_time [+298.599ms; +300.185ms] or [+148.825%; +149.615%]

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

  • 🟥 execution_time [+301.522ms; +302.790ms] or [+151.410%; +152.047%]

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

  • 🟥 execution_time [+303.845ms; +306.384ms] or [+154.091%; +155.378%]

scenario:Benchmarks.Trace.SingleSpanAspNetCoreBenchmark.SingleSpanAspNetCore net472

  • 🟥 execution_time [+300.231ms; +300.925ms] or [+149.757%; +150.103%]
  • 🟩 throughput [+61199735.273op/s; +61466029.121op/s] or [+44.569%; +44.763%]

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

  • 🟥 execution_time [+424.485ms; +427.367ms] or [+527.922%; +531.507%]
  • 🟩 throughput [+1100.165op/s; +1268.973op/s] or [+8.505%; +9.810%]

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

  • 🟥 execution_time [+299.260ms; +300.228ms] or [+149.264%; +149.747%]

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

  • 🟩 throughput [+94020.178op/s; +101997.760op/s] or [+8.778%; +9.523%]

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

  • 🟩 throughput [+47509.027op/s; +66591.739op/s] or [+5.499%; +7.708%]

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

  • 🟩 throughput [+95100.859op/s; +124578.202op/s] or [+7.361%; +9.643%]

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

  • 🟩 throughput [+84883.924op/s; +92239.889op/s] or [+8.430%; +9.161%]

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

  • 🟩 throughput [+48921.669op/s; +53408.630op/s] or [+8.883%; +9.698%]

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

  • 🟩 throughput [+23033.623op/s; +32566.713op/s] or [+5.156%; +7.289%]

scenario:Benchmarks.Trace.TraceAnnotationsBenchmark.RunOnMethodBegin net6.0

  • 🟩 throughput [+73100.877op/s; +97129.146op/s] or [+8.167%; +10.852%]

Known flaky benchmarks without significant changes:

  • scenario:Benchmarks.Trace.ActivityBenchmark.StartStopWithChild net6.0
  • scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorMoreComplexBody net472
  • scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorMoreComplexBody net6.0
  • scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorMoreComplexBody netcoreapp3.1
  • scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorSimpleBody net472
  • scenario:Benchmarks.Trace.Asm.AppSecWafBenchmark.RunWafRealisticBenchmark net472
  • scenario:Benchmarks.Trace.Asm.AppSecWafBenchmark.RunWafRealisticBenchmark netcoreapp3.1
  • scenario:Benchmarks.Trace.Asm.AppSecWafBenchmark.RunWafRealisticBenchmarkWithAttack net472
  • scenario:Benchmarks.Trace.Asm.AppSecWafBenchmark.RunWafRealisticBenchmarkWithAttack netcoreapp3.1
  • scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSlice net472
  • scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSlice netcoreapp3.1
  • scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSliceWithPool net472
  • scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSliceWithPool net6.0
  • scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSliceWithPool netcoreapp3.1
  • scenario:Benchmarks.Trace.CharSliceBenchmark.OriginalCharSlice net472
  • scenario:Benchmarks.Trace.CharSliceBenchmark.OriginalCharSlice net6.0
  • 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.StringConcatAspectBenchmark netcoreapp3.1
  • 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.StartFinishSpan net472
  • scenario:Benchmarks.Trace.SpanBenchmark.StartFinishTwoScopes net472
  • scenario:Benchmarks.Trace.TraceAnnotationsBenchmark.RunOnMethodBegin net472
  • scenario:Benchmarks.Trace.TraceAnnotationsBenchmark.RunOnMethodBegin netcoreapp3.1

@andrewlock
andrewlock merged commit 6ee0df9 into master May 6, 2026
139 checks passed
@andrewlock
andrewlock deleted the andrew/activity-weak-reference branch May 6, 2026 11:51
@github-actions github-actions Bot added this to the vNext-v3 milestone May 6, 2026
andrewlock added a commit that referenced this pull request May 7, 2026
…OTel (#8576)

## Summary of changes

Don't auto-close closed-`Activity` related `Scope` which have been
closed for < 1 minute

## Reason for change

In #8549, we moved the "CloseActivitySlow" reconciliation step when an
`Activity` is closed but we somehow "missed" the `StopActivity()` call
into a background job. We don't have a known mechanism for how that
would happen today, and so this seemed reasonable.

There's a non-ideal aspect to this in terms of `AsyncLocal<T>` and
execution context flow, in that we're now closing the `Scope` from a
background task, that's _off_ the original `async` context that created
it. This will theoretically mean that the `ActiveScope` flow doesn't
work correctly (needs confirming, but it's plausible).

This is _mostly_ "OK" given we don't have a known mechanism where we
would hit this path _however_, we also introduced a race condition,
where a given `Activity` is closed, and is in the _process_ of calling
`StopActivity`, when our background loop executes. In that case we
preemptively close it from the background thread.

## Implementation details

As a workaround, this PR simply adds a 1 minute cutoff before we start
closing the associated `Scope`, on the basis that this _should_ be
plenty of time for our inline handler to run if it's going to. This
isn't ideal, as it's somewhat of a hacky workaround, but then, so is the
CloseActivitySlow process anyway 😅

The other option is that we move the reconciliation _back_ into the hot
path, but that doesn't seem worth the trade off to me, especially as we
consider this to be an unknown edge case anyway, and it's more there for
safety than anything else.

## Test coverage

Added a test to confirm that a "freshly" closed `Activity` is ignored,
and it's only an "old" closed `Activity` that gets swept

## Other details

Related to https://datadoghq.atlassian.net/browse/APMS-19316.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI Generated Largely based on code generated by an AI or LLM. This label is the same across all dd-trace-* repos area:opentelemetry OpenTelemetry support type:performance Performance, speed, latency, resource usage (CPU, memory) type:reliability

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants