You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Today, TestContext cancellation token (source) can be reused for multiple TestInitialize/TestCleanup calls (on inheritance hierarchy of a test class). This is even being extended in #6132 to the new global init/cleanup.
If, during one call, we got cancelled already, what we do today is that we run the remaining steps with the same, already-cancelled token. What is the point of invoking something with an already-cancelled token? We should either:
Create a new token for each call.
Avoid executing subsequent initializes if we are already cancelled.
I'm marking as a bug as it doesn't make sense to call a test initialize with an already-cancelled token. Because in an ideal cooperative cancellation world, we would expect the call to immediately throw operation canceled exception.
Investigated this against both released MSTest 4.5.2 / MTP 2.5.2 and source-built main at 92e83ed.
The original inherited-initializer claim is not reproduced: FixtureMethodRunner already checks for cancellation before invoking user code, and initialization stops on the first failure. That guard predates this issue (#4444).
There is, however, a reproducible cleanup cancellation isolation problem:
An independent GlobalTestCleanup that would release the resource never enters, even if it does not use a cancellation token.
Local cleanup behavior
Global cleanup entered?
Resource released?
Completes normally
Yes
Yes
Throws an unrelated InvalidOperationException
Yes
Yes
Times out cooperatively
No
No
Cancels the context source and returns normally
No
No
The unrelated-exception control distinguishes this from the existing inherited local-cleanup fail-fast policy. Test cleanup gets a fresh source after canceled initialization/test execution, but cancellation during local cleanup poisons the source subsequently passed to global cleanup. Entering the surrounding finally block does not help: the global fixture's entry guard rejects the canceled source.
The same ten isolated packaged-consumer scenarios were executed on both versions: 20 executed, zero skipped, with the expected child exits, exact lifecycle traces, resource states and TRX identities/outcomes checked. Actual loaded DLLs and resolved package payloads were also verified. This is reproduction evidence, not a claim that a fix has already passed.
I suggest retaining this issue and narrowing it to local cleanup cancellation suppressing global teardown. A bounded fix should preserve initialization short-circuiting, local/multiple-global-cleanup fail-fast behavior, the original timeout failure and whole-test/run cancellation semantics. No public API change is needed; indiscriminately replacing the token before every lifecycle call is not necessary.
Implementation and servicing result
The narrow fix is implemented and validated as a non-breaking bug fix, with a matching rel/4.5 backport. It adds no public or internal APIs and changes neither healthy-path token identity nor default timeouts. It preserves the original local failure, keeps explicit cancellation from becoming a false pass, retains active whole-test/run cancellation budgets, and leaves inherited-local and multiple-global-cleanup fail-fast rules intact.
Branch context
Local commit
Lifecycle tests
Packed MTP consumer cases
Main
e0ce525219a1e10bcce8d2b3ab048b03ac26b495
139 passed, including 13 new regressions
8 passed on net10, including executed source-generated metadata
rel/4.5
d438266e9e4efb7a501a7bf4035047f1c29046b8
121 passed, including the same 13 regressions
8 passed on net8, including executed source-generated metadata
Test-value evidence: On each branch, the same retained regression cases produced 9 intended behavioral failures out of 13 unit cases without the production fix, then passed 13/13 with it. The same packed-consumer selection produced 6 resource-release assertion failures out of 8 cases with the original adapter, then passed 8/8 with the rebuilt fixed adapter. No skipped tests or setup failures were counted as red proof. All 16 child adapter loads per packaged phase were verified against that branch's actual nupkg payload.
The release work uses MSTest 4.5.3-dev / MTP 2.5.3-dev; it does not mean an updated package has already been published. Both commits are currently local only: not pushed, opened as pull requests, or merged. No CI verification is claimed.
The separate cases of a last initializer canceling and returning, and global initialization failing before test-class activation, remain outside this fix.
Fixed for rel/4.5 by #12220, merged on November 5, 2026.
That change addresses the cleanup-cancellation isolation defect documented above: cancellation during local TestCleanup no longer suppresses independent GlobalTestCleanup solely because they reuse a canceled context source. The original local cleanup failure is retained.
Closing this issue as completed for that defect. The corresponding main work is tracked in #12225, which is still open; this closure does not imply that PR has merged or that a servicing package has been published.
Activity
Youssef1313 commentedon Jul 24, 2025
I'm marking as a bug as it doesn't make sense to call a test initialize with an already-cancelled token. Because in an ideal cooperative cancellation world, we would expect the call to immediately throw operation canceled exception.
Evangelink commentedon Nov 5, 2026
Investigated this against both released MSTest 4.5.2 / MTP 2.5.2 and source-built main at 92e83ed.
The original inherited-initializer claim is not reproduced:
FixtureMethodRunneralready checks for cancellation before invoking user code, and initialization stops on the first failure. That guard predates this issue (#4444).There is, however, a reproducible cleanup cancellation isolation problem:
A
GlobalTestInitializecreates a resource.The test completes normally.
A local
TestCleanuptimes out cooperatively:An independent
GlobalTestCleanupthat would release the resource never enters, even if it does not use a cancellation token.InvalidOperationExceptionThe unrelated-exception control distinguishes this from the existing inherited local-cleanup fail-fast policy. Test cleanup gets a fresh source after canceled initialization/test execution, but cancellation during local cleanup poisons the source subsequently passed to global cleanup. Entering the surrounding
finallyblock does not help: the global fixture's entry guard rejects the canceled source.The same ten isolated packaged-consumer scenarios were executed on both versions: 20 executed, zero skipped, with the expected child exits, exact lifecycle traces, resource states and TRX identities/outcomes checked. Actual loaded DLLs and resolved package payloads were also verified. This is reproduction evidence, not a claim that a fix has already passed.
I suggest retaining this issue and narrowing it to local cleanup cancellation suppressing global teardown. A bounded fix should preserve initialization short-circuiting, local/multiple-global-cleanup fail-fast behavior, the original timeout failure and whole-test/run cancellation semantics. No public API change is needed; indiscriminately replacing the token before every lifecycle call is not necessary.
Implementation and servicing result
The narrow fix is implemented and validated as a non-breaking bug fix, with a matching
rel/4.5backport. It adds no public or internal APIs and changes neither healthy-path token identity nor default timeouts. It preserves the original local failure, keeps explicit cancellation from becoming a false pass, retains active whole-test/run cancellation budgets, and leaves inherited-local and multiple-global-cleanup fail-fast rules intact.e0ce525219a1e10bcce8d2b3ab048b03ac26b495rel/4.5d438266e9e4efb7a501a7bf4035047f1c29046b8Test-value evidence: On each branch, the same retained regression cases produced 9 intended behavioral failures out of 13 unit cases without the production fix, then passed 13/13 with it. The same packed-consumer selection produced 6 resource-release assertion failures out of 8 cases with the original adapter, then passed 8/8 with the rebuilt fixed adapter. No skipped tests or setup failures were counted as red proof. All 16 child adapter loads per packaged phase were verified against that branch's actual nupkg payload.
The release work uses MSTest
4.5.3-dev/ MTP2.5.3-dev; it does not mean an updated package has already been published. Both commits are currently local only: not pushed, opened as pull requests, or merged. No CI verification is claimed.The separate cases of a last initializer canceling and returning, and global initialization failing before test-class activation, remain outside this fix.
Evangelink commentedon Nov 6, 2026
Fixed for
rel/4.5by #12220, merged on November 5, 2026.That change addresses the cleanup-cancellation isolation defect documented above: cancellation during local
TestCleanupno longer suppresses independentGlobalTestCleanupsolely because they reuse a canceled context source. The original local cleanup failure is retained.Closing this issue as completed for that defect. The corresponding
mainwork is tracked in #12225, which is still open; this closure does not imply that PR has merged or that a servicing package has been published.