You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
pthread_key_create does not initialize thread local variable with null #8740
Function int pthread_setspecific(pthread_key_t k, const void *x) (emscripten/system/lib/libc/musl/src/thread/pthread_setspecific.c) sets struct pthread.tsd[k] array element.
Function int __pthread_key_create(pthread_key_t *k, void (*dtor)(void *)) (emscripten/system/lib/libc/musl/src/thread/pthread_key_create.c) sets only dtor associated with given pthread_key_t and doesn't set pthread.tsd[k] to NULL.
Yes, musl's implementation is wrong. Probably should have filed a bug there?
I don't think it is enough to set the value on create to null: Any other thread that might read its value for the same key will still not see null.
I checked the libc implemention, there they use a generation counter which is compared on every access. If a thread's local generation for a given key is lower than a globally known location for the key, then the local value is set to null on access.
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant.
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.
As a workaround, we could do self->tsd[k] = 0; there, but that only sets the value to zero for the current thread, not for all threads (which is required by the POSIX standard).
I can look at implementing this next week, feel free to fix it earlier. Unit tests for this are also missing, it's a bit curious that this isn't covered by the POSIX test suite.
Hello! I took the liberty of attempting a fix for this issue. It's my first time contributing to emscripten, please let me know of anything I may have missed.
Activity
VirtualTim commentedon Jun 25, 2019
@abujalski is working on similar bugs.
abujalski commentedon Jun 26, 2019
It looks like bug in musl library.
Function
int pthread_setspecific(pthread_key_t k, const void *x)(emscripten/system/lib/libc/musl/src/thread/pthread_setspecific.c) sets structpthread.tsd[k]array element.Function
int __pthread_key_create(pthread_key_t *k, void (*dtor)(void *))(emscripten/system/lib/libc/musl/src/thread/pthread_key_create.c) sets only dtor associated with givenpthread_key_tand doesn't setpthread.tsd[k]toNULL.AndreasReich commentedon Jun 26, 2019
Yes, musl's implementation is wrong. Probably should have filed a bug there?
I don't think it is enough to set the value on create to null: Any other thread that might read its value for the same key will still not see null.
I checked the libc implemention, there they use a generation counter which is compared on every access. If a thread's local generation for a given key is lower than a globally known location for the key, then the local value is set to null on access.
stale commentedon Jun 25, 2020
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant.
AndreasReich commentedon Jun 25, 2020
I believe this is still unchanged. Need to re-test against newest version though to be fair
stale commentedon Jun 26, 2021
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.
sbc100 commentedon Jun 29, 2021
ping
sbc100 commentedon Jun 29, 2021
This bug looks like its fixed in the latest musl code: https://git.musl-libc.org/cgit/musl/tree/src/thread/pthread_key_create.c#n57
So it should be resolved once we land: #13006 (hopefully some time in the next decade!)
kleisauke commentedon Nov 11, 2021
FWIW, this should probably be re-opened due to this TODO-item:
emscripten/system/lib/libc/musl/src/thread/pthread_key_create.c
Lines 60 to 66 in f065db6
As a workaround, we could do
self->tsd[k] = 0;there, but that only sets the value to zero for the current thread, not for all threads (which is required by the POSIX standard).sbc100 commentedon Nov 11, 2021
Ah yes, thanks for pointing that out. Do you fancy opening a PR to do that?
kleisauke commentedon Nov 11, 2021
I can look at implementing this next week, feel free to fix it earlier. Unit tests for this are also missing, it's a bit curious that this isn't covered by the POSIX test suite.
mannk123 commentedon Oct 28, 2022
Hello! I took the liberty of attempting a fix for this issue. It's my first time contributing to emscripten, please let me know of anything I may have missed.