Skip to content

Comments

Fix EINTR handling#7204

Merged
youknowone merged 1 commit intoRustPython:mainfrom
youknowone:eintr
Feb 23, 2026
Merged

Fix EINTR handling#7204
youknowone merged 1 commit intoRustPython:mainfrom
youknowone:eintr

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Feb 23, 2026

Summary by CodeRabbit

  • Bug Fixes
    • More reliable write operations by retrying interrupted system calls, reducing failures from transient interruptions.
    • Preserved non-blocking behavior while improving handling of interrupted writes, avoiding unintended EAGAIN changes.
    • Restored internal buffer handling after write attempts for greater stability and correctness in I/O.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 23, 2026

No actionable comments were generated in the recent review. 🎉


📝 Walkthrough

Walkthrough

Added EINTR-aware retry loops (PEP 475) to VM stdlib write paths and refactored memory view handling in raw_write to use reference-based memory views and properly restore/handle internal borrow buffers across buffered, unbuffered, and non-blocking writes.

Changes

Cohort / File(s) Summary
I/O write paths & memory view handling
crates/vm/src/stdlib/io.rs
Refactored raw_write to construct/reuse memory views via into_ref, handle internal borrowed buffers correctly, and restore internal buffers after writes.
EINTR / signal retry logic
crates/vm/src/stdlib/io.rs
Added EINTR retry loops (PEP 475) using trap_eintr and signal checks across buffered, unbuffered, and non-blocking write flows; preserved EAGAIN behavior for non-blocking writes; updated error propagation to align with retry logic.

Sequence Diagram(s)

sequenceDiagram
    participant VM as VM (raw_write)
    participant Mem as MemoryView / Buffer
    participant OS as OS (write syscall)
    participant Sig as SignalHandler

    VM->>Mem: into_ref / obtain view
    VM->>OS: write(buf)
    OS-->>VM: EINTR
    VM->>Sig: trap_eintr() / check signals
    alt signal handled / retry allowed
        VM->>OS: write(buf)  -- retry loop until success or other error
        OS-->>VM: n_written / EAGAIN / error
    end
    VM->>Mem: restore internal borrow buffer (if used)
    VM-->>Caller: return bytes written or propagate error
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~40 minutes

Poem

🐰 I nibble bytes with nimble paws,
EINTR met, I pause—then cause
A gentle loop to try once more,
Restore my buffer, steady core,
Bits hop home safe to their doors. 🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix EINTR handling' directly and specifically describes the main change in the pull request, which focuses on implementing and fixing EINTR (interrupted system call) handling in write paths.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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 and usage tips.

@youknowone youknowone marked this pull request as ready for review February 23, 2026 03:27
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
crates/vm/src/stdlib/io.rs (1)

1004-1035: Deduplicate the EINTR retry loop in raw_write.

Both branches run the same write + trap_eintr loop; extracting it into a small helper (e.g., write_with_eintr) would reduce drift and simplify maintenance. As per coding guidelines, "When branches differ only in a value but share common logic, extract the differing value first, then call the common logic once to avoid duplicate code".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/vm/src/stdlib/io.rs` around lines 1004 - 1035, The EINTR retry loop in
raw_write is duplicated in both branches; extract the common loop into a small
helper (e.g., write_with_eintr) that takes the callable target and the prepared
mem_obj and performs the vm.call_method + trap_eintr retry logic, then replace
both branches with code that prepares their respective mem_obj (PyMemoryView
from buffer_range or from VecBuffer::from(...).into_pybuffer(...)) and calls
write_with_eintr(self.raw.as_ref().unwrap(), mem_obj, vm). Ensure the helper
returns the same Result/Option shape as the current loop so callers in raw_write
can handle Ok(Some)/Ok(None)/Err uniformly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@crates/vm/src/stdlib/io.rs`:
- Around line 1004-1035: The EINTR retry loop in raw_write is duplicated in both
branches; extract the common loop into a small helper (e.g., write_with_eintr)
that takes the callable target and the prepared mem_obj and performs the
vm.call_method + trap_eintr retry logic, then replace both branches with code
that prepares their respective mem_obj (PyMemoryView from buffer_range or from
VecBuffer::from(...).into_pybuffer(...)) and calls
write_with_eintr(self.raw.as_ref().unwrap(), mem_obj, vm). Ensure the helper
returns the same Result/Option shape as the current loop so callers in raw_write
can handle Ok(Some)/Ok(None)/Err uniformly.

@youknowone youknowone merged commit a670799 into RustPython:main Feb 23, 2026
13 checks passed
@youknowone youknowone deleted the eintr branch February 23, 2026 07:13
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