Skip to content

Commit

Permalink
In make-tcp-socket, close stream (not just fd) when connect fails
Browse files Browse the repository at this point in the history
  • Loading branch information
xrme committed Jul 8, 2024
1 parent 4c6c0fc commit 22da4b4
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion library/sockets.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,20 @@ the socket is not connected."))
:port remote-port
:allow-other-keys t
keys))))
(%socket-connect fd socket-address timeout-in-milliseconds)))
(let ((err (c_connect fd (sockaddr socket-address)
(sockaddr-length socket-address)
timeout-in-milliseconds)))
(unless (eql err 0)
;; When connect fails, the fd is no longer usable,
;; and must be closed. Because we've already made
;; a stream with 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-error nil "connect" err nil
:connect-address socket-address)))))
(setq fd -1)
socket))
(unless (< fd 0)
Expand Down

0 comments on commit 22da4b4

Please sign in to comment.