From 496253fa281e05e68c7d8cbd629d21860dfc39fd Mon Sep 17 00:00:00 2001 From: "F. Duncanh" Date: Mon, 10 Feb 2025 23:11:45 -0500 Subject: [PATCH] join threads before closing sockets when stopping fix for historical (minor) errors in RPiPlay from a mis-transcription of changes in upstream AirplayServer --- lib/raop_ntp.c | 4 ++-- lib/raop_rtp.c | 14 +++++++++++--- lib/raop_rtp_mirror.c | 10 ++++++---- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/raop_ntp.c b/lib/raop_ntp.c index f918878..6d2a291 100644 --- a/lib/raop_ntp.c +++ b/lib/raop_ntp.c @@ -457,13 +457,13 @@ raop_ntp_stop(raop_ntp_t *raop_ntp) COND_SIGNAL(raop_ntp->wait_cond); MUTEX_UNLOCK(raop_ntp->wait_mutex); + THREAD_JOIN(raop_ntp->thread); + if (raop_ntp->tsock != -1) { closesocket(raop_ntp->tsock); raop_ntp->tsock = -1; } - THREAD_JOIN(raop_ntp->thread); - logger_log(raop_ntp->logger, LOGGER_DEBUG, "raop_ntp stopped time thread"); /* Mark thread as joined */ diff --git a/lib/raop_rtp.c b/lib/raop_rtp.c index d8fb660..cfda905 100644 --- a/lib/raop_rtp.c +++ b/lib/raop_rtp.c @@ -457,7 +457,9 @@ raop_rtp_thread_udp(void *arg) /* Timeout happened */ continue; } else if (ret == -1) { - logger_log(raop_rtp->logger, LOGGER_ERR, "raop_rtp error in select"); + int sock_err = SOCKET_GET_ERROR(); + logger_log(raop_rtp->logger, LOGGER_ERR, + "raop_rtp error in select %d %s", sock_err, SOCKET_ERROR_STRING(sock_err)); break; } @@ -832,8 +834,14 @@ raop_rtp_stop(raop_rtp_t *raop_rtp) /* Join the thread */ THREAD_JOIN(raop_rtp->thread); - if (raop_rtp->csock != -1) closesocket(raop_rtp->csock); - if (raop_rtp->dsock != -1) closesocket(raop_rtp->dsock); + if (raop_rtp->csock != -1) { + closesocket(raop_rtp->csock); + raop_rtp->csock = -1; + } + if (raop_rtp->dsock != -1) { + closesocket(raop_rtp->dsock); + raop_rtp->dsock = -1; + } /* Flush buffer into initial state */ raop_buffer_flush(raop_rtp->buffer, -1); diff --git a/lib/raop_rtp_mirror.c b/lib/raop_rtp_mirror.c index bab4180..149eff3 100644 --- a/lib/raop_rtp_mirror.c +++ b/lib/raop_rtp_mirror.c @@ -233,7 +233,9 @@ raop_rtp_mirror_thread(void *arg) /* Timeout happened */ continue; } else if (ret == -1) { - logger_log(raop_rtp_mirror->logger, LOGGER_ERR, "raop_rtp_mirror error in select"); + int sock_err = SOCKET_GET_ERROR(); + logger_log(raop_rtp_mirror->logger, LOGGER_ERR, + "raop_rtp_mirror error in select %d %s", sock_err, SOCKET_ERROR_STRING(sock_err)); break; } @@ -904,14 +906,14 @@ void raop_rtp_mirror_stop(raop_rtp_mirror_t *raop_rtp_mirror) { raop_rtp_mirror->running = 0; MUTEX_UNLOCK(raop_rtp_mirror->run_mutex); + /* Join the thread */ + THREAD_JOIN(raop_rtp_mirror->thread_mirror); + if (raop_rtp_mirror->mirror_data_sock != -1) { closesocket(raop_rtp_mirror->mirror_data_sock); raop_rtp_mirror->mirror_data_sock = -1; } - /* Join the thread */ - THREAD_JOIN(raop_rtp_mirror->thread_mirror); - /* Mark thread as joined */ MUTEX_LOCK(raop_rtp_mirror->run_mutex); raop_rtp_mirror->joined = 1;