Add a new exit handler to enable Shairport Sync to exit cleanly — that is, on exit, to clean up everything, e.g. open files, ports, sockets, devices, threads, it has been using.

If cleanup isn’t finished after one second, exit will occur unconditionally.
Use the new exit handler for normal exit, for SIGINT and SIGTERM handling, for abnormal exit after fatal errors, and for handling quit requests from the D-Bus and MPRIS interfaces.

When an unfixable error occurs without an installed unfixable error handler, unlock the mutex protecting Shairport Sync’s alsa backend to permit a clean exit.

Fix a very low-level mutex-locking error that interfered with debugging.

Simplify much mutex lock handling.
Remove lots of old and unused code, variables and definitions.

Update audio_sndio.c to remove some harmless compiler warnings.

Undo adding two seconds for messages to be output before _Exit() on die() to allow messages to be output...
Undo making die() exit unconditionally.
This commit is contained in:
Mike Brady
2026-07-16 14:51:06 +01:00
parent 0adbe5fc72
commit 83ae1d286b
27 changed files with 317 additions and 329 deletions
+3 -3
View File
@@ -35,7 +35,7 @@
ssize_t buffered_read(buffered_tcp_desc *descriptor, void *buf, size_t count,
size_t *bytes_remaining) {
ssize_t response = -1;
if (debug_mutex_lock(&descriptor->mutex, 50000, 4) != 0)
if (pthread_mutex_lock(&descriptor->mutex) != 0)
debug(1, "problem with mutex");
pthread_cleanup_push(mutex_unlock, (void *)&descriptor->mutex);
// wipe the slate dlean before reading...
@@ -116,7 +116,7 @@ void *buffered_tcp_reader(void *arg) {
do {
int have_time_to_sleep = 0;
if (debug_mutex_lock(&descriptor->mutex, 500000, 4) != 0)
if (pthread_mutex_lock(&descriptor->mutex) != 0)
debug(1, "problem with mutex");
pthread_cleanup_push(mutex_unlock, (void *)&descriptor->mutex);
while (descriptor->buffer_occupancy == descriptor->buffer_max_size) {
@@ -150,7 +150,7 @@ void *buffered_tcp_reader(void *arg) {
nread = recv(fd, descriptor->eoq, bytes_to_request, 0);
// debug(1, "Received %d bytes for a buffer size of %d bytes.",nread,
// descriptor->buffer_occupancy + nread);
if (debug_mutex_lock(&descriptor->mutex, 50000, 4) != 0)
if (pthread_mutex_lock(&descriptor->mutex) != 0)
debug(1, "problem with not empty mutex");
pthread_cleanup_push(mutex_unlock, (void *)&descriptor->mutex);
if (nread < 0) {
+12 -3
View File
@@ -42,6 +42,8 @@ int debugger_show_file_and_line = 1;
static uint64_t ns_time_at_startup = 0;
static uint64_t ns_time_at_last_debug_message;
void (*local_exit_requester)(const int exit_status) = NULL;
// always lock use this when accessing the ns_time_at_last_debug_message
static pthread_mutex_t debug_timing_lock = PTHREAD_MUTEX_INITIALIZER;
@@ -57,13 +59,14 @@ uint64_t debug_get_absolute_time_in_ns() {
return time_now_ns;
}
void debug_init(int level, int show_elapsed_time, int show_relative_time, int show_file_and_line) {
void debug_init(int level, int show_elapsed_time, int show_relative_time, int show_file_and_line, void (*exit_requester)(const int exit_status)) {
ns_time_at_startup = debug_get_absolute_time_in_ns();
ns_time_at_last_debug_message = ns_time_at_startup;
debuglev = level;
debugger_show_elapsed_time = show_elapsed_time;
debugger_show_relative_time = show_relative_time;
debugger_show_file_and_line = show_file_and_line;
local_exit_requester = exit_requester;
}
int debug_level() { return debuglev; };
@@ -144,8 +147,14 @@ void _die(const char *filename, const int linenumber, const char *format, ...) {
// syslog(LOG_ERR, "%s", b);
fprintf(stderr, "%s\n", b);
pthread_setcancelstate(oldState, NULL);
usleep(2000000); //allow some time for printing of message
_Exit(EXIT_FAILURE);
if (local_exit_requester != NULL) {
local_exit_requester(EXIT_FAILURE);
// wait for the exit request to be honoured
usleep(1000000);
// if not, head for the hills
fprintf(stderr, "fatal error: exit cleanup could not be completed.\n");
_Exit(EXIT_FAILURE);
}
}
void _warn(const char *filename, const int linenumber, const char *format, ...) {
+2 -2
View File
@@ -1,7 +1,7 @@
/*
MIT License
Copyright (c) 2023--2025 Mike Brady 4265913+mikebrady@users.noreply.github.com
Copyright (c) 2023--2026 Mike Brady 4265913+mikebrady@users.noreply.github.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -38,7 +38,7 @@ SOFTWARE.
// level 0 is no messages, level 3 is most messages
EXTERNC void debug_init(int level, int show_elapsed_time, int show_relative_time,
int show_file_and_line);
int show_file_and_line, void (*exit_requester)(const int exit_status));
EXTERNC int debug_level();
EXTERNC void set_debug_level(int level);
+66
View File
@@ -0,0 +1,66 @@
/*
MIT License
Copyright (c) 2023--2026 Mike Brady 4265913+mikebrady@users.noreply.github.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// exit_init() starts a thread what waits for the exit_request_flag to
// be set to non-zero.
// It then runs the standard exit(exit_status) to
// terminate the program, running all the atexit handlers first.
// exit_request() can be called from anywhere in the program
// including a SIG handler and
// the thread will take care of terminating the program cleanly.
// pass in EXIT_SUCCESS or EXIT_FAILURE in the request.
#include <signal.h> // for sig_atomic_t
#include <stdlib.h> // for EXIT_SUCCESS
#include <string.h> // for memset
#include <unistd.h> // for usleep
#include <pthread.h>
#include "exit.h"
#include "common.h"
volatile sig_atomic_t exit_request_flag = 0;
volatile sig_atomic_t exit_status = EXIT_SUCCESS;
pthread_t exit_manager_thread;
void *exit_manager(__attribute__((unused)) void *arg) {
while(exit_request_flag == 0) {
usleep(100000);
}
exit(exit_status);
return NULL;
}
void exit_init() {
memset(&exit_manager_thread, 0, sizeof(pthread_t));
named_pthread_create(&exit_manager_thread, NULL, &exit_manager, NULL, "exit_manager");
}
void exit_request(const int exit_status_requested) {
exit_status = exit_status_requested; // EXIT_SUCCESS or EXIT_FAILURE
exit_request_flag = 1; // ask for exit
}
+30
View File
@@ -0,0 +1,30 @@
/*
MIT License
Copyright (c) 2023--2026 Mike Brady 4265913+mikebrady@users.noreply.github.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <stddef.h>
void exit_init();
void exit_request(const int exit_status); // call this to ask for the program to be exited.
// pass in EXIT_SUCCESS or EXIT_FAILURE.
+2 -2
View File
@@ -52,7 +52,7 @@ int _safe_socket_close(const char *filename, const int linenumber, int *sockfd)
int result = 0;
int oldstate;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
debug_mutex_lock(&safe_socket_lock, 1000000, 1);
pthread_mutex_lock(&safe_socket_lock);
if (*sockfd == 0) {
_debug(filename, linenumber, 1, "_safe_socket_close: socket is zero!");
}
@@ -64,7 +64,7 @@ int _safe_socket_close(const char *filename, const int linenumber, int *sockfd)
} else {
_debug(filename, linenumber, 1, "_safe_socket_close: socket already closed!");
}
debug_mutex_unlock(&safe_socket_lock, 4);
pthread_mutex_unlock(&safe_socket_lock);
pthread_setcancelstate(oldstate, NULL);
return result;
}
+4 -4
View File
@@ -45,7 +45,7 @@ static pthread_mutex_t reference_counter_lock = PTHREAD_MUTEX_INITIALIZER;
static int msg_indexes = 1;
void msg_retain(rtsp_message *msg) {
int rc = debug_mutex_lock(&reference_counter_lock, 500000, 4);
int rc = pthread_mutex_lock(&reference_counter_lock);
if (rc)
debug(1, "Error %d locking reference counter lock", rc);
if (msg > (rtsp_message *)0x00010000) {
@@ -63,7 +63,7 @@ void msg_retain(rtsp_message *msg) {
rtsp_message *msg_init(void) {
// no thread cancellation points here
int rc = debug_mutex_lock(&reference_counter_lock, 500000, 4);
int rc = pthread_mutex_lock(&reference_counter_lock);
if (rc)
debug(1, "Error %d locking reference counter lock", rc);
@@ -128,7 +128,7 @@ void _debug_print_msg_headers(rtsp_conn_info *conn, const char *filename, const
}
void msg_free(rtsp_message **msgh) {
debug_mutex_lock(&reference_counter_lock, 1000, 0);
pthread_mutex_lock(&reference_counter_lock);
if (*msgh > (rtsp_message *)0x00010000) {
rtsp_message *msg = *msgh;
msg->referenceCount--;
@@ -161,7 +161,7 @@ void msg_free(rtsp_message **msgh) {
"%" PRIxPTR ".",
(uintptr_t)*msgh);
}
debug_mutex_unlock(&reference_counter_lock, 0);
pthread_mutex_unlock(&reference_counter_lock);
}
int msg_handle_line(rtsp_message **pmsg, char *line) {