Skip to content

Commit

Permalink
Fix crash on double pthread_cancel(3) on exit
Browse files Browse the repository at this point in the history
Process teardown stops all threads, thus calling pthread_cancel(3) in an
atexit(3) handler immediately before main() exits seems redundant.

On OpenBSD 7.4-current, failure to listen on the RTSP socket triggers
```
$ nc -4l 5000 &
$ nc -6l 5000 &
$ shairport-sync -c/dev/null
warning: could not establish a service on port 5000 -- program terminating. Is another instance of Shairport Sync running on this device?
Segmentation fault (core dumped)
```
```
Program terminated with signal SIGSEGV, Segmentation fault.
#0  pthread_cancel (thread=0x6207604e640) at /usr/src/lib/librthread/rthread.c:433
433             if (tib->tib_canceled == 0 && tid != 0 &&
[Current thread is 1 (process 290061)]
#0  pthread_cancel (thread=0x6207604e640) at /usr/src/lib/librthread/rthread.c:433
#1  0x0000061e5377df14 in exit_rtsp_listener ()
#2  0x000006212fdb4140 in _libc___cxa_finalize (dso=0x0) at /usr/src/lib/libc/stdlib/atexit.c:177
#3  0x000006212fde9c45 in _libc_exit (status=0) at /usr/src/lib/libc/stdlib/exit.c:54
#4  0x0000061e5377ba88 in _start ()
```

Fix this by omitting the explicit pthread_cancel(3), at which point
exit_rtsp_listener() becomes redundant, so remove it entirely.
  • Loading branch information
klemensn committed Jan 31, 2024
1 parent 750e43b commit 79e6ee0
Showing 1 changed file with 0 additions and 6 deletions.
6 changes: 0 additions & 6 deletions shairport.c
Original file line number Diff line number Diff line change
Expand Up @@ -1569,11 +1569,6 @@ const char *pid_file_proc(void) {
}
#endif

void exit_rtsp_listener() {
pthread_cancel(rtsp_listener_thread);
pthread_join(rtsp_listener_thread, NULL); // not sure you need this
}

void exit_function() {

if (type_of_exit_cleanup != TOE_emergency) {
Expand Down Expand Up @@ -2656,7 +2651,6 @@ int main(int argc, char **argv) {

activity_monitor_start(); // not yet for AP2
pthread_create(&rtsp_listener_thread, NULL, &rtsp_listen_loop, NULL);
atexit(exit_rtsp_listener);
pthread_join(rtsp_listener_thread, NULL);
return 0;
}

0 comments on commit 79e6ee0

Please sign in to comment.