Skip to content
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

Force the OneTimeTearDown to be executed on the current thread if STA #4004

Merged

Conversation

EraserKing
Copy link
Contributor

@EraserKing EraserKing commented Nov 25, 2021

This aims to be a workaround for #3961

When both SingleThreaded and Parallizable attributes are attached to the test fixture, SingleThreadedAttribute.ApplyToContext may be executed later than expected (even after all test methods in the test fixtures are executed), so it leads to the following result:

  1. The parent context still has IsSingleThreaded false, and all the test methods are executed on this context: (targetApartment and currentApartment are both Unknown, in WorkItem.Execute), so WorkItem.RunOnCurrentThread is always called.

  2. Then, when the OneTimeTearDown of test fixture is executed, a new OneTimeTearDownWorkItem is created based on the current work item (see CompositeWorkItem.OnAllChildItemsCompleted), which inherits the parent ExecutionStrategy. At that time, since the Context.IsSingleThreaded may still haven't been set to true, the ExecutionStrategy will be Parallel.

  3. At this time, not sure about the call stack, SingleThreadedAttribute.ApplyToContext is called and IsSingleThreaded set to true.

  4. When the one time tear down work item is actually executed, though Context.IsSingleThreaded is now true, but the ParallelExecutionStrategy is already set in step 2, so it will still fall into the ParallelExecutionStrategy.Parallel branch and executed by a parallel queue, which may happen on another thread (see ParallelWorkItemDispatcher.Dispatch).

  5. The PR adds a workaround, when it's going to execute the work item, double-check if Context.IsSingleThreaded is 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.

@dnfadmin
Copy link

dnfadmin commented Nov 25, 2021

CLA assistant check
All CLA requirements met.

@rprouse rprouse self-requested a review February 21, 2023 02:52
@OsirisTerje
Copy link
Member

@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?

Copy link
Member

@stevenaw stevenaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🙂

@OsirisTerje OsirisTerje merged commit 0b75bff into nunit:master Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants