diff --git a/nostr_websocket/nostr_websocket_openssl.c b/nostr_websocket/nostr_websocket_openssl.c index 2f955ead..38714c9b 100644 --- a/nostr_websocket/nostr_websocket_openssl.c +++ b/nostr_websocket/nostr_websocket_openssl.c @@ -580,23 +580,30 @@ static int tls_recv(void* ctx, void* data, size_t len, int timeout_ms) { return -1; } - // Check if SSL has pending data first - if (SSL_pending(tls->ssl) == 0 && timeout_ms > 0) { - // Only use select() if no data is pending in SSL buffers + // Always use select() to ensure proper blocking behavior + // If SSL has pending data, use zero timeout to return immediately + // Otherwise use the full timeout to block until data arrives + if (timeout_ms > 0) { fd_set readfds; struct timeval tv; FD_ZERO(&readfds); FD_SET(tls->socket_fd, &readfds); - tv.tv_sec = timeout_ms / 1000; - tv.tv_usec = (timeout_ms % 1000) * 1000; + // If SSL has buffered data, use zero timeout; otherwise use full timeout + if (SSL_pending(tls->ssl) > 0) { + tv.tv_sec = 0; + tv.tv_usec = 0; + } else { + tv.tv_sec = timeout_ms / 1000; + tv.tv_usec = (timeout_ms % 1000) * 1000; + } int result = select(tls->socket_fd + 1, &readfds, NULL, NULL, &tv); if (result < 0) { return -1; - } else if (result == 0) { - return -1; // Timeout + } else if (result == 0 && SSL_pending(tls->ssl) == 0) { + return -1; // Timeout with no pending data } }