Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@vargaz
Copy link
Contributor

@vargaz vargaz commented Aug 26, 2017

No description provided.

Copy link
Contributor

Choose a reason for hiding this comment

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

Other uses are wrapped in MONO_ENTER_GC_SAFE/MONO_EXIT_GC_SAFE. Do we need that here a well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will add it.

@ntherning
Copy link
Contributor

ntherning commented Aug 28, 2017

I had to change this slightly to make it work on Windows. The tid of the other thread must be read before the call to mono_join_uninterrupted() or it might get cleared before the call to mono_native_thread_join(). With my slight change this PR fixes the issue I'm trying to fix in #5037 but in a nicer way. If you go ahead and merge this PR with my slight change please close #5037.

diff --git a/mono/metadata/threads.c b/mono/metadata/threads.c
index 8eb4fe5f24e..935b3860b5b 100644
--- a/mono/metadata/threads.c
+++ b/mono/metadata/threads.c
@@ -1820,7 +1820,7 @@ ves_icall_System_Threading_Thread_Join_internal(MonoThread *this_obj, int ms)
                mono_set_pending_exception (mono_get_exception_thread_state ("Thread has not been started."));
                return FALSE;
        }
-
+       MonoNativeThreadId tid = thread_get_tid (thread);
        UNLOCK_THREAD (thread);

        if(ms== -1) {
@@ -1839,6 +1839,14 @@ ves_icall_System_Threading_Thread_Join_internal(MonoThread *this_obj, int ms)
        if(ret==MONO_THREAD_INFO_WAIT_RET_SUCCESS_0) {
                THREAD_DEBUG (g_message ("%s: join successful", __func__));

+               /* Wait for the thread to really exit */
+               MONO_ENTER_GC_SAFE;
+               /* This shouldn't block */
+               mono_threads_join_lock ();
+               mono_native_thread_join (tid);
+               mono_threads_join_unlock ();
+               MONO_EXIT_GC_SAFE;
+
                return(TRUE);
        }

@kumpera
Copy link
Contributor

kumpera commented Aug 28, 2017

LGTM

@vargaz
Copy link
Contributor Author

vargaz commented Aug 28, 2017

What code clears the tid ?

@vargaz vargaz merged commit ec30afd into mono:master Aug 29, 2017
@vargaz vargaz deleted the thread-join branch August 29, 2017 01:38
@nealef
Copy link
Contributor

nealef commented Aug 29, 2017

A couple of tests are failing today with:

Thread 1 (Thread 0x20000029c60 (LWP 62357)):
#0 0x0000020000310f80 in waitpid () from /lib64/libpthread.so.0
#1 0x00000000800c9f8e in mono_handle_native_crash (signal=signal@entry=0x802ffd88 "SIGSEGV", ctx=ctx@entry=0x3ffd25fcad0, info=info@entry=0x3ffd25fca50) at mini-exceptions.c:2743
#2 0x00000000800312e8 in mono_sigsegv_signal_handler (_dummy=, _info=0x3ffd25fca50, context=0x3ffd25fcad0) at mini-runtime.c:3126
#3
#4 0x00000200003087da in pthread_join () from /lib64/libpthread.so.0
#5 0x00000000802ea502 in mono_native_thread_join (tid=tid@entry=0) at mono-threads-posix.c:241
#6 0x0000000080219678 in ves_icall_System_Threading_Thread_Join_internal (this_obj=, ms=) at threads.c:1847
#7 0x00000200000981a8 in ?? ()
#8 0x000002000009805c in ?? ()
#9 0x0000020000041ff2 in ?? ()

Is the setting of thread->tid something that's platform specific?

Neale

@vargaz
Copy link
Contributor Author

vargaz commented Aug 29, 2017

Its somewhat platform specific, but all unix platforms have the same implementation.

@nealef
Copy link
Contributor

nealef commented Aug 29, 2017 via email

@vargaz vargaz restored the thread-join branch August 29, 2017 20:03
@vargaz
Copy link
Contributor Author

vargaz commented Aug 29, 2017

Ok, will revert and investigate.

akoeplinger added a commit that referenced this pull request Aug 29, 2017
…). (#5454)"

This reverts commit ec30afd.

It causes test failures on s390x.
ntherning added a commit to ntherning/mono that referenced this pull request Sep 5, 2017
This is the behavior of .NET. After this patch the code on Mono for Windows
will make sure the underlying native thread of a managed thread has died
before Thread.Join() returns.

This PR builds on PR mono#5454 but makes it Windows specific. PR mono#5454 was
reverted since it caused crashes on some Unix platforms.
@nealef
Copy link
Contributor

nealef commented Sep 11, 2017

The symptom I reported a couple of weeks has reappeared. It appears the thread exits, the tid is zeroed but it still appears in the hash table. I circumvented the SEGV with:

--- a/mono/utils/mono-threads-posix.c
+++ b/mono/utils/mono-threads-posix.c
@@ -238,7 +238,10 @@ mono_native_thread_join (MonoNativeThreadId tid)
 {
        void *res;
 
-       return !pthread_join (tid, &res);
+       if (tid != 0)
+               return !pthread_join (tid, &res);
+       else
+               return TRUE;
 }
 
 #endif /* defined(_POSIX_VERSION) */

@vargaz
Copy link
Contributor Author

vargaz commented Sep 12, 2017

The commit has been reverted so this problem is not caused by it.

@lewurm
Copy link
Contributor

lewurm commented Sep 12, 2017

@nealef probably the same issue: #5532 (comment)

@cherusker
Copy link
Contributor

@nealef @vargaz @luhenry this might be connected to https://bugzilla.xamarin.com/show_bug.cgi?id=59371 ... I suggest to remove the line that sets tid = 0 ... I don't know why the tid is set to 0 in general; 0 is a possible (valid) thread id. Furthermore, I can't find any check that reads tid == 0.

@vargaz
Copy link
Contributor Author

vargaz commented Sep 12, 2017

Another try:
#5549

@cherusker
Copy link
Contributor

@akoeplinger do you think it's interesting/possible/useful to add a Redhat distribution to the CI to capture a few more distro specific issues?

picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…). (mono/mono#5454)"

This reverts commit mono/mono@ec30afd.

It causes test failures on s390x.


Commit migrated from mono/mono@c603a69
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
This is the behavior of .NET. After this patch the code on Mono for Windows
will make sure the underlying native thread of a managed thread has died
before Thread.Join() returns.

This PR builds on PR mono/mono#5454 but makes it Windows specific. PR mono/mono#5454 was
reverted since it caused crashes on some Unix platforms.


Commit migrated from mono/mono@95f0800
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.

8 participants