Skip to content

fix(fast_io): IOCP writer_from_file overlapped flag + typed error handling (#1929, #1930) - #3608

Merged
oferchen merged 2 commits into
masterfrom
fix/iocp-writer-overlapped-and-errors
May 4, 2026
Merged

oferchen merged 2 commits into
masterfrom
fix/iocp-writer-overlapped-and-errors

Conversation

@oferchen

@oferchen oferchen commented May 4, 2026

Copy link
Copy Markdown
Owner

Summary

Closes two P0 beta-blocking IOCP bugs in fast_io:

  • Cache x86 checksum SIMD feature detection #1929 - iocp::writer_from_file now reopens std::fs::File handles with FILE_FLAG_OVERLAPPED instead of silently corrupting overlapped I/O. Path is recovered via GetFinalPathNameByHandleW, the original handle is dropped, and the file is reopened through a new IocpWriter::create_for_append that uses OPEN_EXISTING + FILE_FLAG_OVERLAPPED. Anonymous handles (pipes, unnamed temp files) are rejected under IocpPolicy::Enabled with a clear ErrorKind::Unsupported message naming FILE_FLAG_OVERLAPPED and the typical culprits (named pipes, console handles, character devices). IocpPolicy::Auto transparently falls back to standard buffered I/O when path recovery or reopen fails.

  • Optimize vectored rolling checksum aggregation #1930 - Two Win32 error codes are now mapped to typed IocpError variants:

    • ERROR_INVALID_PARAMETER (87) -> IocpError::InvalidOperation { context }. Surfaced from WriteFile, ReadFile, GetQueuedCompletionStatus, and GetQueuedCompletionStatusEx call sites in file_writer, file_reader, and pump. Maps to io::ErrorKind::InvalidInput.
    • ERROR_INSUFFICIENT_BUFFER (122) -> IocpError::InsufficientBuffer { requested, capacity }. The pump drain loop transparently doubles its OVERLAPPED_ENTRY buffer (capped at 8192 entries / 256 KiB) and retries; the typed error is only surfaced if the cap is reached. Maps to io::ErrorKind::OutOfMemory.

    Both variants implement From<IocpError> for io::Error, so existing io::Result-returning APIs stay backwards-compatible.

Files modified

  • crates/fast_io/src/iocp/error.rs (new) - IocpError enum, is_invalid_parameter, is_insufficient_buffer, classify_overlapped_error.
  • crates/fast_io/src/iocp/mod.rs - export error module and re-export IocpError.
  • crates/fast_io/src/iocp/file_factory.rs - rewrite writer_from_file per Cache x86 checksum SIMD feature detection #1929; add private path_from_file helper; add 3 Windows tests.
  • crates/fast_io/src/iocp/file_writer.rs - factor open_with_disposition; add IocpWriter::create_for_append; classify errors via classify_overlapped_error.
  • crates/fast_io/src/iocp/file_reader.rs - classify ReadFile and GetQueuedCompletionStatus errors.
  • crates/fast_io/src/iocp/pump.rs - dynamic batch growth on ERROR_INSUFFICIENT_BUFFER; classify default error path; add 2 tests.
  • crates/fast_io/src/iocp_stub.rs - mirror IocpError enum on non-Windows for type compatibility.
  • crates/fast_io/src/lib.rs - re-export IocpError.
  • crates/fast_io/Cargo.toml - add Win32_System_Pipes feature for the CreatePipe-based test.

Coordination with #1897

Did not block this work. The completion pump from #1897 is already on master; this PR only modifies the drain loop's error-handling path (adds ERROR_INSUFFICIENT_BUFFER retry and classify_overlapped_error upgrade) and leaves the pump's public API untouched.

New error variants (signatures)

pub enum IocpError {
    InvalidOperation { context: &'static str },
    InsufficientBuffer { requested: u32, capacity: u32 },
}

impl From<IocpError> for io::Error { ... }

pub fn is_invalid_parameter(err: &io::Error) -> bool;
pub fn is_insufficient_buffer(err: &io::Error) -> bool;
pub fn classify_overlapped_error(err: io::Error, context: &'static str) -> io::Error;

Test plan

  • CI: fmt + clippy clean on Linux/macOS/Windows.
  • CI: nextest runs the 6 new error.rs unit tests on every platform (typed-error variants, kind mapping, code recognition, classify pass-through).
  • CI Windows runner: writer_from_file_enabled_reopens_with_overlapped confirms reopen produces an Iocp variant and writes succeed.
  • CI Windows runner: writer_from_file_enabled_rejects_anonymous_handle confirms anonymous (CreatePipe) handles are rejected with ErrorKind::Unsupported and the message names FILE_FLAG_OVERLAPPED.
  • CI Windows runner: writer_from_file_auto_falls_back_for_anonymous_handle confirms Auto never errors on a real file.
  • CI Windows runner: pump_drains_burst_larger_than_batch_size confirms a 4x batch burst dispatches every completion (exercises the dynamic growth path).
  • CI Windows runner: iocp_error_insufficient_buffer_round_trips confirms the typed error maps to OutOfMemory and preserves request/capacity in the message.
  • No regressions in the existing iocp_stub test suite (cross-platform compile parity preserved).

…dling (#1929, #1930)

#1929: IOCP writer_from_file now correctly handles std::fs::File handles
that lack FILE_FLAG_OVERLAPPED. Under IocpPolicy::Enabled the path is
recovered via GetFinalPathNameByHandleW, the original handle is dropped,
and the file is reopened through IocpWriter::create_for_append with
FILE_FLAG_OVERLAPPED. Anonymous handles (pipes, unnamed temp files) are
rejected with a clear ErrorKind::Unsupported message that names the
underlying FILE_FLAG_OVERLAPPED requirement. IocpPolicy::Auto silently
falls back to standard buffered I/O on any failure.

#1930: Two Win32 error codes are now mapped to typed IocpError variants:
- ERROR_INVALID_PARAMETER (87) -> IocpError::InvalidOperation, returned
  by WriteFile/ReadFile/GetQueuedCompletionStatus(Ex) call sites in the
  file reader, file writer, and completion pump. Maps to
  io::ErrorKind::InvalidInput.
- ERROR_INSUFFICIENT_BUFFER (122) -> IocpError::InsufficientBuffer.
  The pump drain loop transparently doubles its OVERLAPPED_ENTRY buffer
  (capped at 8192 entries / 256 KiB) and retries, only surfacing the
  typed error if the cap is reached. Maps to io::ErrorKind::OutOfMemory.

Both variants implement From<IocpError> for io::Error, so existing
io::Result-returning APIs remain backwards compatible. Cross-platform
stub mirrors the type for compile parity.

Tests added (Windows-gated):
- writer_from_file_enabled_reopens_with_overlapped: real file handle
  reopened as Iocp variant, content writes succeed.
- writer_from_file_enabled_rejects_anonymous_handle: CreatePipe handle
  yields ErrorKind::Unsupported with FILE_FLAG_OVERLAPPED in the message.
- writer_from_file_auto_falls_back_for_anonymous_handle: Auto policy
  never errors on a regular file (Iocp or Std both acceptable).
- pump_drains_burst_larger_than_batch_size: 4x batch-size completion
  burst dispatches without loss, exercising the dynamic-growth path.
- iocp_error_insufficient_buffer_round_trips: typed variant maps to
  OutOfMemory and preserves request/capacity in the message.
- error.rs: 6 unit tests covering both variants, code recognition
  helpers, and classify_overlapped_error pass-through.
@github-actions github-actions Bot added the bug Something isn't working label May 4, 2026
@oferchen
oferchen merged commit dbc7cc5 into master May 4, 2026
38 checks passed
@oferchen
oferchen deleted the fix/iocp-writer-overlapped-and-errors branch May 5, 2026 04:20
oferchen added a commit that referenced this pull request May 5, 2026
…dling (#1929, #1930) (#3608)

* fix(fast_io): IOCP writer_from_file overlapped flag + typed error handling (#1929, #1930)

#1929: IOCP writer_from_file now correctly handles std::fs::File handles
that lack FILE_FLAG_OVERLAPPED. Under IocpPolicy::Enabled the path is
recovered via GetFinalPathNameByHandleW, the original handle is dropped,
and the file is reopened through IocpWriter::create_for_append with
FILE_FLAG_OVERLAPPED. Anonymous handles (pipes, unnamed temp files) are
rejected with a clear ErrorKind::Unsupported message that names the
underlying FILE_FLAG_OVERLAPPED requirement. IocpPolicy::Auto silently
falls back to standard buffered I/O on any failure.

#1930: Two Win32 error codes are now mapped to typed IocpError variants:
- ERROR_INVALID_PARAMETER (87) -> IocpError::InvalidOperation, returned
  by WriteFile/ReadFile/GetQueuedCompletionStatus(Ex) call sites in the
  file reader, file writer, and completion pump. Maps to
  io::ErrorKind::InvalidInput.
- ERROR_INSUFFICIENT_BUFFER (122) -> IocpError::InsufficientBuffer.
  The pump drain loop transparently doubles its OVERLAPPED_ENTRY buffer
  (capped at 8192 entries / 256 KiB) and retries, only surfacing the
  typed error if the cap is reached. Maps to io::ErrorKind::OutOfMemory.

Both variants implement From<IocpError> for io::Error, so existing
io::Result-returning APIs remain backwards compatible. Cross-platform
stub mirrors the type for compile parity.

Tests added (Windows-gated):
- writer_from_file_enabled_reopens_with_overlapped: real file handle
  reopened as Iocp variant, content writes succeed.
- writer_from_file_enabled_rejects_anonymous_handle: CreatePipe handle
  yields ErrorKind::Unsupported with FILE_FLAG_OVERLAPPED in the message.
- writer_from_file_auto_falls_back_for_anonymous_handle: Auto policy
  never errors on a regular file (Iocp or Std both acceptable).
- pump_drains_burst_larger_than_batch_size: 4x batch-size completion
  burst dispatches without loss, exercising the dynamic-growth path.
- iocp_error_insufficient_buffer_round_trips: typed variant maps to
  OutOfMemory and preserves request/capacity in the message.
- error.rs: 6 unit tests covering both variants, code recognition
  helpers, and classify_overlapped_error pass-through.

* style: cargo fmt --all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant