Skip to content

Commit d82c491

Browse files
miss-islingtondicejvstinnerbrettcannon
authored
[3.13] gh-146139: Disable socketpair authentication on WASI (GH-146140) (#148527)
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 26105b0 commit d82c491

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

650654
return (ssock, csock)
651655

0 commit comments

Comments
 (0)