Skip to content

Commit

Permalink
Fix future issue in sock_connect()
Browse files Browse the repository at this point in the history
Use _SyncSocketWriterFuture for cancellation, so that we could remove
the writer directly in the callback.

Fixes MagicStack#378
Closes MagicStack#389
  • Loading branch information
fantix committed Feb 9, 2021
1 parent 8c471f8 commit 4842175
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions uvloop/loop.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ cdef class Loop:

if UVLOOP_DEBUG:
if fut.cancelled():
# Shouldn't happen with _SyncSocketReaderFuture.
# Shouldn't happen with _SyncSocketWriterFuture.
raise RuntimeError(
f'_sock_sendall is called on a cancelled Future')

Expand Down Expand Up @@ -1044,9 +1044,7 @@ cdef class Loop:
else:
return

fut = self._new_future()
fut.add_done_callback(lambda fut: self._remove_writer(sock))

fut = _SyncSocketWriterFuture(sock, self)
handle = new_MethodHandle3(
self,
"Loop._sock_connect",
Expand All @@ -1059,8 +1057,16 @@ cdef class Loop:
return fut

cdef _sock_connect_cb(self, fut, sock, address):
if fut.cancelled():
return
if UVLOOP_DEBUG:
if fut.cancelled():
# Shouldn't happen with _SyncSocketWriterFuture.
raise RuntimeError(
f'_sock_connect_cb is called on a cancelled Future')

if not self._has_writer(sock):
raise RuntimeError(
f'socket {sock!r} does not have a writer '
f'in the _sock_connect_cb callback')

try:
err = sock.getsockopt(uv.SOL_SOCKET, uv.SO_ERROR)
Expand All @@ -1074,8 +1080,10 @@ cdef class Loop:
raise
except BaseException as exc:
fut.set_exception(exc)
self._remove_writer(sock)
else:
fut.set_result(None)
self._remove_writer(sock)

cdef _sock_set_reuseport(self, int fd):
cdef:
Expand Down

0 comments on commit 4842175

Please sign in to comment.