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
{{ message }}
Repository navigation
async test + Apartment(ApartmentState.STA) => await not returning on STA thread #1200
We have tests that use async/await with Apartment(ApartmentState.STA) and before the upgrade to NUnit 3.0.1 they used to work, but after the upgrade they fail because WPF components don't run on STA thread after await statement.
Are you saying that when the test is marked STA, the thread is no longer STA after an await in NUnit 3, or are you saying that the workarounds in the SO post no longer work?
Yes, it is no longer STA after await in NUnit 3.2.1 (the same issue in 3.0.1 as well).
Marking test method with [Apartment(ApartmentState.STA)] as well as obsolete [RequiresSTA] doesn't work. The managed thread ID is different after await.
My current work project has ran into this very issue. I'm curious if any work has been done to address this. I'm interested in at least looking into it, but wanted to touch base and get a status update to not duplicate work effort.
There's an easy fix for this, I just did this the other day. We need to set a synchronization context and message pump on the STA thread. Otherwise, await schedules continuations on a random thread pool thread.
Here's a workaround you all can use so that you don't waste threads:
Change
[Apartment(ApartmentState.STA)]publicasyncTaskMyTestAsync(){awaitFirstThing;// Still on STA threadawaitSecondThing;// etc}
to
[Apartment(ApartmentState.STA)]publicvoidMyTest(){SingleThreadedSynchronizationContext.Await(async()=>{awaitFirstThing;// Still on STA threadawaitSecondThing;// etc});}
@nunit/framework-team The fix for this in NUnit will be to install the SingleThreadedSynchronizationContext before each invocation of a test method. This will collide with my PR #2181 so I should probably assign this to myself.
@mlimber No, just make sure [Apartment(ApartmentState.STA)] is specified for the method, class (or base class) or assembly. Some runners default to STA, others don't.
This sets a single-threaded SynchronizationContext so that the task's awaiter posts the continuation back to a queue on the same thread, so it might go without saying, but don't do .ConfigureAwait(false) if you care what thread you continue on.
Activity
rprouse commentedon Apr 7, 2016
Are you saying that when the test is marked STA, the thread is no longer STA after an await in NUnit 3, or are you saying that the workarounds in the SO post no longer work?
yar-shukan commentedon Apr 21, 2016
Yes, it is no longer STA after await in NUnit 3.2.1 (the same issue in 3.0.1 as well).
Marking test method with [Apartment(ApartmentState.STA)] as well as obsolete [RequiresSTA] doesn't work. The managed thread ID is different after await.
keithallenjackson commentedon Mar 22, 2017
My current work project has ran into this very issue. I'm curious if any work has been done to address this. I'm interested in at least looking into it, but wanted to touch base and get a status update to not duplicate work effort.
Rincoln commentedon Jun 29, 2017
I've had to do the same thing to work with proprietary ODBC drivers. My solution is a little convoluted, but works.
public Task<T> RunSTAThread<T>(Func<T> action)
{
var tcs = new TaskCompletionSource<T>();
System.Threading.Thread runThread = new System.Threading.Thread(new System.Threading.ThreadStart(() =>
{
try
{
var result = action();
tcs.TrySetResult(result);
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
}));
runThread.SetApartmentState(System.Threading.ApartmentState.STA);
runThread.Start();
return tcs.Task;
}
Then you simply run your method requiring STA thread by:
var result = await RunSTAThread(MethodRequiringSTA);
CharliePoole commentedon Jun 29, 2017
This was still marked as
confirmand nothing has been done. I guess we should at least consider it confirmed.jnm2 commentedon Jun 29, 2017
There's an easy fix for this, I just did this the other day. We need to set a synchronization context and message pump on the STA thread. Otherwise,
awaitschedules continuations on a random thread pool thread.jnm2 commentedon Jun 29, 2017
Stephen Toub gives an excellent example:
https://blogs.msdn.microsoft.com/pfxteam/2012/01/20/await-synchronizationcontext-and-console-apps/
jnm2 commentedon Jun 29, 2017
Here's a workaround you all can use so that you don't waste threads:
Change
to
And use:
jnm2 commentedon Jun 29, 2017
@nunit/framework-team The fix for this in NUnit will be to install the SingleThreadedSynchronizationContext before each invocation of a test method. This will collide with my PR #2181 so I should probably assign this to myself.
CharliePoole commentedon Jun 29, 2017
Cool!
jnm2 commentedon Jun 29, 2017
(I've been using this for my console apps because sometimes jumping around a bunch of threads gets on my nerves 😛)
jnm2 commentedon May 14, 2018
This is fixed for 3.11! 🎉
mlimber commentedon Feb 15, 2019
@jnm2: Is there anything that needs to be done to take advantage of this in 3.11 for a test like:
jnm2 commentedon Feb 15, 2019
@mlimber No, just make sure
[Apartment(ApartmentState.STA)]is specified for the method, class (or base class) or assembly. Some runners default to STA, others don't.If you're curious, see https://github.com/nunit/nunit/blob/v3.11/src/NUnitFramework/framework/Internal/AsyncToSyncAdapter.cs#L113 and https://github.com/nunit/nunit/blob/v3.11/src/NUnitFramework/framework/Internal/SingleThreadedSynchronizationContext.cs#L30.
This sets a single-threaded SynchronizationContext so that the task's awaiter posts the continuation back to a queue on the same thread, so it might go without saying, but don't do
.ConfigureAwait(false)if you care what thread you continue on.apobekiaris commentedon Jan 23, 2021
for me the next test fails
however this pass
I thought it was fixed in 3.11?? and I currently on 3.13
Steinblock commentedon Mar 24, 2022
This was I trap I ran into, too.
To understand it you must know, that behind the scenes something like this happens
So the code in fact behaves like expected.
I solved it this way