Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Lib/asyncio/base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,13 @@ def _set_nodelay(sock):
if (sock.family in {socket.AF_INET, socket.AF_INET6} and
sock.type == socket.SOCK_STREAM and
sock.proto == socket.IPPROTO_TCP):
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
try:
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
except OSError:
# TCP_NODELAY is only a latency optimization. It can fail
# on a connection already reset by the peer (e.g. EINVAL
# on macOS); the error will surface on the next I/O.
pass
else:
def _set_nodelay(sock):
pass
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_asyncio/test_base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2517,6 +2517,17 @@ def test_set_nodelay(self):
sock.setblocking(False)
self.check_set_nodelay(sock)

@unittest.skipUnless(hasattr(socket, 'TCP_NODELAY'),
'need socket.TCP_NODELAY')
def test_set_nodelay_error(self):
# gh-161076: a failing setsockopt() must not break transport creation.
sock = mock.Mock(family=socket.AF_INET, type=socket.SOCK_STREAM,
proto=socket.IPPROTO_TCP)
sock.setsockopt.side_effect = OSError(errno.EINVAL, 'Invalid argument')
base_events._set_nodelay(sock)
sock.setsockopt.assert_called_once_with(
socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)



if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fix :mod:`asyncio` leaving a half-created transport registered with the
event loop and the server when setting ``TCP_NODELAY`` on a socket fails.
Such a failure could make :meth:`asyncio.Server.wait_closed` hang and
prevent new connections from being served. Errors from
``setsockopt(TCP_NODELAY)`` are now ignored.
Loading