-
Notifications
You must be signed in to change notification settings - Fork 23
fix: resolve implicit-sender leak under xUnit v3 parallel execution #735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Aaronontheweb
merged 7 commits into
akkadotnet:dev
from
Aaronontheweb:xunit3-testkit-compat-cleanup
May 17, 2026
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8de3911
fix: resolve implicit-sender leak under xUnit v3 parallel execution (…
Aaronontheweb 2f4f655
fix: guard After() cleanup when Before() returned early
Aaronontheweb cfba21d
fix: remove DisableTestParallelization so parallel tests actually run
Aaronontheweb 41e8049
fix: use AsyncLocal instead of ThreadStatic in BeforeAfterTestAttribute
Aaronontheweb 7b9bffd
fix: use upstream AkkaCleanAmbientContextAttribute and bump to Akka.N…
Aaronontheweb 06eec0a
Merge remote-tracking branch 'akkadotnet/dev' into xunit3-testkit-com…
Aaronontheweb ba5e786
fix: update API approval baseline to include AkkaCleanAmbientContext …
Aaronontheweb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
src/Akka.Hosting.TestKit.Tests/ParallelAmbientContextSpec.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="ParallelAmbientContextSpec.cs" company="Akka.NET Project"> | ||
| // Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Akka.Actor; | ||
| using Akka.Actor.Internal; | ||
| using Akka.TestKit; | ||
| using Xunit; | ||
|
|
||
| namespace Akka.Hosting.TestKit.Tests; | ||
|
|
||
| public abstract class ParallelAmbientContextSpecBase : TestKit | ||
| { | ||
| protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IServiceProvider provider) | ||
| { | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Implicit_sender_should_resolve_to_own_TestActor() | ||
| { | ||
| TestActor.Tell("ping"); | ||
| await ExpectMsgAsync<string>( | ||
| "ping", | ||
| TimeSpan.FromSeconds(5), | ||
| cancellationToken: TestContext.Current.CancellationToken); | ||
| Assert.Equal(TestActor, LastSender); | ||
|
|
||
| await Task.Yield(); | ||
| TestActor.Tell("ping-after-yield"); | ||
| await ExpectMsgAsync<string>( | ||
| "ping-after-yield", | ||
| TimeSpan.FromSeconds(5), | ||
| cancellationToken: TestContext.Current.CancellationToken); | ||
| Assert.Equal(TestActor, LastSender); | ||
| } | ||
| } | ||
|
|
||
| public class ParallelAmbientContextSpec01 : ParallelAmbientContextSpecBase { } | ||
| public class ParallelAmbientContextSpec02 : ParallelAmbientContextSpecBase { } | ||
| public class ParallelAmbientContextSpec03 : ParallelAmbientContextSpecBase { } | ||
| public class ParallelAmbientContextSpec04 : ParallelAmbientContextSpecBase { } | ||
| public class ParallelAmbientContextSpec05 : ParallelAmbientContextSpecBase { } | ||
| public class ParallelAmbientContextSpec06 : ParallelAmbientContextSpecBase { } | ||
| public class ParallelAmbientContextSpec07 : ParallelAmbientContextSpecBase { } | ||
| public class ParallelAmbientContextSpec08 : ParallelAmbientContextSpecBase { } | ||
| public class ParallelAmbientContextSpec09 : ParallelAmbientContextSpecBase { } | ||
| public class ParallelAmbientContextSpec10 : ParallelAmbientContextSpecBase { } | ||
| public class ParallelAmbientContextSpec11 : ParallelAmbientContextSpecBase { } | ||
| public class ParallelAmbientContextSpec12 : ParallelAmbientContextSpecBase { } | ||
| public class ParallelAmbientContextSpec13 : ParallelAmbientContextSpecBase { } | ||
| public class ParallelAmbientContextSpec14 : ParallelAmbientContextSpecBase { } | ||
| public class ParallelAmbientContextSpec15 : ParallelAmbientContextSpecBase { } | ||
| public class ParallelAmbientContextSpec16 : ParallelAmbientContextSpecBase { } | ||
|
|
||
| public abstract class ParallelNoImplicitSenderSpecBase : TestKit, INoImplicitSender | ||
| { | ||
| protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IServiceProvider provider) | ||
| { | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Current_should_be_null_both_pre_and_post_await() | ||
| { | ||
| Assert.Null(InternalCurrentActorCellKeeper.Current); | ||
| await Task.Yield(); | ||
| Assert.Null(InternalCurrentActorCellKeeper.Current); | ||
| await Task.Yield(); | ||
| Assert.Null(InternalCurrentActorCellKeeper.Current); | ||
| } | ||
| } | ||
|
|
||
| public class ParallelNoImplicitSenderSpec01 : ParallelNoImplicitSenderSpecBase { } | ||
| public class ParallelNoImplicitSenderSpec02 : ParallelNoImplicitSenderSpecBase { } | ||
| public class ParallelNoImplicitSenderSpec03 : ParallelNoImplicitSenderSpecBase { } | ||
| public class ParallelNoImplicitSenderSpec04 : ParallelNoImplicitSenderSpecBase { } | ||
| public class ParallelNoImplicitSenderSpec05 : ParallelNoImplicitSenderSpecBase { } | ||
| public class ParallelNoImplicitSenderSpec06 : ParallelNoImplicitSenderSpecBase { } | ||
| public class ParallelNoImplicitSenderSpec07 : ParallelNoImplicitSenderSpecBase { } | ||
| public class ParallelNoImplicitSenderSpec08 : ParallelNoImplicitSenderSpecBase { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "$schema": "https://xunit.github.io/schema/current/xunit.runner.schema.json", | ||
| "longRunningTestSeconds": 60, | ||
| "parallelizeAssembly": false, | ||
| "parallelizeTestCollections": false | ||
| "parallelizeAssembly": true, | ||
| "parallelizeTestCollections": true | ||
| } |
100 changes: 100 additions & 0 deletions
100
src/Akka.Hosting.TestKit/Internals/ActorCellKeepingSynchronizationContext.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="ActorCellKeepingSynchronizationContext.cs" company="Akka.NET Project"> | ||
| // Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Akka.Actor; | ||
| using Akka.Actor.Internal; | ||
|
|
||
| namespace Akka.Hosting.TestKit.Internals | ||
| { | ||
| /// <summary> | ||
| /// A decorator <see cref="SynchronizationContext"/> that preserves the outer | ||
| /// SC's scheduling (e.g. xUnit v3's MaxConcurrencySyncContext) while pinning | ||
| /// <see cref="InternalCurrentActorCellKeeper.Current"/> around every callback. | ||
| /// When no outer SC exists, falls back to <see cref="ThreadPool"/> dispatch. | ||
| /// </summary> | ||
| internal sealed class ActorCellKeepingSynchronizationContext : SynchronizationContext | ||
| { | ||
| private readonly ActorCell? _cell; | ||
| private readonly SynchronizationContext? _inner; | ||
|
|
||
| internal ActorCellKeepingSynchronizationContext(ActorCell? cell, SynchronizationContext? inner) | ||
| { | ||
| _cell = cell; | ||
| _inner = inner; | ||
| } | ||
|
|
||
| public override void Post(SendOrPostCallback d, object? state) | ||
| { | ||
| void WrappedCallback(object? s) | ||
| { | ||
| var oldCell = InternalCurrentActorCellKeeper.Current; | ||
| var oldCtx = Current; | ||
| SetSynchronizationContext(this); | ||
| InternalCurrentActorCellKeeper.Current = _cell; | ||
| try | ||
| { | ||
| d(s); | ||
| } | ||
| finally | ||
| { | ||
| InternalCurrentActorCellKeeper.Current = oldCell; | ||
| SetSynchronizationContext(oldCtx); | ||
| } | ||
| } | ||
|
|
||
| if (_inner != null) | ||
| _inner.Post(WrappedCallback, state); | ||
| else | ||
| ThreadPool.QueueUserWorkItem(WrappedCallback, state); | ||
| } | ||
|
|
||
| public override void Send(SendOrPostCallback d, object? state) | ||
| { | ||
| if (_inner != null) | ||
| { | ||
| _inner.Send(_ => | ||
| { | ||
| var oldCell = InternalCurrentActorCellKeeper.Current; | ||
| var oldCtx = Current; | ||
| SetSynchronizationContext(this); | ||
| InternalCurrentActorCellKeeper.Current = _cell; | ||
| try | ||
| { | ||
| d(state); | ||
| } | ||
| finally | ||
| { | ||
| InternalCurrentActorCellKeeper.Current = oldCell; | ||
| SetSynchronizationContext(oldCtx); | ||
| } | ||
| }, state); | ||
| } | ||
| else | ||
| { | ||
| var tcs = new TaskCompletionSource<int>(); | ||
| Post(_ => | ||
| { | ||
| try | ||
| { | ||
| d(state); | ||
| tcs.SetResult(0); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| tcs.TrySetException(e); | ||
| } | ||
| }, state); | ||
| tcs.Task.Wait(); | ||
| } | ||
| } | ||
|
|
||
| public override SynchronizationContext CreateCopy() | ||
| => new ActorCellKeepingSynchronizationContext(_cell, _inner); | ||
| } | ||
| } |
61 changes: 61 additions & 0 deletions
61
src/Akka.Hosting.TestKit/Internals/HostingCleanAmbientContextAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="HostingCleanAmbientContextAttribute.cs" company="Akka.NET Project"> | ||
| // Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| using System; | ||
| using System.Reflection; | ||
| using System.Threading; | ||
| using Akka.Actor; | ||
| using Akka.Actor.Internal; | ||
| using Akka.TestKit; | ||
| using Xunit; | ||
| using Xunit.v3; | ||
|
|
||
| namespace Akka.Hosting.TestKit.Internals | ||
| { | ||
| [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)] | ||
| internal sealed class HostingCleanAmbientContextAttribute : BeforeAfterTestAttribute | ||
| { | ||
| [ThreadStatic] | ||
| private static SynchronizationContext? _previousContext; | ||
|
|
||
| [ThreadStatic] | ||
| private static bool _applied; | ||
|
|
||
| public override void Before(MethodInfo methodUnderTest, IXunitTest test) | ||
| { | ||
| var instance = TestContext.Current.TestClassInstance; | ||
| if (instance is not TestKitBase testKit) | ||
| { | ||
| _applied = false; | ||
| return; | ||
| } | ||
|
|
||
| _applied = true; | ||
| var cell = testKit is INoImplicitSender ? null : TryGetCell(testKit); | ||
|
|
||
| InternalCurrentActorCellKeeper.Current = cell; | ||
| _previousContext = SynchronizationContext.Current; | ||
| SynchronizationContext.SetSynchronizationContext( | ||
| new ActorCellKeepingSynchronizationContext(cell, _previousContext)); | ||
| } | ||
|
|
||
| public override void After(MethodInfo methodUnderTest, IXunitTest test) | ||
| { | ||
| if (!_applied) | ||
| return; | ||
|
|
||
| _applied = false; | ||
| InternalCurrentActorCellKeeper.Current = null; | ||
| SynchronizationContext.SetSynchronizationContext(_previousContext); | ||
| _previousContext = null; | ||
| } | ||
|
|
||
| private static ActorCell? TryGetCell(TestKitBase testKit) | ||
| => testKit.TestActor is ActorRefWithCell withCell | ||
| ? withCell.Underlying as ActorCell | ||
| : null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.