Skip to content

Isolate shared disposal span capture by parent activity - #7492

Merged
thomhurst merged 3 commits into
mainfrom
issue-7435-disposal-span-capture
Nov 3, 2026
Merged

thomhurst merged 3 commits into
mainfrom
issue-7435-disposal-span-capture

Conversation

@thomhurst

@thomhurst thomhurst commented Nov 3, 2026 •

Copy link
Copy Markdown
Owner

Shared-object disposal span tests intermittently capture duplicate or missing spans in concurrent Ubuntu net8.0 runs, including the Ubuntu failure blocking #7471. Capture spans by each test context’s unique class and assembly parent IDs, so global listener callbacks do not depend on ambient AsyncLocal state. Keep the existing single-span, parent, scope and error assertions.

Add a regression with overlapping listeners and disposal on a task without execution-context flow. It captures exactly one span for each independent parent.

Closes #7435

Validation:

  • Targeted SharedObjectDisposeSpanTests, Release net10.0 on macOS arm64: 5 passed.
  • New regression with the original AsyncLocal helper: 1 failed (no captured activity); restored fix: all 5 passed.
  • git diff --check passed.
  • Only .NET 10 runtime is installed locally; Ubuntu net8.0 concurrent-suite validation remains for CI.

Summary by CodeRabbit

  • Tests
    • Expanded coverage for disposal activity tracking, including overlapping scopes and disposal performed without execution context flow.
    • Updated disposal-span tests to verify activity capture against the relevant test context.
    • Added checks confirming that activities are captured regardless of parent and that overlapping listeners capture only activities associated with their own test contexts.
    • Verified that per-test objects continue to emit no disposal span. These checks improve confidence in disposal activity tracking across different test scenarios.

Review follow-up (ae3979c): use an explicit unrelated parent in the parent-independent capture regression so HTML reporting can be disabled. Validation with TUNIT_DISABLE_HTML_REPORTER=1: isolated regression 1 passed; all 6 SharedObjectDisposeSpanTests passed in Release net10.0 on macOS arm64; git diff --check passed.

@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Walkthrough

The disposal-span tests now pass each isolated TestContext to StoppedActivityScope. The scope captures dispose activities by the context’s class or assembly parent span ID, or by an optional operation name. Tests cover parent-independent capture and overlapping listeners when execution-context flow is suppressed.

Changes: Disposal Span Capture

Layer / File(s) Summary
Context-based activity filtering
tests/TUnit.UnitTests/SharedObjectDisposeSpanTests.cs
StoppedActivityScope uses class and assembly span IDs from TestContext to capture matching dispose activities. It can also capture activities with a specified operation name, and no longer uses or clears an AsyncLocal scope.
Disposal-span capture tests
tests/TUnit.UnitTests/SharedObjectDisposeSpanTests.cs
Existing tests pass TestContext to each scope. New tests cover parent-independent capture, per-test disposal, and overlapping scopes with execution-context flow suppressed.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Other · Severity of issue fixed: Medium

Possibly related issues

  • thomhurst/TUnit#7287 — The issue concerns shared data-source disposal spans and their session, assembly, or class parents. These tests cover shared-object spans parented to class or assembly activities.

Possibly related PRs

  • thomhurst/TUnit#7396 — This PR introduced the shared-object disposal spans and tests that this change updates.
Merge Risk: Minimal · up to ae397

The disposal-span tests now isolate their listeners correctly, with no remaining actionable merge-blocking risk identified.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: isolating shared disposal span capture by parent activity.
Linked Issues check Passed Issue [#7435] requires deterministic capture of one disposal span per test without weakening assertions. StoppedActivityScope now uses each test context's explicit class or assembly parent ID instea…
Out of Scope Changes check Passed The pull request changes only tests/TUnit.UnitTests/SharedObjectDisposeSpanTests.cs. The helper update, fixture, retained assertions, and regression tests directly support [#7435]. No unrelated prod…
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Full details: Linked Issues check

Explanation

Issue [#7435] requires deterministic capture of one disposal span per test without weakening assertions. StoppedActivityScope now uses each test context's explicit class or assembly parent ID instead of ambient AsyncLocal state. The existing parent, scope, error, and no-span assertions remain. The new overlapping-listener regression verifies isolation when ExecutionContext flow is suppressed.

Full details: Out of Scope Changes check

Explanation

The pull request changes only tests/TUnit.UnitTests/SharedObjectDisposeSpanTests.cs. The helper update, fixture, retained assertions, and regression tests directly support [#7435]. No unrelated production code, configuration, or unrelated test changes are shown.

Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 1 files.

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks each span by name,
Then checks its parent all the same.
Two listeners watch, each on its own,
While contexts do not flow or roam.
The disposal tests now capture clear,
And hop along without a sneer.

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.
Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3d06e88a-34bb-4d72-8f02-700744ca6573
Commits

Reviewing files that changed from the base of the PR and between 0cdc0c8 and ae3979c.

Files selected for processing: 1
  • tests/TUnit.UnitTests/SharedObjectDisposeSpanTests.cs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 2 remain after this review.

@thomhurst

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T11:17:49.429040Z ae3979c New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Review

This is a test-only change. It replaces the AsyncLocal-based capture in StoppedActivityScope with a filter on each context's own class and assembly span IDs.

Why the fix works

  • CreateContext() gives each test context fresh, uniquely-IDed stopped activities.
  • Because of that, filtering on ParentSpanId is deterministic. It no longer matters which execution context the global ActivityStopped callback runs in, so parallel tests can't leak spans in and disposal on a non-flowing task can't lose them.
  • The new regression test is well designed. It uses overlapping listeners, and it disposes on a Task.Run created under ExecutionContext.SuppressFlow(). It asserts exactly one span per listener and the correct parent for each. The PR description says it failed with the old helper, which is what a regression test should do.

Minor, non-blocking

  • The class and assembly span IDs are captured when the scope is constructed, and Activity! assumes CreateContext always sets them. That holds today. A short comment on the helper would stop someone from passing a context without activities later.
  • The scope accepts a span from either the class or the assembly parent, so a PerClass test could in principle pass on an assembly-parented span. The per-test ParentSpanId assertions already cover this, so I wouldn't change it.

I haven't run the tests. The PR notes Ubuntu net8.0 concurrent validation is left to CI, so check that result before merging.

LGTM.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 72494fbefe

ℹ️ 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".

@greptile-apps

greptile-apps Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge; the previous test failure is fixed and no new issues were found.

Summary

The PR isolates disposal-span capture by each test’s parent activities instead of relying on the current async flow.

  • Disposal span tests match each capture to its test context.

Reviews (3) · Last reviewed commit: "Remove reporter dependency from span reg..." · Reviewed by Greptile

@thomhurst

Copy link
Copy Markdown
Owner Author

Checked the blocker of #7471 under this PR’s lock. Current head 72494fb has a positive Claude verdict and no inline comments or review threads. The two Claude suggestions are explicitly nonblocking: CreateContext always initializes the activities, and existing parent assertions cover the accepted parent IDs. No fix is needed for promotion.

Codex review is running for this exact head; its request at 10:54:42 UTC is still within the 15-minute window and has only an eyes reaction, so approval cannot yet be inferred or requested again. CodeQL run 41623113033 is also running. Keep this PR draft until Codex completes positively, then promote, read the new ready-for-review Claude verdict, and arm auto-merge. After this merges, rebase #7471 onto main for fresh CI.

Resolve routes checked: this is the existing closing PR for #7435, not an already-shipped or superseded change; there is no finished parent or stale acceptance scope to close, and no external or human decision to park behind. The remaining gates complete asynchronously; no on-hold label or unchanged-head rerun is appropriate.

@thomhurst

thomhurst commented Nov 3, 2026 •

Copy link
Copy Markdown
Owner Author

Rechecked from assigned #7471 under the #7492 lock. Current head ae3979c has positive Claude and Codex verdicts; all review surfaces were read and all threads are resolved. All .NET jobs and Ubuntu/Windows AOT jobs passed. The macOS engine job has now passed too. The sole remaining job is AOT (macos-latest), actively executing Run Pipeline in run 41626176380. Assert-PrGreen.ps1 -ForAutoMerge passes; squash auto-merge remains armed.

Resolve routes checked: main still has a different disposal-span test implementation; #7492 remains the open closing PR for #7435 and its body has Closes #7435. The issue remains open with no native children or prerequisite dependencies, and its acceptance criteria match this fix. Timeline links show no replacement. There is no finished parent, stale remaining scope, outstanding review fix, failed current-head job, or reversible decision to implement. No human, upstream, credentials, or ownership blocker applies. Rebasing this reviewed fix or restarting passed checks would not resolve the actively executing hosted job. No on-hold label is appropriate.

Remaining wait: run 41626176380. After #7492 merges, rebase #7471 onto fresh main and push for fresh CI.

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Review

The change looks correct and I found no blocking issues.

  • Root cause fixed: the old AsyncLocal scope depended on ambient execution-context flow in a process-wide ActivityListener. That explains the flaky duplicate and missing spans under parallel runs. Filtering on each test context's unique class and assembly span IDs removes that dependency.
  • Isolation holds: CreateContext() builds fresh W3C activities for each context, so the span IDs are unique per test. Overlapping listeners won't capture each other's disposal spans.
  • Regression test: it uses two overlapping listeners and ExecutionContext.SuppressFlow(). This reproduces the original failure mode, and the PR description says it failed against the old helper.
  • Existing assertions: the single-span, parent, scope and error checks are kept.

Minor, non-blocking: Per_Test_Object_Disposal_Emits_No_Span now passes if a span is emitted under some other parent. The filter would silently drop it. It still catches a span emitted under the class or assembly parent, which is the realistic regression, so this is acceptable.

The Ubuntu net8.0 concurrent run in CI is the remaining confirmation.

@thomhurst
thomhurst enabled auto-merge (squash) November 3, 2026 10:58

@coderabbitai coderabbitai 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.

  • Fix comments: Fix CodeRabbit comments on this PR

@thomhurst
thomhurst disabled auto-merge November 3, 2026 11:03
@thomhurst

Copy link
Copy Markdown
Owner Author

Pushed review fix 0cdc0c8 and resolved the CodeRabbit thread with the commit and validation evidence. All 6 targeted SharedObjectDisposeSpanTests pass in Release net10.0 on macOS arm64. New head is confirmed on GitHub. Auto-merge was disabled before pushing; fresh Claude review and CI are queued. Recheck all review surfaces and current-head verdicts before rearming via Enable-AutoMerge.ps1. After this merges, #7471 can rebase onto main for fresh CI.

@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: 0cdc0c8593

ℹ️ 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".

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Review

Test-only change, and it looks sound. Keying capture on each context's unique class and assembly span IDs, instead of ambient AsyncLocal state, fits the cause of the flakiness: ActivityStopped is a global callback and doesn't run in the creator's async flow. The new regression test (overlapping listeners, disposal under SuppressFlow) covers that case directly. The PerTestSpanFixture split and the parent-independent name filter keep Per_Test_Object_Disposal_Emits_No_Span meaningful. They stop it from matching other tests' spans, and an unexpected root span would still be caught.

Minor, non-blocking notes:

  • The filter relies on CreateContext giving each test fresh class and assembly activities. The comment says so, and the ! dereferences will fail loudly if that changes. A debug assertion or small helper would make the contract explicit.
  • Per_Test_Listener_Captures_Unexpected_Spans_Regardless_Of_Parent uses TestContext.Current!.Activity!, so it depends on that ambient activity existing. It's fine as long as that holds under all test configurations.
  • The net8.0 Ubuntu concurrent run is still unverified locally, so CI is the real check.

No blocking issues. LGTM.

@coderabbitai coderabbitai 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.

  • Fix comments: Fix CodeRabbit comments on this PR

@thomhurst

Copy link
Copy Markdown
Owner Author

Fixed in ae3979c. The regression now creates an explicit unrelated parent through CreateStoppedActivity, removing its dependency on the runner activity. With TUNIT_DISABLE_HTML_REPORTER=1, the isolated regression passes (1/1) and all SharedObjectDisposeSpanTests pass (6/6), Release net10.0 on macOS arm64. git diff --check passes.

All three duplicate findings are fixed and their threads resolved. Fresh current-head reviews and CI must complete before rearming auto-merge. After merge, rebase #7471 onto main for fresh CI.

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Review

The change is sound. It fixes the flakiness at its cause. The old StoppedActivityScope filtered on an AsyncLocal, and ActivityStopped callbacks don't reliably run on the flow that created the listener. Matching on each test context's own class and assembly span IDs removes that dependency.

What I checked

  • The overlapping-listener regression uses ExecutionContext.SuppressFlow(). That reproduces the failure the PR describes, and it asserts that each listener sees exactly one span with the correct parent.
  • Per_Test_Object_Disposal_Emits_No_Span now uses a distinct PerTestSpanFixture type name, so the empty-capture assertion can't be satisfied by a span from another test's SpanFixture. I think this was a real latent hole, so the change is good.
  • The parentIndependentOperationName escape hatch is used only by tests that need it. The unique GUID in the operation name keeps it from matching other tests' spans.

Minor, non-blocking

  • Both Activity! dereferences in the constructor depend on CreateContext always setting activities. The comment says so, but a clear failure message would be more helpful than a NullReferenceException if that ever changes.
  • The class and assembly span IDs are captured once at construction. If any test were to replace those activities afterwards, filtering would silently miss them. No current test does this.

No blocking issues. The Ubuntu net8.0 concurrent run in CI is the remaining validation, as the PR notes.

@thomhurst
thomhurst enabled auto-merge (squash) November 3, 2026 11:18
@thomhurst
thomhurst merged commit 8112ade into main Nov 3, 2026
37 checks passed
@thomhurst
thomhurst deleted the issue-7435-disposal-span-capture branch November 3, 2026 14:37

This branch was previously deployed

1 inactive deployment
Pull Requests — ae3979ca Deployed Nov 3, 2026 by thomhurst via modularpipeline (ubuntu-latest) #20920
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shared disposal span tests capture duplicate or missing activities on Ubuntu net8.0

1 participant