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

Skip to content

Conversation

@vargaz
Copy link
Contributor

@vargaz vargaz commented Sep 12, 2017

…ge the check for started threads in mono_thread_set_name_internal () so it check the thread state and not the tid. Avoid setting the tid to 0 when the thread exits.

Copy link
Contributor

@cherusker cherusker Sep 12, 2017

Choose a reason for hiding this comment

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

Would it be possible to have a check that does not rely on tid != 0 since, in theory, 0 is a valid thread id on many systems?

@vargaz
Copy link
Contributor Author

vargaz commented Sep 12, 2017

The test failures are caused by this PR.

@nealef
Copy link
Contributor

nealef commented Sep 18, 2017

Where do we stand with this?

@vargaz
Copy link
Contributor Author

vargaz commented Sep 18, 2017

It still causes test failures on linux.

ntherning referenced this pull request Sep 22, 2017
…-in-Thread-Join

Wait for the native thread to die in Thread.Join() on Windows
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI, when thread_get_tid() was called here in a previous attempt to fix this issue it occasionally returned 0 on Windows. That is why I had to move the thread_get_tid() call up to the LOCK_THREAD block. Let me know and I can help you test your PR on Windows, just to make sure it works 100% of the time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Calling it earlier is not going to work since the thread might not been started yet, so it doesn't have a tid.

Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't that tested for here: https://github.com/vargaz/mono/blob/74c3322958194d74d3d47473230018965fc4409f/mono/metadata/threads.c#L1820? IIUC if the thread is unstarted this condition will trigger and the fuction returns FALSE?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that test is racy, since the flag is not cleared in the newly created thread but in the creating thread. Obtaining the tid after the join () call should be better because join () will only return when the thread is close to terminating, which ensures it set its tid field.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did it return 0 on windows because of:
96ace09

Copy link
Contributor

Choose a reason for hiding this comment

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

Aha, I wasn't aware of 96ace09. That could be why tid sometimes was 0. I will try your PR on the flaky WaitHandleTest on Windows today and let you know. The PR builds don't reliably test for this flakiness since it only happens occasionally.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I have run WaitHandleTest 100 times on Windows x64 without seeing the previous flakiness. So as far as Windows is concerned this PR looks good!

@vargaz
Copy link
Contributor Author

vargaz commented Sep 25, 2017

Some linux tests are still reliably failing with this PR.

…ge the check for started threads in mono_thread_set_name_internal () so it check the thread state and not the tid. Avoid setting the tid to 0 when the thread exits.
@vargaz
Copy link
Contributor Author

vargaz commented Sep 25, 2017

Needed to call mono_thread_join () which actually tests that the thread has not been joined yet.

Copy link
Contributor

@kumpera kumpera left a comment

Choose a reason for hiding this comment

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

LGTM

@vargaz
Copy link
Contributor Author

vargaz commented Sep 25, 2017

build

@vargaz
Copy link
Contributor Author

vargaz commented Sep 25, 2017

This might still be racy, since mono_thread_join () will be a no-op if the thread is not yet added to the joinable threads list, which happens in the sgen unregister callback, much later than the thread event getting signalled.

@vargaz
Copy link
Contributor Author

vargaz commented Sep 25, 2017

The linux failures seems to be gone.

@vargaz
Copy link
Contributor Author

vargaz commented Sep 26, 2017

build

@vargaz vargaz merged commit 52b96a9 into mono:master Sep 27, 2017
@vargaz vargaz deleted the thread-join2 branch September 27, 2017 00:08
@nealef
Copy link
Contributor

nealef commented Sep 27, 2017

I still timeout running tests. This is running finalizer-exit with a timeout of 300.

Thread 3 (Thread 0x3ff843ff910 (LWP 43655)):
#0  0x0000004536f7479e in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1  0x00000000802ceaee in mono_os_cond_wait (data=<value optimized out>)
    at ../../mono/utils/mono-os-mutex.h:173
#2  get_work (data=<value optimized out>) at sgen-thread-pool.c:165
#3  thread_func (data=<value optimized out>) at sgen-thread-pool.c:196
#4  0x0000004536f6f556 in start_thread () from /lib64/libpthread.so.0
#5  0x0000004536eb78b6 in thread_start () from /lib64/libc.so.6

Thread 2 (Thread 0x3ff8256b910 (LWP 43656)):
#0  0x0000004536f783c6 in sem_wait@@GLIBC_2.2 () from /lib64/libpthread.so.0
#1  0x00000000802e6dc2 in mono_os_sem_wait () at ../../mono/utils/mono-os-semaphore.h:209
#2  mono_os_sem_timedwait () at ../../mono/utils/mono-os-semaphore.h:242
#3  mono_threads_wait_pending_operations () at mono-threads.c:246
#4  0x00000000802e8748 in mono_thread_info_safe_suspend_and_run (id=4396075697984, 
    interrupt_kernel=<value optimized out>, callback=0x8020c0a8 <async_suspend_critical>, 
    user_data=0x3ff8256a1f8) at mono-threads.c:1042
#5  0x000000008020fa52 in async_suspend_internal (thread=0x3ff825dc130, interrupt=<value optimized out>)
    at threads.c:4941
#6  0x0000000080211676 in mono_thread_suspend_all_other_threads () at threads.c:3475
#7  0x000000008018e1f2 in ves_icall_System_Environment_Exit (result=<value optimized out>) at icall.c:6736
#8  0x000003ff8a864192 in ?? ()
#9  0x000003ff8a86cdf4 in ?? ()
#10 0x000003ff8a86cbba in ?? ()
#11 0x000000008025cc6e in mono_gc_run_finalize (obj=<value optimized out>, data=<value optimized out>)
    at gc.c:313
#12 0x000000008028f058 in sgen_gc_invoke_finalizers () at sgen-gc.c:2727
#13 0x000000008025dee8 in finalizer_thread (unused=<value optimized out>) at gc.c:885
#14 0x000000008020ffa2 in start_wrapper_internal (data=<value optimized out>) at threads.c:993
#15 start_wrapper (data=<value optimized out>) at threads.c:1053
#16 0x0000004536f6f556 in start_thread () from /lib64/libpthread.so.0
#17 0x0000004536eb78b6 in thread_start () from /lib64/libc.so.6

Thread 1 (Thread 0x3ff8a87c740 (LWP 43643)):
#0  0x0000004536de326c in sigsuspend () from /lib64/libc.so.6
#1  0x00000000802ea00e in suspend_signal_handler (_dummy=<value optimized out>, info=<value optimized out>, 
    context=0x3ffcbbfe058) at mono-threads-posix-signals.c:177
#2  <signal handler called>
#3  0x0000004536f791ba in __lll_lock_wait () from /lib64/libpthread.so.0
#4  0x0000004536f72040 in pthread_mutex_lock () from /lib64/libpthread.so.0
#5  0x000000008020cf8a in mono_os_mutex_lock (thread=<value optimized out>)
    at ../../mono/utils/mono-os-mutex.h:99
#6  mono_coop_mutex_lock (thread=<value optimized out>) at ../../mono/utils/mono-coop-mutex.h:56
#7  lock_thread (thread=<value optimized out>) at threads.c:420
#8  0x0000000080210988 in mono_thread_execute_interruption () at threads.c:4456
#9  0x0000000080210bfe in mono_thread_interruption_checkpoint_request (
    bypass_abort_protection=<value optimized out>) at threads.c:4603
#10 0x000000008007c220 in mono_interruption_checkpoint_from_trampoline () at jit-icalls.c:1922
#11 0x000003ff8a8e90e4 in ?? ()
#12 0x000003ff825fe358 in ?? ()
#13 0x000003ff825fe62a in ?? ()

@vargaz
Copy link
Contributor Author

vargaz commented Sep 27, 2017

This PR only affects Thread.Join () those failures are probably unrelated.

@nealef
Copy link
Contributor

nealef commented Sep 27, 2017

You are correct, it was a hangover from the thread exit fix that was merged yesterday. I was trying to recreate the problem by lowering the timeout and lowered it too far. Back at the default all tests now pass. Apologies.

picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…ge the check for started threads in mono_thread_set_name_internal () so it check the thread state and not the tid. Avoid setting the tid to 0 when the thread exits. (mono/mono#5549)

Commit migrated from mono/mono@52b96a9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants