Skip to content

pthread_key_create does not initialize thread local variable with null #8740

Description

@AndreasReich

The following code fails with emscripten right now:

pthread_key_t k;
pthread_key_create(&k, NULL);
assert(pthread_getspecific(k) == 0); // fine

pthread_setspecific(k, (void*)1);
pthread_key_delete(k);

pthread_key_create(&k, NULL);
assert(pthread_getspecific(k) == 0); // fails

According to POSIX specification, after creation of a thread local key, the value should be zero on key creation on all threads.
See: http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_key_create.html

Activity

  1. VirtualTim commented on Jun 25, 2019

    @VirtualTim
    Collaborator

    @abujalski is working on similar bugs.

  2. abujalski commented on Jun 26, 2019

    @abujalski
    Contributor

    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 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.

  3. AndreasReich commented on Jun 26, 2019

    @AndreasReich
    Author

    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.

  4. stale commented on Jun 25, 2020

    @stale

    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.

  5. AndreasReich commented on Jun 25, 2020

    @AndreasReich
    Author

    I believe this is still unchanged. Need to re-test against newest version though to be fair

  6. stale commented on Jun 26, 2021

    @stale

    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.

  7. sbc100 commented on Jun 29, 2021

    @sbc100
    Collaborator

    ping

  8. sbc100 commented on Jun 29, 2021

    @sbc100
    Collaborator

    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!)

  9. kleisauke commented on Nov 11, 2021

    @kleisauke
    Collaborator

    FWIW, this should probably be re-opened due to this TODO-item:

    // TODO(sbc): Implement circular list of threads
    /*
    __tl_lock();
    do td->tsd[k] = 0;
    while ((td=td->next)!=self);
    __tl_unlock();
    */

    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).

  10. reopened this on Nov 11, 2021
  11. sbc100 commented on Nov 11, 2021

    @sbc100
    Collaborator

    Ah yes, thanks for pointing that out. Do you fancy opening a PR to do that?

  12. kleisauke commented on Nov 11, 2021

    @kleisauke
    Collaborator

    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.

  13. mannk123 commented on Oct 28, 2022

    @mannk123
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions