Skip to content

Commit de2cb96

Browse files
committed
gh-146139: Disable socketpair authentication on WASI
Calling `connect(2)` on a non-blocking socket on WASI may leave the socket in a "connecting" but not yet "connected" state. In the former case, calling `getpeername(2)` on it will fail, leading to an unhandled exception in Python.
1 parent 6fe91a9 commit de2cb96

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

Lib/socket.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -649,18 +649,22 @@ def _fallback_socketpair(family=AF_INET, type=SOCK_STREAM, proto=0):
649649
# Authenticating avoids using a connection from something else
650650
# able to connect to {host}:{port} instead of us.
651651
# We expect only AF_INET and AF_INET6 families.
652-
try:
653-
if (
654-
ssock.getsockname() != csock.getpeername()
655-
or csock.getsockname() != ssock.getpeername()
656-
):
657-
raise ConnectionError("Unexpected peer connection")
658-
except:
659-
# getsockname() and getpeername() can fail
660-
# if either socket isn't connected.
661-
ssock.close()
662-
csock.close()
663-
raise
652+
#
653+
# Note that we skip this on WASI because on that platorm the client socket
654+
# may not have finished connecting by the time we've reached this point.
655+
if sys.platform != "wasi":
656+
try:
657+
if (
658+
ssock.getsockname() != csock.getpeername()
659+
or csock.getsockname() != ssock.getpeername()
660+
):
661+
raise ConnectionError("Unexpected peer connection")
662+
except:
663+
# getsockname() and getpeername() can fail
664+
# if either socket isn't connected.
665+
ssock.close()
666+
csock.close()
667+
raise
664668

665669
return (ssock, csock)
666670

0 commit comments

Comments
 (0)