Force the OneTimeTearDown to be executed on the current thread if STA - #4004
Conversation
…ingleThreaded flag is on
|
NOTE to self or other reviewers. This may also fix: |
|
@stevenaw You looked at this earlier. It doesn't come with any tests, but do (or did) you see any possible side-effects of this? If not, any reason for not merging it in? |
stevenaw
left a comment
There was a problem hiding this comment.
I hadn't had the chance to pull and run the code, but the changes "look" safe to me and appear to be backwards compatible too. I'd be fine if it were merged in.
I think I'd mostly avoided any explicit action on it earlier out of a desire to dig into if there was also any relation to the changes in the underlying bug fixed by #4099 , but that investigation doesn't warrant holding this up.
Not sure if we also prefer Rob review since he self-assigned, but I can approve from my own involvement in this. I understand he's a little busy right now. Otherwise, feel free to merge this in @OsirisTerje 🙂
This aims to be a workaround for #3961
When both
SingleThreadedandParallizableattributes are attached to the test fixture,SingleThreadedAttribute.ApplyToContextmay be executed later than expected (even after all test methods in the test fixtures are executed), so it leads to the following result:The parent context still has
IsSingleThreadedfalse, and all the test methods are executed on this context: (targetApartmentandcurrentApartmentare bothUnknown, inWorkItem.Execute), soWorkItem.RunOnCurrentThreadis always called.Then, when the
OneTimeTearDownof test fixture is executed, a newOneTimeTearDownWorkItemis created based on the current work item (seeCompositeWorkItem.OnAllChildItemsCompleted), which inherits the parentExecutionStrategy. At that time, since theContext.IsSingleThreadedmay still haven't been set to true, theExecutionStrategywill beParallel.At this time, not sure about the call stack,
SingleThreadedAttribute.ApplyToContextis called andIsSingleThreadedset to true.When the one time tear down work item is actually executed, though
Context.IsSingleThreadedis now true, but theParallelExecutionStrategyis already set in step 2, so it will still fall into theParallelExecutionStrategy.Parallelbranch and executed by a parallel queue, which may happen on another thread (seeParallelWorkItemDispatcher.Dispatch).The PR adds a workaround, when it's going to execute the work item, double-check if
Context.IsSingleThreadedis set to true, and if so, force it to run on the current thread.The clean fix should be investiage why step 3 is delayed but that's beyond my ability. Thanks.