-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Start thread pool worker threads in the default execution context #46181
Conversation
The execution context transfers to new threads, and this creates an extra stack frame on the thread and may cause AsyncLocal value change notifications to be sent unnecessarily. Switched to the default context before creating a worker thread.
src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.WorkerThread.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
src/coreclr/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs
Outdated
Show resolved
Hide resolved
Is it possible to test this (even if it's not a certain result) |
The |
Actually it may be a bug that this is fixing. |
Do we have tests that validate that new threads inherit execution context of the parent thread? I do not see the code that captures the execution context of parent thread in Mono, and no bugs on it either. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you!
Doesn't look like it. I added a test now, but will need to check the implementation in Mono. |
The EC flowing into thread pool threads did turn out to be a bug with visible unintended consequences, added a test for it. Also fixed the same thing for gate thread and wait thread creation. |
Can't we add a no-capture flag to the thread constructor to avoid capturing the execution context? |
Yea that's an option, it's the CC @stephentoub |
Does seem a bit odd though |
I suppose |
Yes, make it internal API for this PR |
Yep sounds good |
FYI for new reviewers there will be more changes coming I'll send an update once it's ready for review |
…use that instead
Updated to add internal |
src/mono/netcore/System.Private.CoreLib/src/System/Threading/Thread.Mono.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great. I assume we should do the same for timer threads in the portable timer case but those threads aren't created lazily AFAIK
I have updated the portable timer now as well. The timer thread is created lazily the first time a timer is scheduled. |
The execution context transfers to new threads, and this creates an extra stack frame on the thread and may cause AsyncLocal value change notifications to be sent unnecessarily. Switched to the default context before creating a worker thread.