Skip to content

Commit 821d39b

Browse files
Merge pull request #7674 from schdooz/calling-thread-dispatcher-sync-context-fix
Unset ActorCellKeepingSynchronizationContext in CallingThreadDispatcher
2 parents 39bf725 + 426c6c3 commit 821d39b

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

src/core/Akka.TestKit/ActorCellKeepingSynchronizationContext.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ namespace Akka.TestKit
2222
class ActorCellKeepingSynchronizationContext : SynchronizationContext
2323
{
2424
private readonly ActorCell _cell;
25-
26-
internal static ActorCell AsyncCache { get; set; }
2725

2826
/// <summary>
2927
/// TBD
@@ -46,7 +44,8 @@ public override void Post(SendOrPostCallback d, object state)
4644
var oldCell = InternalCurrentActorCellKeeper.Current;
4745
var oldContext = Current;
4846
SetSynchronizationContext(this);
49-
InternalCurrentActorCellKeeper.Current = AsyncCache ?? _cell;
47+
48+
InternalCurrentActorCellKeeper.Current = _cell;
5049

5150
try
5251
{

src/core/Akka.TestKit/CallingThreadDispatcher.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//-----------------------------------------------------------------------
77

88
using System;
9+
using System.Threading;
910
using Akka.Configuration;
1011
using Akka.Dispatch;
1112

@@ -53,7 +54,22 @@ public CallingThreadDispatcher(MessageDispatcherConfigurator configurator) : bas
5354

5455
protected override void ExecuteTask(IRunnable run)
5556
{
56-
run.Run();
57+
var currentSyncContext = SynchronizationContext.Current;
58+
59+
try
60+
{
61+
// Actors should not run with ActorCellKeepingSynchronizationContext
62+
// (or any sync context that wraps ActorCellKeepingSynchronizationContext, e.g. Xunit's AsyncTestSyncContext)
63+
// otherwise continuations in async message handlers will use ActorCellKeepingSynchronizationContext
64+
// instead of ActorTaskScheduler which causes ActorContext to be incorrect.
65+
SynchronizationContext.SetSynchronizationContext(null);
66+
67+
run.Run();
68+
}
69+
finally
70+
{
71+
SynchronizationContext.SetSynchronizationContext(currentSyncContext);
72+
}
5773
}
5874

5975
protected override void Shutdown()

src/core/Akka.TestKit/Internal/InternalTestActorRef.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -323,18 +323,6 @@ internal TestActorTaskScheduler(ActorCell testActorCell, Action<object, Exceptio
323323
_testActorCell = (TestActorCell) testActorCell;
324324
}
325325

326-
/// <inheritdoc />
327-
protected override void OnBeforeTaskStarted()
328-
{
329-
ActorCellKeepingSynchronizationContext.AsyncCache = _testActorCell;
330-
}
331-
332-
/// <inheritdoc />
333-
protected override void OnAfterTaskCompleted()
334-
{
335-
ActorCellKeepingSynchronizationContext.AsyncCache = null;
336-
}
337-
338326
public void OnTaskCompleted(object message, Exception exception)
339327
{
340328
_taskCallback(message, exception);

0 commit comments

Comments
 (0)