FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fallback `socketpair` authentication is unreliable on WASI · Issue #146139 · python/cpython · GitHub

Repository navigation

Fallback socketpair authentication is unreliable on WASI #146139

Closed

Description

Bug report

Bug description:

#122133 introduced authentication for the fallback socketpair implementation in Lib/socket.py. However, that's unreliable on WASI (and possibly elsewhere), where calling connect(2) on a non-blocking socket 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.

More generally, I'm a bit surprised the authentication code works reliably on any platform; I don't believe POSIX guarantees getpeername(2) will succeed for a socket which may or may not have finished connecting. Perhaps other platforms reliably complete loopback connections synchronously for non-blocking sockets?

Note that WASI socket support is not yet enabled by default in CPython, but is available via componentize-py, which has been carrying a patch to CPython which I'm finally getting around to submitting upstream.

Given the unofficial state of WASI sockets in CPython, reproducing the issue is a bit awkward, but can be done by:

  1. cloning the componentize-py Git repo (e.g. git clone --recurse-submodules https://github.com/bytecodealliance/componentize-py),
  2. switching to the cpython subdirectory, removing the aforementioned patch (e.g. git reset --hard HEAD^),
  3. following the directions in CONTRIBUTING.md to build everything,
  4. installing Wasmtime,
  5. and finally running cargo test --release --test componentize tcp_example

...at which point you'll see something like:

Traceback (most recent call last):
  File "/0/app.py", line 17, in run
  File "/python/asyncio/runners.py", line 203, in run
  File "/python/asyncio/runners.py", line 59, in __enter__
  File "/python/asyncio/runners.py", line 146, in _lazy_init
  File "/python/asyncio/events.py", line 844, in new_event_loop
  File "/python/asyncio/events.py", line 732, in new_event_loop
  File "/python/asyncio/unix_events.py", line 61, in __init__
  File "/python/asyncio/selector_events.py", line 66, in __init__
  File "/python/asyncio/selector_events.py", line 120, in _make_self_pipe
  File "/python/socket.py", line 645, in _fallback_socketpair
OSError: [Errno 53] Socket not connected

I plan to post a PR for this shortly.

CPython versions tested on:

3.14

Operating systems tested on:

Other

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Mar 18, 2026
  2. dicej commented on Mar 18, 2026

    ContributorAuthor

    cc @brettcannon @sethmlarson In case you have thoughts or opinions.

  3. added a commit that references this issue on Mar 18, 2026
  4. vstinner commented on Mar 19, 2026

    Member

    More generally, I'm a bit surprised the authentication code works reliably on any platform; I don't believe POSIX guarantees getpeername(2) will succeed for a socket which may or may not have finished connecting. Perhaps other platforms reliably complete loopback connections synchronously for non-blocking sockets?

    socketpair() is only implemented in pure Python in Lib/socket.py if _socket.socketpair() doesn't exist. On most platforms (ex: Linux), _socket.socketpair() exists and so the pure Python implementation is not used.

  5. dicej commented on Mar 19, 2026

    ContributorAuthor

    socketpair() is only implemented in pure Python in Lib/socket.py if _socket.socketpair() doesn't exist. On most platforms (ex: Linux), _socket.socketpair() exists and so the pure Python implementation is not used

    Right, I understand that. What I'm wondering is which platform(s) #122133 and #122134 were intended to address, since they were clearly targeted at (and only affect) platforms which use the pure Python version.

  6. vstinner commented on Mar 19, 2026

    Member

    I'm only aware of Windows which lacks _socket.socketpair(). I didn't know that WASI also lacks _socket.socketpair().

  7. brettcannon commented on Mar 19, 2026

    Member

    @brettcannon @sethmlarson In case you have thoughts or opinions.

    I have no opinion as I'm not a networking expert and I've been waiting to avoid worrying about it until WASI 0.3 + threads shows up. 🫣

  8. added a commit that references this issue on Apr 13, 2026
  9. added a commit that references this issue on Apr 13, 2026
  10. added 2 commits that reference this issue on Apr 13, 2026
  11. added 2 commits that reference this issue on Apr 13, 2026
  12. added a commit that references this issue on Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS-wasistdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL