Skip to content

fix(rw): fix WritePort retirement busy race and Stream zero-length << - #368

Merged
Jiu-xiao merged 3 commits into
devfrom
fix/writeport-20261119
Nov 19, 2026
Merged

Jiu-xiao merged 3 commits into
devfrom
fix/writeport-20261119

Conversation

@Jiu-xiao

Copy link
Copy Markdown
Member

Summary

Three commits separating the two behavior fixes from the cleanup/consistency refactor:

  1. 0e8b4fdfb — fix(rw): close BLOCK_DETACHED to IDLE busy race in WritePort

    • Update WritePort::TryClaimProducer(Phase* failed_phase = nullptr) to record the observed non-IDLE phase on failure.
    • Use failed_phase == Phase::BLOCK_DETACHED in WritePort::operator() so a concurrent BLOCK_DETACHED -> IDLE retirement between TryClaimProducer and WaitForRetirement no longer falsely returns ErrorCode::BUSY, while preserving oversized BLOCK_DETACHED -> BUSY precedence and leaving non-BLOCK_DETACHED phases returning ErrorCode::BUSY without touching admission_waiter_.
    • Simplify WaitForRetirement fallback from LoadPhase() == Phase::IDLE && TryClaimProducer() to TryClaimProducer().
    • Add regression test test_rw_write_port_block_detached_retirement_race in test/automatic/core/rw/write_port/test_write_port.cpp.
  2. fc5c3fcf2 — fix(rw): align WritePort::Stream operator<< zero-length behavior with Write

    • Delegate WritePort::Stream::operator<<(const ConstRawData&) directly to UNUSED(Write(data)); return *this; so zero-length << without port ownership neither claims LOCKED nor triggers empty completion/callback/POLLING DONE transitions on later Commit() or destruction.
    • Remove redundant port_->queue_data_ == nullptr checks in WritePort::Stream::Write and WritePort::Stream::EmptySize().
    • Add regression coverage VerifyStreamZeroShiftWithoutOwnership in test/automatic/core/rw/write_stream/test_write_stream.cpp.
  3. 16d60ab7a — refactor(rw): use PopBatch in WriteQueue::PopAll and hoist CAS initial loads

    • Replace ConsumeWithReader + std::memcpy in WritePort::WriteQueue::PopAll with SPSCQueue::PopBatch and remove unused <cstring>.
    • Hoist initial observed = state_.load(std::memory_order_acquire) loads outside for (;;) loops in CompleteRequest, WaitForRetirement, and WaitForBlock.

Verification

  • ctest (Debug/ON, Release/ON, Release/OFF, LIBXR_TEST_BUILD=ON): 9/10 passed (test_linux_can skipped without CAP_NET_ADMIN vcan), 0 failed; direct ./test/libxr_test --case write_port and --case write_stream passed in all 3 configs (including repeated write_port race stress runs).
  • LIBXR_SYSTEM=none (Debug and Release, xr target): built cleanly with 0 warnings.
  • tools/format_cpp_files.sh --check (clang-format 21.1.8) on touched files: passed.
  • tools/check_doxygen_warnings.sh (doxygen 1.12.0): 933 warnings (matches baseline 933).
  • git diff --check origin/dev..HEAD: clean.

@greptile-apps

greptile-apps Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

Fixes a false BUSY result during timed-out write completion and makes zero-length stream appends leave an unowned port untouched.

  • Fitting BLOCK writes continue when an old request retires.
  • Zero-length Stream inserts leave the port free when the stream has no ownership.
  • WriteQueue copies the full request remainder with a batch queue read.

No actionable issues found. Tests were not run during this review.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Blocking write fails to claim port] --> B{Observed BLOCK_DETACHED and size fits?}
  B -->|No| C[Return BUSY]
  B -->|Yes| D[Try to register retirement wait]
  D -->|Registered| E[Wait for backend handoff]
  D -->|Already retired| F[Try to claim IDLE directly]
  E --> G[Submit next write after successful handoff]
  F -->|Claimed| G
Loading

Reviews (1) · Last reviewed commit: "refactor(rw): use PopBatch in WriteQueue..." · Reviewed by Greptile

@Jiu-xiao
Jiu-xiao merged commit 326ae1d into dev Nov 19, 2026
8 checks passed
@Jiu-xiao
Jiu-xiao deleted the fix/writeport-20261119 branch November 19, 2026 08:28
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