Skip to content

Fix closed socket descriptors on 32-bit Windows - #9432

Merged
youknowone merged 2 commits into
RustPython:mainfrom
youknowone:windows-x86-io-audit
Oct 29, 2026
Merged

youknowone merged 2 commits into
RustPython:mainfrom
youknowone:windows-x86-io-audit

Conversation

@youknowone

@youknowone youknowone commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Summary

Fix closed socket descriptors on 32-bit Windows. Widening the unsigned INVALID_SOCKET value to i64 produced 4294967295 on x86 instead of Python's -1 sentinel.

  • Return -1 explicitly from fileno() and detach() when no socket is owned; this also fixes closed-socket repr().
  • Preserve all live descriptor conversions and ownership transfer behavior.
  • Add a Rust regression test covering closed fileno(), repeated detach(), and repr().

Only crates/stdlib/src/socket.rs is changed. No C API, ctypes, PyO3, CI, or stdlib test modifications.

Validation

  • Windows x86 release and Windows x64: complete test_socket and test_asyncio.test_sock_lowlevel pass (783 tests run, 516 skipped per architecture).
  • Windows x86 release: live socket detach/reconstruction and closed descriptor smoke checks pass.
  • Native workspace tests pass, excluding wasm, venvlauncher, and C API.
  • Workspace all-targets Clippy with -D warnings, separate C API Clippy, formatting, and pre-commit hooks pass.
  • Separate C API tests were attempted but cannot link because pythonXY.lib is unavailable; C API is unchanged and outside this fix.

Implementation, investigation, validation, and focused review were assisted by Codex. The review was an automated focused review, not human review.

Summary by CodeRabbit

  • Bug Fixes
    • detach and fileno now return -1 for closed sockets across platforms, rather than a platform-specific sentinel value.
    • Uninitialized sockets also consistently report -1 through repeated calls to both methods. Their displayed representation reflects the same descriptor value.

CI follow-up

Linux and macOS Clippy reported an unused Unix INVALID_SOCKET definition after the closed-socket fix. Restrict the constant to Windows, its only remaining code consumer. No CI workflow or lint suppression changes.

Verified with native Linux (WSL) cargo clippy --locked -p rustpython-stdlib --features threading --lib -- -D warnings, Windows workspace tests, and Windows workspace all-targets Clippy with -D warnings (same exclusions as above). GitHub CI is rerunning for the new commit.

Return the Python-facing -1 sentinel explicitly for closed sockets instead of widening Windows INVALID_SOCKET. Keep live descriptor conversions unchanged and cover fileno, repeated detach, and repr with a regression test.

Implementation, investigation, validation, and focused review assisted by Codex.

Assisted-by: Codex:GPT-5
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-28T13:38:37.820053Z
We couldn’t complete this review. Comment @codex review to try again.
d3503f3 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between d3503f3 and 2e17fdc.

Files selected for processing: 1
  • crates/stdlib/src/socket.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

PySocket.detach and PySocket.fileno return -1 when the socket is absent. The Unix INVALID_SOCKET definition was removed. A test checks repeated calls on an uninitialized socket and verifies that its representation reports fd=-1.

Changes: Closed socket descriptor

Layer / File(s) Summary
Closed-socket results and test
crates/stdlib/src/socket.rs
detach and fileno return -1 when the socket is absent. INVALID_SOCKET is now defined only on Windows. A test checks repeated calls and confirms the representation reports fd=-1.

Priority: Normal

Estimated code review effort: 1 (Trivial) | ~4 minutes

Change: Bug fix

Possibly related PRs

  • RustPython/RustPython#9266: Updates select to consume the integer returned by fileno(), which is affected by the closed-socket return value.

Suggested reviewers: 1ndahous3, kyokuping

Merge Risk: Minimal · up to 2e17f

The change corrects closed-socket descriptors on 32-bit Windows without changing live-socket handling. No actionable merge-blocking risk remains after normal checks.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: fixing closed socket descriptors on 32-bit Windows.
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 1 files.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

The closed-socket descriptor fix leaves INVALID_SOCKET referenced only by the Windows raw-socket validation branch. Remove its unused Unix definition so Linux and macOS Clippy pass with -D warnings, without changing descriptor validation or CI settings.

Investigation, implementation, validation, and focused automated review assisted by Codex.

Assisted-by: Codex:GPT-5
@codspeed

codspeed Bot commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 62 untouched benchmarks
⏩ 4 skipped benchmarks1


Comparing youknowone:windows-x86-io-audit (2e17fdc) with main (84d7afc)

Open in CodSpeed

Footnotes

  1. 4 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@youknowone
youknowone merged commit b005397 into RustPython:main Oct 29, 2026
29 checks passed
@youknowone
youknowone deleted the windows-x86-io-audit branch October 29, 2026 05:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant