From 35eb25749ebbfce0c73e9f48b0c041512d771d7f Mon Sep 17 00:00:00 2001 From: "R. Matthew Emerson" Date: Mon, 8 Jul 2024 20:31:28 -0700 Subject: [PATCH] In make-tcp-socket, close stream (not just fd) when connect fails --- library/sockets.lisp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/library/sockets.lisp b/library/sockets.lisp index b653b2035..b74a10461 100644 --- a/library/sockets.lisp +++ b/library/sockets.lisp @@ -685,7 +685,19 @@ the socket is not connected.")) :port remote-port :allow-other-keys t keys)))) - (%socket-connect fd socket-address timeout-in-milliseconds))) + (handler-bind + ((socket-creation-error + (lambda (c) + (declare (ignore c)) + ;; When connect fails, the fd is no longer usable, + ;; and must be closed. Because we've already made + ;; a stream using this fd, close the fd by closing + ;; the stream. + (close socket) + ;; Don't try to close fd again in the unwind-protect + ;; cleanup form. + (setq fd -1)))) + (%socket-connect fd socket-address timeout-in-milliseconds)))) (setq fd -1) socket)) (unless (< fd 0)