Bug 58317 - Leaking threads: SGen worker and Finalizer
Summary: Leaking threads: SGen worker and Finalizer
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: General (show other bugs)
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: Future Cycle (TBD)
Assignee: Ludovic Henry
URL:
Depends on:
Blocks:
 
Reported: 2017-07-22 20:04 UTC by Armin
Modified: 2017-09-07 12:55 UTC (History)
4 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

Description Armin 2017-07-22 20:04:38 UTC
TSan reports two leaky threads (commit 16309a75610232cb0368bcbb99725c2516578949): "SGen worker" and "Finalizer" neither get joined nor detached explicitly.

SGen worker:
'''''''''''
As I couldn't find a suitable function in mono-threads-posix.c, I tested to add `(void) pthread_detach (pthread_self ());` after `mono_os_cond_signal (&done_cond);` in sgen-thread-pool.c:235 - which removed the warning.
Of course, the same logic could be applied in `sgen_thread_pool_shutdown ()` right after the while-loop with `mono_os_cond_wait ()`.

Finalizer:
'''''''''
The same idea worked with the Finalizer as well: I added `(void) pthread_detach(pthread_self ());` after `mono_coop_cond_signal (&exited_cond);` in gc:913 (`finalizer_thread ()`) - with success.

==================
SGen worker
==================
WARNING: ThreadSanitizer: thread leak (pid=38445)
  Thread T1 'SGen worker' (tid=38470, finished) created by main thread at:
    #0 pthread_create <null> (mono-sgen+0x000000435a56)
    #1 mono_native_thread_create /home/cherusker/mono/mono/mono/utils/mono-threads-posix.c:190:9 (mono-sgen+0x000000f1492f)
    #2 sgen_thread_pool_start /home/cherusker/mono/mono/mono/sgen/sgen-thread-pool.c:288:3 (mono-sgen+0x000000ec64c4)
    #3 sgen_gc_init /home/cherusker/mono/mono/mono/sgen/sgen-gc.c:3674:2 (mono-sgen+0x000000dfeb9d)
    #4 mono_gc_base_init /home/cherusker/mono/mono/mono/metadata/sgen-mono.c:2995:2 (mono-sgen+0x000000d9496d)
    #5 mono_init_internal /home/cherusker/mono/mono/mono/metadata/domain.c:503:2 (mono-sgen+0x000000cf0723)
    #6 mono_init_from_assembly /home/cherusker/mono/mono/mono/metadata/domain.c:806:9 (mono-sgen+0x000000cf227c)
    #7 mini_init /home/cherusker/mono/mono/mono/mini/mini-runtime.c:3953:12 (mono-sgen+0x0000004cd324)
    #8 mono_main /home/cherusker/mono/mono/mono/mini/driver.c:2052:11 (mono-sgen+0x000000723137)
    #9 mono_main_with_options /home/cherusker/mono/mono/mono/mini/./main.c:46:9 (mono-sgen+0x0000004c1017)
    #10 main /home/cherusker/mono/mono/mono/mini/./main.c:339:9 (mono-sgen+0x0000004c0453)

SUMMARY: ThreadSanitizer: thread leak (/home/cherusker/mono/mono/mono/mini/mono-sgen+0x435a56) in __interceptor_pthread_create
==================
Finalizer
==================
WARNING: ThreadSanitizer: thread leak (pid=38445)
  Thread T2 'Finalizer' (tid=38471, finished) created by main thread at:
    #0 pthread_create <null> (mono-sgen+0x000000435a56)
    #1 mono_gc_pthread_create /home/cherusker/mono/mono/mono/metadata/sgen-mono.c:2517:8 (mono-sgen+0x000000d931fd)
    #2 mono_thread_platform_create_thread /home/cherusker/mono/mono/mono/utils/mono-threads-posix.c:79:8 (mono-sgen+0x000000f143d1)
    #3 create_thread /home/cherusker/mono/mono/mono/metadata/threads.c:1121:7 (mono-sgen+0x000000c6e5e9)
    #4 mono_thread_create_internal /home/cherusker/mono/mono/mono/metadata/threads.c:1208:8 (mono-sgen+0x000000c6d894)
    #5 mono_gc_init_finalizer_thread /home/cherusker/mono/mono/mono/metadata/gc.c:926:14 (mono-sgen+0x000000cf972c)
    #6 mono_gc_init /home/cherusker/mono/mono/mono/metadata/gc.c:961:2 (mono-sgen+0x000000cf9627)
    #7 mono_runtime_init_checked /home/cherusker/mono/mono/mono/metadata/appdomain.c:305:2 (mono-sgen+0x000000ce116d)
    #8 mini_init /home/cherusker/mono/mono/mono/mini/mini-runtime.c:4019:2 (mono-sgen+0x0000004cd4b5)
    #9 mono_main /home/cherusker/mono/mono/mono/mini/driver.c:2052:11 (mono-sgen+0x000000723137)
    #10 mono_main_with_options /home/cherusker/mono/mono/mono/mini/./main.c:46:9 (mono-sgen+0x0000004c1017)
    #11 main /home/cherusker/mono/mono/mono/mini/./main.c:339:9 (mono-sgen+0x0000004c0453)

SUMMARY: ThreadSanitizer: thread leak (/home/cherusker/mono/mono/mono/mini/mono-sgen+0x435a56) in __interceptor_pthread_create
==================
Comment 1 Ludovic Henry 2017-07-28 18:25:51 UTC
We can't just detach thread (see https://github.com/mono/mono/commit/2fd16f60) so please submit a PR with the changes you proposed before but using `mono_threads_add_joinable_thread` instead of `pthread_detach`. Please assign the PR to me. Thank you.
Comment 2 Armin 2017-07-29 11:47:29 UTC
Sure, I will gladly look into it!
Comment 3 Ludovic Henry 2017-09-07 12:55:37 UTC
Fixed with 7db0fb0c886f5157066e26c2e2ae2d39c338cf6b

Note You need to log in before you can comment on or make changes to this bug.