Title: Standard WASI sockets: lent sockets on preview1, full sockets on wasm32-wasip2 · Issue #9867 · RustPython/RustPython · GitHub
Open Graph Title: Standard WASI sockets: lent sockets on preview1, full sockets on wasm32-wasip2 · Issue #9867 · RustPython/RustPython
X Title: Standard WASI sockets: lent sockets on preview1, full sockets on wasm32-wasip2 · Issue #9867 · RustPython/RustPython
Description: Goal Give _socket real behavior on WebAssembly through standard WASI only, before revisiting the WASIX backend in #9809. There are two steps, one PR each (the first may be split, see below): WASI preview1: lent sockets. Accept, receive, ...
Open Graph Description: Goal Give _socket real behavior on WebAssembly through standard WASI only, before revisiting the WASIX backend in #9809. There are two steps, one PR each (the first may be split, see below): WASI p...
X Description: Goal Give _socket real behavior on WebAssembly through standard WASI only, before revisiting the WASIX backend in #9809. There are two steps, one PR each (the first may be split, see below): WASI p...
Opengraph URL: https://github.com/RustPython/RustPython/issues/9867
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Standard WASI sockets: lent sockets on preview1, full sockets on wasm32-wasip2","articleBody":"## Goal\n\nGive `_socket` real behavior on WebAssembly through **standard WASI only**, before revisiting the WASIX backend in #9809. There are two steps, one PR each (the first may be split, see below):\n\n1. **WASI preview1: lent sockets.** Accept, receive, send and shut down on sockets that the runtime or embedder hands to the guest, the same way preopened directories work for files.\n - Preview1 has only `sock_accept`, `sock_recv`, `sock_send` and `sock_shutdown`. It cannot create sockets, connect, or resolve names.\n - This is the scope of the `wasm32-wasip1` CPython build.\n2. **`wasm32-wasip2`: full sockets.** TCP, UDP and name lookup through `wasi:sockets`, using the BSD socket API that wasi-libc implements on top of it.\n\n| Target | Contract | Guest code | Provided by |\n|---|---|---|---|\n| wasm32-wasip1 (`host_env`) | preview1 `sock_*` on lent fds | wasi-libc (`accept`, `recv`, `send`, `shutdown`, ...) | WASI runtime (e.g. wasmtime `-S tcplisten=`) |\n| wasm32-unknown, non-web (`host_io`) | same preview1 `sock_*` subset | `host_env::wasi_p1` imports | embedder (example runner) |\n| wasm32-unknown, web | none yet | none | socket creation fails honestly; `_socket` still imports for `ssl` |\n| wasm32-wasip2 (`host_env`) | `wasi:sockets` | wasi-libc BSD sockets | WASI runtime (e.g. wasmtime with network permission) |\n| native | unchanged | unchanged | unchanged |\n\n## Principles (same as #9833 / #9853)\n\n- **One `_socket` module.**\n - `crates/stdlib/src/socket.rs` already reaches the OS only through `rustpython_host_env::socket`. Make that module compile on WASI and wasm32-unknown, gate missing pieces with `cfg`, and give `host_env::socket` a backend per target.\n - Retire the separate shim `crates/stdlib/src/socket_wasm.rs`. Do not grow a second implementation of timeouts, `sendall`, address conversion or `getaddrinfo` argument handling.\n - `crates/host_env/src/socket_wasm.rs` (constants) folds into the per-target backend.\n- **Match CPython's WASI build for what exists and what does not.**\n - Use `Modules/socketmodule.c` with its `HAVE_*` guards and the WASI `config.site` (`Tools/wasm/...`) to decide two things: which functions and constants exist at all, and which exist but fail at run time.\n - Do not invent results: no fabricated fds, no-op successes or made-up addresses.\n- **Constants come from the platform.**\n - On WASI, Python sees the wasi-libc values for `AF_*`, `SOCK_*`, `SOL_*`, `SO_*` and `SHUT_*`.\n - wasm32-unknown uses the same numbers as wasm32-wasip1, so both wasm targets agree (the same reasoning as the errno renumbering in #9853 step 3).\n- **No new Cargo feature.**\n - Lent sockets on wasm32-unknown are descriptors handed over by the embedder, just like preopened directories, so they are part of `host_io`.\n - On WASI, sockets come from wasi-libc under `host_env`, just like file I/O.\n - A separate networking feature is only needed if the guest ever opens sockets by itself on wasm32-unknown (the #9809 question).\n- **Contract tests** assert exception classes and symbolic `errno.E*` names, never numbers. Every new import lands in the guest, the host (example runner) and the contract test in the same PR.\n- No new runtime checks on native hot paths.\n\n## PR 1: preview1 lent sockets\n\nPossible split: **1a** moves WASI/wasm32-unknown onto `socket.rs` with the current capability set (structural, behavior otherwise unchanged except removing fabricated results). **1b** adds lent sockets.\n\n### Guest\n\n- **wasm32-wasip1:** implement `host_env::socket` for `target_os = \"wasi\", target_env = \"p1\"` over wasi-libc.\n - Use the `libc` crate bindings where they exist. Where they are missing, declare the wasi-libc symbols in `host_env`, never in `stdlib`.\n - Check wasi-libc for what preview1 provides: `accept`/`accept4`, `recv`/`send`, `shutdown`, `getsockopt` (`SO_TYPE`), `fcntl(O_NONBLOCK)` and `poll`.\n- **wasm32-unknown with `host_io`:**\n - Add `sock_accept`, `sock_recv`, `sock_send`, `sock_shutdown`, `fd_fdstat_set_flags` (`O_NONBLOCK`) and `poll_oneoff` (fd_read/fd_write/clock subscriptions, used for timeouts) to `crates/host_env/src/wasi_p1.rs`.\n - Implement the `host_env::socket` subset on top of them with the existing errno translation.\n- **Python surface:** decide from CPython's WASI build.\n - `socket.socket(fileno=N)` with a lent fd: the socket type comes from `fd_fdstat_get`. Family and address queries behave as CPython does on WASI.\n - Cover `accept` (connections arrive without a peer address on preview1; follow CPython), `recv`/`recv_into`, `send`/`sendall`, `shutdown`, `setblocking`/`settimeout` and `close`/`detach`.\n - `socket.socket()` with no fd, `connect`, `bind` and name lookup either do not exist or fail exactly as they do on CPython WASI.\n- **Browser** (wasm32-unknown without `host_io`): `_socket` imports, and `ssl` still imports. Socket creation fails the same way.\n\n### Host and tests\n\n- **Example runner:**\n - Add `--tcplisten ADDR` to `example_projects/wasm32_without_js/wasm-runtime`. It binds on the host and lends the listener to the guest as an fd, numbered the way the runtime numbers preopens.\n - Implement the new imports in `wasm-runtime/src/wasi.rs`, or in the shared host crate if #9853 step 1 has landed.\n- **Contract test:**\n - Add `example_projects/wasm32_without_js/test_wasi_sock.py`, reusing the harness style of `test_wasi_fs.py`, with two modes:\n - runner: `--runtime/--module`, wasm32-unknown;\n - WASI runtime: wasm32-wasip1.\n - The harness picks a free host port and lends it.\n - Scenarios:\n - the host connects, the guest accepts and echoes;\n - a receive loop handles short reads;\n - `shutdown`;\n - `settimeout` gives `TimeoutError`;\n - non-blocking `recv` gives `BlockingIOError`;\n - creating a socket with no fd fails with the expected exception;\n - an fd that is not lent gives `OSError` (`EBADF`).\n- **WASI runtime in CI:** check whether wasmer supports lending listen sockets to preview1 guests. If it does not, run this mode with wasmtime (`-S tcplisten=...`, verify the flag spelling for the pinned version).\n - Install wasmtime from a pinned action or release with a checksum.\n - The workflow change must pass zizmor.\n\n### Acceptance\n\n- The contract test passes in both modes.\n- `wasm32-unknown-unknown` builds with and without `host_io`.\n- `wasm32-wasip1` builds.\n- The browser crate builds.\n- Existing WASI snippet and CPython test runs show no new failures.\n- The AGENTS.md checks for Rust changes pass.\n\n## PR 2: wasm32-wasip2 sockets\n\n- `wasm32-wasip2` is already `cargo check`ed in CI (the cross-target matrix in `.github/workflows/ci.yaml`), and there are `target_env = \"p2\"` paths in `crates/host_env/src/os.rs`.\n- **Backend:** implement `host_env::socket` for p2 over wasi-libc's BSD API.\n - Check whether the locked `socket2` (0.6.5) supports `wasm32-wasip2`. If it does, share the existing unix backend path behind `cfg`; otherwise call `libc` directly.\n - Name lookup goes through wasi-libc `getaddrinfo`.\n- **Python surface:** `socket.rs` with p2 `cfg`s.\n - Creation, `bind`/`listen`/`connect`, TCP and UDP, `getaddrinfo`, timeouts and non-blocking I/O.\n - Options and families that wasi-libc lacks are absent or fail as their `HAVE_*` guards dictate.\n- **CI:** a job that builds `wasm32-wasip2` and runs a socket contract test under wasmtime with network permission (e.g. `-S inherit-network=y -S allow-ip-name-lookup=y`; verify the flags for the pinned version).\n - Scenarios: loopback TCP and UDP echo, `getaddrinfo(\"localhost\")`, connecting to a closed port gives `ConnectionRefusedError`, and running without network permission gives `OSError` with the expected errno name.\n - The PR 1 lent-socket scenarios also pass here.\n - Then try the relevant `Lib/test/test_socket` classes under wasip2, and mark failures per AGENTS.md.\n- **Scope:** networking, plus whatever p2 build fixes it strictly needs. This does not change the \"preview1\" decision in #9853 for files or wasm32-unknown; `wasm32-wasip2` is an additional target.\n\n### Acceptance\n\n- The p2 socket test passes in CI.\n- The PR 1 contract test passes in all its modes.\n- Native socket tests are unchanged.\n\n## After these land: #9809\n\nRevisit WASIX with this boundary in place. It would be at most one more opt-in `host_env::socket` backend for `wasm32-wasip1`, using the same `socket.rs` and the same contract tests, or it is dropped if wasip2 covers the need.\n\n## Risks and notes\n\n- **Python-visible constants on WASI change** to wasi-libc values. Check that `Lib/socket.py`'s enum conversion and `ssl` import still work.\n- **Runtime support differs.** Preview1 lent sockets are a wasmtime feature; wasmer support is unverified. Wasip2 sockets need a component-model runtime. Keep per-runtime differences as per-mode assertions in the contract test, as `test_wasi_fs.py` does.\n- **Lent fds share the fd namespace with preopened directories.** The runner must number them consistently with the runtime convention, and the guest preopen scan in `wasi_p1.rs` must skip socket fds.\n\nContext: #9809 (WASIX sockets, on hold), #9833 (preview1 file I/O for the wasm32-unknown guest), #9853 (uniform `host_io` plan).\n\n— *commented by Claude*\n","author":{"url":"https://github.com/youknowone","@type":"Person","name":"youknowone"},"datePublished":"2026-11-16T03:53:22Z","url":"https://github.com/RustPython/RustPython/issues/9867"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:5418a718-0011-baf3-1ec7-af8111c30f6c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C55A:1DC4CC:E09AD1:12BD63A:6B01D40D |
| html-safe-nonce | de1507dd67940d2a5ec87ea50702079e3abe9693f5ba81c9748b0cead7efcd22 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNTVBOjFEQzRDQzpFMDlBRDE6MTJCRDYzQTo2QjAxRDQwRCIsInZpc2l0b3JfaWQiOiI3NTY5NzYwODY4OTI2MzQ2MjUzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 263f829d00854616687c03ee5404221c0177a39f205a342f98ad1ded86a163d1 |
| hovercard-subject-tag | issue:6413795726 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/RustPython/RustPython/9867/issue_layout |
| twitter:image | https://opengraph.githubassets.com/30fa23259ffe232b98872a202d6bfa4bc9b3a6bfcd184ae4d68cc41c9766ad6e/RustPython/RustPython/issues/9867 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/30fa23259ffe232b98872a202d6bfa4bc9b3a6bfcd184ae4d68cc41c9766ad6e/RustPython/RustPython/issues/9867 |
| og:image:alt | Goal Give _socket real behavior on WebAssembly through standard WASI only, before revisiting the WASIX backend in #9809. There are two steps, one PR each (the first may be split, see below): WASI p... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | youknowone |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git |
| octolytics-dimension-user_id | 39710557 |
| octolytics-dimension-user_login | RustPython |
| octolytics-dimension-repository_id | 135201145 |
| octolytics-dimension-repository_nwo | RustPython/RustPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 135201145 |
| octolytics-dimension-repository_network_root_nwo | RustPython/RustPython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width