From 0bc014580a0051cb0523856e6cc676e78a65512a Mon Sep 17 00:00:00 2001 From: Michael Zillgith Date: Fri, 24 Apr 2020 15:31:45 +0200 Subject: [PATCH] - linux/windows socket: close socket when connect fails in Socket_connectAsync --- hal/socket/linux/socket_linux.c | 5 +++++ hal/socket/win32/socket_win32.c | 1 + 2 files changed, 6 insertions(+) diff --git a/hal/socket/linux/socket_linux.c b/hal/socket/linux/socket_linux.c index 020c850d..dc9bbaeb 100644 --- a/hal/socket/linux/socket_linux.c +++ b/hal/socket/linux/socket_linux.c @@ -431,6 +431,11 @@ Socket_connectAsync(Socket self, const char* address, int port) if (connect(self->fd, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) < 0) { if (errno != EINPROGRESS) { + if (close(self->fd) == -1) { + if (DEBUG_SOCKET) + printf("SOCKET: failed to close socket (errno: %i)\n", errno); + } + self->fd = -1; return false; } diff --git a/hal/socket/win32/socket_win32.c b/hal/socket/win32/socket_win32.c index d42a3674..232ba17f 100644 --- a/hal/socket/win32/socket_win32.c +++ b/hal/socket/win32/socket_win32.c @@ -386,6 +386,7 @@ Socket_connectAsync(Socket self, const char* address, int port) if (connect(self->fd, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) == SOCKET_ERROR) { if (WSAGetLastError() != WSAEWOULDBLOCK) { + closesocket(self->fd); self->fd = INVALID_SOCKET; return false; }