|
| 1 | +/** |
| 2 | + * Copyright 2026 The Emscripten Authors. All rights reserved. |
| 3 | + * Emscripten is available under two separate licenses, the MIT license and the |
| 4 | + * University of Illinois/NCSA Open Source License. Both these licenses can be |
| 5 | + * found in the LICENSE file. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <pthread.h> |
| 9 | +#include <sys/types.h> |
| 10 | +#include <stdbool.h> |
| 11 | +#include <stdio.h> |
| 12 | +#include <stdlib.h> |
| 13 | +#include <assert.h> |
| 14 | +#include <unistd.h> |
| 15 | +#include <errno.h> |
| 16 | +#include <signal.h> |
| 17 | + |
| 18 | +pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; |
| 19 | +pthread_cond_t started = PTHREAD_COND_INITIALIZER; |
| 20 | +pthread_cond_t unblock = PTHREAD_COND_INITIALIZER; |
| 21 | +pthread_cond_t pending = PTHREAD_COND_INITIALIZER; |
| 22 | +_Atomic bool got_sigterm = false; |
| 23 | +_Atomic bool is_pending = false; |
| 24 | +_Atomic bool unblock_signal = false; |
| 25 | + |
| 26 | +pthread_t child_thread; |
| 27 | + |
| 28 | +void signal_handler(int sig, siginfo_t * info, void * arg) { |
| 29 | + printf("signal_handler: sig=%d onthread=%d\n", sig, pthread_self() == child_thread); |
| 30 | + assert(sig == SIGTERM); |
| 31 | + assert(pthread_self() == child_thread); |
| 32 | + got_sigterm = true; |
| 33 | +} |
| 34 | + |
| 35 | +void setup_handler() { |
| 36 | + struct sigaction act; |
| 37 | + sigemptyset(&act.sa_mask); |
| 38 | + act.sa_flags = SA_SIGINFO; |
| 39 | + act.sa_sigaction = signal_handler; |
| 40 | + sigaction(SIGTERM, &act, NULL); |
| 41 | +} |
| 42 | + |
| 43 | +void sleepms(long msecs) { |
| 44 | + usleep(msecs * 1000); |
| 45 | +} |
| 46 | + |
| 47 | +void *thread_start(void *arg) { |
| 48 | + sigset_t set; |
| 49 | + int ret; |
| 50 | + |
| 51 | + // First we block all signals. |
| 52 | + sigfillset(&set); |
| 53 | + pthread_sigmask(SIG_BLOCK, &set, NULL); |
| 54 | + |
| 55 | + // Now we signal that we are running |
| 56 | + pthread_mutex_lock(&lock); |
| 57 | + pthread_cond_signal(&started); |
| 58 | + pthread_mutex_unlock(&lock); |
| 59 | + |
| 60 | + printf("Waiting for SIGTERM to becoming pending\n"); |
| 61 | + |
| 62 | + // Now loop until we see that SIGTERM is pending. |
| 63 | + while (1) { |
| 64 | + sigpending(&set); |
| 65 | + if (sigismember(&set, SIGTERM)) { |
| 66 | + printf("SIGTERM is now pending\n"); |
| 67 | + pthread_mutex_lock(&lock); |
| 68 | + is_pending = true; |
| 69 | + pthread_cond_signal(&pending); |
| 70 | + pthread_mutex_unlock(&lock); |
| 71 | + break; |
| 72 | + } |
| 73 | + sleepms(1); |
| 74 | + } |
| 75 | + |
| 76 | + assert(!got_sigterm); |
| 77 | + |
| 78 | + pthread_mutex_lock(&lock); |
| 79 | + if (!unblock_signal) { |
| 80 | + pthread_cond_wait(&unblock, &lock); |
| 81 | + } |
| 82 | + pthread_mutex_unlock(&lock); |
| 83 | + |
| 84 | + // Now unlock all signals andwe should recieve SIGTERM here. |
| 85 | + printf("Unblocking signals\n"); |
| 86 | + sigfillset(&set); |
| 87 | + pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
| 88 | + |
| 89 | + assert(got_sigterm); |
| 90 | + |
| 91 | + return NULL; |
| 92 | +} |
| 93 | + |
| 94 | +int main() { |
| 95 | + int ret; |
| 96 | + setup_handler(); |
| 97 | + |
| 98 | + pthread_mutex_lock(&lock); |
| 99 | + ret = pthread_create(&child_thread, NULL, thread_start, 0); |
| 100 | + assert(ret == 0); |
| 101 | + |
| 102 | + // Wait until thread kicks in and sets the shared variable. |
| 103 | + pthread_cond_wait(&started, &lock); |
| 104 | + pthread_mutex_unlock(&lock); |
| 105 | + printf("thread has started, sending SIGTERM\n"); |
| 106 | + |
| 107 | + ret = pthread_kill(child_thread, SIGTERM); |
| 108 | + assert(ret == 0); |
| 109 | + printf("SIGTERM sent\n"); |
| 110 | + |
| 111 | + pthread_mutex_lock(&lock); |
| 112 | + if (!is_pending) { |
| 113 | + pthread_cond_wait(&pending, &lock); |
| 114 | + } |
| 115 | + |
| 116 | + // Now the signal is pending on the child thread we block and unblock |
| 117 | + // all signals on the mains thread, which should be a no-op |
| 118 | + // We had a bug where pending signals were not stored in TLS which would |
| 119 | + // cause this to raise the pending signals erronously here. |
| 120 | + sigset_t set; |
| 121 | + sigfillset(&set); |
| 122 | + pthread_sigmask(SIG_BLOCK, &set, NULL); |
| 123 | + pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
| 124 | + |
| 125 | + // Signal the child thread to unlock and recieve the signal |
| 126 | + unblock_signal = true; |
| 127 | + pthread_cond_signal(&unblock); |
| 128 | + pthread_mutex_unlock(&lock); |
| 129 | + |
| 130 | + pthread_join(child_thread, NULL); |
| 131 | + printf("done\n"); |
| 132 | + return 0; |
| 133 | +} |
0 commit comments