-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Wait for the native thread to die in Thread.Join() on Windows #5037
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
Wait for the native thread to die in Thread.Join() on Windows #5037
Conversation
mono/metadata/threads.c
Outdated
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.
Doesn't mono_join_uninterrupted () already do this ?
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.
Shouldn't this use the same mechanics as mono_threads_join_threads ?
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.
@vargaz IIUC mono_join_uninterrupted waits for an event to be signalled in the other thread (in mono_threads_signal_thread_handle which is called by unregister_thread). When the event is signalled the native thread has not yet been fully terminated.
@kumpera I guess you mean doing something similar to https://github.com/mono/mono/blob/master/mono/metadata/threads.c#L5048? Is that entire sequence starting with MONO_ENTER_GC_SAFE required?
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.
I mean using that very thing, but replacing pthread_join with WaitOne
|
@niklas why is it flaky if we don't do that? Isn't it less flaky because some cases of races have just less chances of happening because it's a bit slower? |
|
@luhenry The tests in |
a100cbb to
a54c7ac
Compare
|
build |
|
This appears to build fine and tests run as expected. Any objections to merging it? @kumpera You had a comment on the implementation? |
|
@ntherning I think we must first explore on whether extending the existing thread join code from unix to windows would not solve it. We do it in this way to ensure proper lifetime of the underlying OS primitive and I'm not sure your approach allows for it. |
|
@kumpera One thing this PR addresses is the abandoned mutex issue which makes The alternative to this PR would be to change Mutex on Windows to emulate mutexes using some other OS primitive and maintain the state ourselves, similarly to how it's done on unix. But that would break interoperability when managed code and native code are sharing mutexes. So not really an option. |
|
Wouldn't calling mono_native_thread_join () solve this ? |
|
I don't think this is what @kumpera had in mind though, but rather, if possible, align the thread joining on Windows with what we do on unix. As I tried to explain I don't think there's a way to do that while replicating what .NET does regarding mutexes. |
|
@ntherning if I understand correctly what @kumpera means is: please modify |
This is the behavior of .NET. After this patch the code on Mono for Windows will make sure the underlying native thread of a runtime created thread has died before Thread.Join() returns.
a54c7ac to
6bf491a
Compare
|
But AFAICT |
|
Hi Niklas, How about the following:
|
| if (is_runtime_thread) { | ||
| // The thread was created by the runtime. Make sure the underlying | ||
| // native thread has terminated before we return. | ||
| WaitForSingleObjectEx (thread->native_handle, INFINITE, FALSE); |
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.
This is wrong, the WaitOne should respect the timeout of the icall.
|
How about we call mono_native_thread_join () which should do the same ? |
|
@vargaz AFAICT, we can't do that cuz pthread_join would fail on detached threads. OTOH, we could merge this as is and queue cleanup work to follow it. In such case, we'd only need to address the join timeout issue. |
|
We don't detach threads anymore, i.e. don't call pthread_detach (). |
|
Then I guess it's about how much @ntherning wants to go down the rabbit role. |
|
Superseded by #5454. |
This is the behavior of .NET. After this patch the code on Mono for Windows will make sure the underlying native thread of a runtime created thread has died before
Thread.Join()returns.This PR should make some of the tests in
WaitHandleTestless flaky on Windows.