Skip to content

Commit 592c68c

Browse files
miss-islingtondicejvstinnerbrettcannon
authored
[3.14] gh-146139: Disable socketpair authentication on WASI (GH-146140) (#148526)
gh-146139: Disable `socketpair` authentication on WASI (GH-146140) 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. (cherry picked from commit a5b76d5) Co-authored-by: Joel Dice <joel.dice@akamai.com> Co-authored-by: Victor Stinner <vstinner@python.org> Co-authored-by: Brett Cannon <brett@python.org>
1 parent 0b9332b commit 592c68c

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
@@ -640,18 +640,22 @@ def _fallback_socketpair(family=AF_INET, type=SOCK_STREAM, proto=0):
640640
# Authenticating avoids using a connection from something else
641641
# able to connect to {host}:{port} instead of us.
642642
# We expect only AF_INET and AF_INET6 families.
643-
try:
644-
if (
645-
ssock.getsockname() != csock.getpeername()
646-
or csock.getsockname() != ssock.getpeername()
647-
):
648-
raise ConnectionError("Unexpected peer connection")
649-
except:
650-
# getsockname() and getpeername() can fail
651-
# if either socket isn't connected.
652-
ssock.close()
653-
csock.close()
654-
raise
643+
#
644+
# Note that we skip this on WASI because on that platorm the client socket
645+
# may not have finished connecting by the time we've reached this point (gh-146139).
646+
if sys.platform != "wasi":
647+
try:
648+
if (
649+
ssock.getsockname() != csock.getpeername()
650+
or csock.getsockname() != ssock.getpeername()
651+
):
652+
raise ConnectionError("Unexpected peer connection")
653+
except:
654+
# getsockname() and getpeername() can fail
655+
# if either socket isn't connected.
656+
ssock.close()
657+
csock.close()
658+
raise
655659

656660
return (ssock, csock)
657661

0 commit comments

Comments
 (0)