Skip to content

Commit d5195d7

Browse files
committed
Simply detach() socket objects in UVHandle._close()
Some advantages: * no close() syscall and no EBADF * it's not possible to have any kind of FD race
1 parent 6bfe65d commit d5195d7

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

uvloop/handles/handle.pyx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,20 @@ cdef class UVSocketHandle(UVHandle):
276276
# This code will only run for transports created from
277277
# Python sockets, i.e. with `loop.create_server(sock=sock)` etc.
278278
if self._fileobj is not None:
279-
try:
280-
socket_dec_io_ref(self._fileobj)
281-
282-
# `socket.close()` will raise an EBADF because libuv
283-
# has already closed the underlying FDself.
284-
self._fileobj.close()
285-
except OSError as ex:
286-
if ex.errno != errno_EBADF:
287-
raise
279+
if isinstance(self._fileobj, socket_socket):
280+
# Detaching the socket object is an ideal solution:
281+
# * libuv will actually close the FD
282+
# * detach() call will reset FD for the Python socket
283+
# object, which means that it won't be closed 2 times.
284+
self._fileobj.detach()
285+
else:
286+
try:
287+
# `socket.close()` will raise an EBADF because libuv
288+
# has already closed the underlying FDself.
289+
self._fileobj.close()
290+
except OSError as ex:
291+
if ex.errno != errno_EBADF:
292+
raise
288293
except Exception as ex:
289294
self._loop.call_exception_handler({
290295
'exception': ex,

0 commit comments

Comments
 (0)