Skip to content

atexit callback reads an incorrect value from a TLS variable when -sPROXY_TO_PTHREAD=1 #26040

Open

Description

@ktock

In Emscripten, an atexit callback reads an unexpected value from a TLS variable when -sPROXY_TO_PTHREAD=1 is used.

Reproducer

#include <stdio.h>
#include <stdlib.h>

__thread int a = 0;

static void print_a(void)
{
  printf("a=%d\n", a);
}

int main(int argc, char **argv)
{
  a = 1;
  atexit(print_a);
  exit(0);
}

Expected behaviour

When -sPROXY_TO_PTHREAD=1 is not used, atexit callback can read the correct value from the TLS variable.

$ emcc -o ./test.js -sEXIT_RUNTIME=1 ./test.c
$ node ./test.js
a=1

gcc can also read the TLS variable correctly.

$ gcc -o ./a.out ./test.c
$ ./a.out
a=1

Actual behaviour

When -sPROXY_TO_PTHREAD=1 is used, Emscripten's atexit callback reads an incorrect value from the TLS variable.

$ emcc -o ./test.js -pthread -sPROXY_TO_PTHREAD=1 -sEXIT_RUNTIME=1 ./test.c
$ node ./test.js
a=0

emcc -v

emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 4.0.23-git (28bcb86466a273859b8adb43cb167b97e05e145d)
clang version 22.0.0git (https:/github.com/llvm/llvm-project 8518d2c4057d9aa4249b8466a4d77771e4f1bf4f)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /emsdk/upstream/bin
Build config: +assertions

Activity

  1. changed the title [-]atexit callback reads an incorrect value from a TLS variable[/-] [+]atexit callback reads an incorrect value from a TLS variable when `-sPROXY_TO_PTHREAD=1`[/+] on Jan 5, 2026
  2. brendandahl commented on Jan 6, 2026

    @brendandahl
    Collaborator

    This appears to be happening because we call __funcs_on_exit() on the main browser thread where TLS is not setup. We should either be calling it from the worker pthread thread that calls main or maybe setup TLS on the main thread. I'm guessing the former would be better, but not sure.

  3. sbc100 commented on Jan 10, 2026

    @sbc100
    Collaborator

    Its not that TLS is not setup on the main browser thread, right? Its just that that value of the TLS variable a is never changed except on the proxied main thread, so the value on the main thread remains zero.

    Apparently the atexit functions are supposed to always run on the thread which calls exit() so sometimes they do run on different threads, but this is clearly and example of where we are doing the wrong thing since they should be running on the same proxied main thread.

  4. sbc100 commented on Jan 11, 2026

    @sbc100
    Collaborator

    Unfortunately this is going to be quite tricky to fix. It might require a major refactor of how to handle exit in pthread environments.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions