From 27fb039839a2c6f339fc413be944f2a1f05bfe34 Mon Sep 17 00:00:00 2001 From: Roman Pertsev Date: Tue, 7 Oct 2025 12:49:12 +0300 Subject: [PATCH] res_audiosocket: fix temporarily unavailable Operations on non-blocking sockets may return a resource temporarily unavailable error (EAGAIN or EWOULDBLOCK). This is not a fatal error but a normal condition indicating that the operation would block. This patch corrects the handling of this case. Instead of incorrectly treating it as a reason to terminate the connection, the code now waits for data to arrive on the socket. --- res/res_audiosocket.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/res/res_audiosocket.c b/res/res_audiosocket.c index 0381fd9493..48bfc1f145 100644 --- a/res/res_audiosocket.c +++ b/res/res_audiosocket.c @@ -297,10 +297,29 @@ struct ast_frame *ast_audiosocket_receive_frame_with_hangup(const int svc, *hangup = 0; } - n = read(svc, &header, 3); - if (n == -1) { - ast_log(LOG_WARNING, "Failed to read header from AudioSocket because: %s\n", strerror(errno)); - return NULL; + while (i < 3) { + n = read(svc, header, 3); + if (n == -1) { + if (errno == EAGAIN || errno == EWOULDBLOCK) { + int poll_result = ast_wait_for_input(svc, 5); + + if (poll_result == 1) { + continue; + } else if (poll_result == 0) { + ast_debug(1, "Poll timed out while waiting for header data\n"); + continue; + } else { + ast_log(LOG_WARNING, "Poll error: %s\n", strerror(errno)); + } + } + + ast_log(LOG_ERROR, "Failed to read header from AudioSocket because: %s\n", strerror(errno)); + return NULL; + } + if (n == 0) { + break; + } + i += n; } if (n == 0 || *kind == AST_AUDIOSOCKET_KIND_HANGUP) {