René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:5418a718-0011-baf3-1ec7-af8111c30f6c
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idC55A:1DC4CC:E09AD1:12BD63A:6B01D40D
html-safe-noncede1507dd67940d2a5ec87ea50702079e3abe9693f5ba81c9748b0cead7efcd22
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNTVBOjFEQzRDQzpFMDlBRDE6MTJCRDYzQTo2QjAxRDQwRCIsInZpc2l0b3JfaWQiOiI3NTY5NzYwODY4OTI2MzQ2MjUzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac263f829d00854616687c03ee5404221c0177a39f205a342f98ad1ded86a163d1
hovercard-subject-tagissue:6413795726
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/RustPython/RustPython/9867/issue_layout
twitter:imagehttps://opengraph.githubassets.com/30fa23259ffe232b98872a202d6bfa4bc9b3a6bfcd184ae4d68cc41c9766ad6e/RustPython/RustPython/issues/9867
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/30fa23259ffe232b98872a202d6bfa4bc9b3a6bfcd184ae4d68cc41c9766ad6e/RustPython/RustPython/issues/9867
og:image:altGoal 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameyouknowone
hostnamegithub.com
expected-hostnamegithub.com
Nonecc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c
turbo-cache-controlno-preview
go-importgithub.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git
octolytics-dimension-user_id39710557
octolytics-dimension-user_loginRustPython
octolytics-dimension-repository_id135201145
octolytics-dimension-repository_nwoRustPython/RustPython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id135201145
octolytics-dimension-repository_network_root_nwoRustPython/RustPython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release8c77c9e8439031ad54c75a250694842892f693e1
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/RustPython/RustPython/issues/9867#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FRustPython%2FRustPython%2Fissues%2F9867
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FRustPython%2FRustPython%2Fissues%2F9867
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=RustPython%2FRustPython
Reloadhttps://github.com/RustPython/RustPython/issues/9867
Reloadhttps://github.com/RustPython/RustPython/issues/9867
Reloadhttps://github.com/RustPython/RustPython/issues/9867
RustPythonhttps://github.com/RustPython
RustPythonhttps://github.com/RustPython/RustPython
Codehttps://github.com/RustPython/RustPython
Issues195 (195)https://github.com/RustPython/RustPython/issues
Pull requests132 (132)https://github.com/RustPython/RustPython/pulls
Discussionshttps://github.com/RustPython/RustPython/discussions
Actionshttps://github.com/RustPython/RustPython/actions
Projectshttps://github.com/RustPython/RustPython/projects
Wikihttps://github.com/RustPython/RustPython/wiki
Security and qualityhttps://github.com/RustPython/RustPython/security
Insightshttps://github.com/RustPython/RustPython/insights
https://github.com/RustPython/RustPython
New issuehttps://github.com/login?return_to=https://github.com/RustPython/RustPython/issues/9867
New issuehttps://github.com/login?return_to=https://github.com/RustPython/RustPython/issues/9867
Standard WASI sockets: lent sockets on preview1, full sockets on wasm32-wasip2https://github.com/RustPython/RustPython/issues/9867#top
https://github.com/youknowone
youknowonehttps://github.com/youknowone
on Nov 16, 2026https://github.com/RustPython/RustPython/issues/9867#issue-6413795726
#9809https://github.com/RustPython/RustPython/pull/9809
#9833https://github.com/RustPython/RustPython/pull/9833
#9853https://github.com/RustPython/RustPython/issues/9853
Uniform capability-scoped file I/O on wasm32 (wasi/non-web/web) and native via host_io #9853https://github.com/RustPython/RustPython/issues/9853
Add opt-in WASIX TCP and UDP socket support #9809https://github.com/RustPython/RustPython/pull/9809
Uniform capability-scoped file I/O on wasm32 (wasi/non-web/web) and native via host_io #9853https://github.com/RustPython/RustPython/issues/9853
Uniform capability-scoped file I/O on wasm32 (wasi/non-web/web) and native via host_io #9853https://github.com/RustPython/RustPython/issues/9853
#9809https://github.com/RustPython/RustPython/pull/9809
#9809https://github.com/RustPython/RustPython/pull/9809
#9833https://github.com/RustPython/RustPython/pull/9833
#9853https://github.com/RustPython/RustPython/issues/9853
Sign up for freehttps://github.com/signup?return_to=https://github.com/RustPython/RustPython/issues/9867
Sign in to commenthttps://github.com/login?return_to=https://github.com/RustPython/RustPython/issues/9867
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.