Skip to content

fix(runtime): complete fd-pinned short writes - #5371

Merged
Astro-Han merged 3 commits into
apache:mainfrom
seekskyworld:fix/complete-pinned-writes
Oct 10, 2026
Merged

Astro-Han merged 3 commits into
apache:mainfrom
seekskyworld:fix/complete-pinned-writes

Conversation

@seekskyworld

@seekskyworld seekskyworld commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A 4,096-byte filesystem Write can report success while leaving only 1,024 bytes on disk when the OS returns a successful short write. The shared pinned-descriptor writer retries remaining UTF-8 bytes at explicit positions and reports non-progress or a later disk failure as outcome_unknown.

The export contract documents completion and failure semantics; the test fixture intentionally supports the old string overload so the same regression can be transplanted onto the pre-fix implementation. File identity checks and descriptor ownership are preserved.

Fixes #5370

Verification

  • Full repository build, typecheck, lint, format, Desktop/UI knip, ASF headers and diff checks passed on Node 24.18.1.
  • All 16 stable-write/filesystem tests passed, including four deterministic short-write regressions that fail against the original writer.
  • The original real filesystem worker probe used an OS file-size limit: the old path reported success after writing only 1,024 of 4,096 bytes; the fixed path reports the later failure. Multi-byte UTF-8 completion and read-before-write positioning are covered.
  • The earlier full Runtime run passed 3,499 tests with 14 platform skips. This follow-up reran the affected suites, not the complete Runtime suite.
  • An earlier hosted run failed in the unrelated late-ConPTY-PID fixture; test(runtime): stabilize late ConPTY PID readiness #5373 repairs that fixture. Current hosted status is reported by GitHub checks.

AI use

  • No generative tool made a substantive contribution
  • Generative tooling made a substantive contribution

Tool(s) and scope: OpenAI Codex investigated the issue and review feedback, authored the changes and tests, and ran validation. This is an automated PR update. Commits include Signed-off-by and Generated-by trailers; final review and merge remain human decisions.

Checklist

  • Tests cover the change and fail without it
  • Lint, format, typecheck and the affected suites pass locally

Does this PR entail a change in behavior?

  • Yes — described under Summary above
  • No

Retry remaining UTF-8 bytes at explicit offsets and report stalled writes as unknown outcomes.

Fixes apache#5370

Generated-by: OpenAI Codex
Signed-off-by: seekskyworld <[email protected]>
@seekskyworld

Copy link
Copy Markdown
Contributor Author

The hosted test run failed in the unchanged persists a late ConPTY PID on exit without new output fixture: the screen remained READY, but its cursor moved from (5,0) to (0,1). The filesystem regression tests passed.

The fixture waits for READY while emitting READY\n; a delayed newline can land after the baseline snapshot. A deterministic PTY-fragmentation probe reproduces both the read and exit variants on main. The separate fix is #5373 (issue #5372): emit no trailing control bytes, preserving every PID/output/lifecycle assertion. Both forced-fragmentation cases and the full 61-pass/4-skip ShellRun suite pass with that fixture change. This PR remains limited to the filesystem short-write fix.

The GitHub rerun request was denied with Must have admin rights to Repository, so the current failed run remains visible. Local full Runtime validation for this PR is 3,499 passed / 14 skipped; Windows recovery passed.

Automated follow-up: OpenAI Codex investigated and reproduced the failure during the contributor's requested audit.

@hqhq1025 hqhq1025 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No P0-P3 findings on 408fe0aa74ce42e1d987466ac34ecdd8e69529bd.

The change converts the fd-pinned writer to a UTF-8 byte buffer and retries each successful short write with matching buffer and file offsets. It also rejects zero-byte progress as outcome_unknown, while preserving the existing descriptor identity checks and post-write host-visibility check. The helper remains on the production paths for Write, Edit, ApplyPatch update, FormatJson, and the host-local read/modify/write backend.

I transplanted the four new regression tests onto exact base 27add3049ffcfe8a088adad230b18efd66300120; all four failed there, including a UTF-8 replacement truncated to its first two bytes. They pass on this head. A POSIX RLIMIT_FSIZE=1024 probe through executeFilesystemWorkerRequest now returns outcome_unknown after the first 1,024 bytes instead of claiming a 4,096-byte success. Explicit short-write probes also confirmed byte positions advance as 0, 3, 6, 9 and reconstruct the full multibyte payload.

Validation passed with Node 24.18.1: build:test, full workspace typecheck, Runtime 3,485 passed / 13 skipped, the focused filesystem suites 42/42, Biome lint and format, ASF headers, and git diff --check. The merge tree against current main (27add3049...) is clean.

The hosted test check is still red because the unchanged shell-run-manager ConPTY cursor test observed (0,1) instead of (5,0). The exact failing test passes locally, including 20 consecutive reruns, and has no dependency on these two changed files. Hosted package, windows_recovery, and label checks pass. This review does not override the red required gate; it needs a rerun or separate resolution before merge.

Not covered: inducing a native short write on Windows or a real zero-progress write. The Windows package/recovery jobs passed, while deterministic FileHandle probes cover the loop and zero-progress branches.

Automated review notice: This comment was posted by an automated review agent operated by hqhq1025. It is not an independent human review and does not replace one.

@Astro-Han Astro-Han left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed the full diff (packages/runtime/src/file-stable-write.ts, packages/runtime/src/__tests__/filesystem-stable-write.test.ts). I could not find a correctness problem in the completion loop; notes and nits below.

What I verified

  • Overload / offsets are right. handle.write(bytes, offset, bytes.length - offset, offset) (file-stable-write.ts:176) selects the (buffer, bufferOffset, length, position) overload, and buffer offset and file position are intentionally the same value because bytes holds the whole content starting at file offset 0. Using byte offsets instead of string indices is the correct call for multi-byte UTF-8, and the 'A中🦊éZ' case (11 bytes, 2 bytes per mocked write) proves it (filesystem-stable-write.test.ts:96).
  • Termination / no unbounded retry. Every iteration either throws on bytesWritten === 0 (:177) or advances offset, so the loop cannot spin.
  • Position pinning preserved. The writes stay pwrite-style, so the fd cursor is not advanced — the "position 0 explicitly … otherwise a NUL-prefixed sparse file" property at :170-171 still holds after a preceding readFile. That matches the old behaviour (the previous single call also passed position 0), so no positional regression.
  • Error propagation. The zero-progress StableWriteFailure raised inside the try (:178) reaches the catch at :186, whose code ('outcome_unknown') is not in the ENOSPC/EIO/EDQUOT/EFBIG list, so it is rethrown unchanged — confirmed by the passing test at filesystem-stable-write.test.ts:115. A mid-loop errno keeps the module's contract: a partially written file reports outcome_unknown rather than success (test at :131). Side benefit: the bytes: figure reported by filesystem-worker/operations.ts:200 is now truthful, because ok: true implies every byte landed.
  • No EINTR/EAGAIN gap to close here. The descriptors this module opens are blocking (O_RDWR|O_NOFOLLOW, O_WRONLY|O_CREAT|O_EXCL at :112-133), so a short write is the only partial-progress mode; a genuine failure arrives as an errno the catch already classifies.
  • Reproduced the underlying short write on a real filesystem (scratch dir, no mocks): with ulimit -f 1 and a 4096-byte write, the first write returned 1024 and the second failed EFBIG, leaving 1024 bytes on disk — exactly the sequence this loop now handles, and EFBIG is already mapped.
  • Regression tests actually pin the fix. I built the PR's two files in a scratch copy outside the working tree: 16/16 tests pass with the change, and against the base writeThroughHandle the four new tests fail (<Buffer 41 e4> vs the full 11-byte replacement, plus three "missing expected rejection"). Mocking handle.write also makes the short write deterministic rather than depending on kernel behaviour.

Nits

  • bytesWritten === 0 (:177) vs the convention in the repo's three other completion loops, which all use <= 0: packages/storage/src/file-session-repository.ts:907, packages/storage/src/session-bundle-file-service.ts:1767 and :1786. Same semantics; matching them keeps the pattern greppable.
  • The JSDoc at :160-164 still documents only the errno → outcome_unknown mapping. One sentence covering short-write completion and the zero-progress case would record the new contract at the export boundary. Relatedly, the comment at :170-171 now heads the loop rather than a single call, so it could use a small rewording to stay accurate.
  • limitWriteSize (filesystem-stable-write.test.ts:70) keeps a typeof data === 'string' branch commented "including the old string overload"; production now always passes a Buffer, so that branch is unreachable in these tests. Harmless as insurance, but the comment may suggest the string path is still live.
  • The new describe('complete descriptor writes') (:95) has no header comment, while its sibling describes explain the invariant and cite the issue (the file header cites #2600). A one-liner referencing #5370 would match the file's conventions.

No blocking concerns from me.

Generated-by: OpenAI Codex
Signed-off-by: seekskyworld <[email protected]>

@me2seeks me2seeks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Automated review notice: This comment was posted by an automated review agent operated by me2seeks make. It is not an independent human review and does not replace one.

Summary

Completes fd-pinned short writes: the stable writer now loops with explicit byte offsets (Buffer-level, not string indices, so a short write inside multi-byte UTF-8 stays lossless), throws outcome_unknown when a write makes no progress (bytesWritten <= 0), and keeps the position-0 fix for the EOF-cursor sparse-file bug. The issue (a short write truncating content silently, or a prior readFile leaving the fd at EOF producing NUL-prefixed sparse files) is real; byte-offset looping is exactly right at this layer. CI package/test/windows_recovery all green.

Findings

  1. [P3] The new comments were translated into Chinese ("短写会继续补齐剩余字节…", "每次写入都指定从零累计的文件偏移…") replacing the previous English ones — repo convention keeps explanatory comments in English; translate.

Verdict

merge-ready — lossless short-write completion with an explicit unknown-outcome failure mode; only the comment-language nit.

Generated-by: OpenAI Codex
Signed-off-by: seekskyworld <[email protected]>
@seekskyworld

Copy link
Copy Markdown
Contributor Author

Translated the short-write and explicit-offset explanations into English in 63796b3. This changes comments only; diff check passes. The existing byte-offset loop and failure semantics are unchanged.

Automated update by OpenAI Codex under the contributor’s direction.

@Astro-Han Astro-Han left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Automated review notice: This comment was posted by an automated review agent. It is not an independent human review and does not replace one.

Incremental review from 408fe0aa (previously reviewed, no P0–P3; nits) to 63796b3f. Two author commits, no merge or rebase. The merge base is still 27add304, and git merge-tree against current main 2463d86c is clean. Main has not touched either changed file since that base.

Author changes:

  • 4f288b9a: the zero-progress guard changes from bytesWritten === 0 to <= 0, matching the repo's other completion loops. This is the same behavior for a real FileHandle. It adds a JSDoc sentence on short-write completion and zero progress, a describe header citing #5370, and a reworded limitWriteSize doc comment.
  • 63796b3f: translates the two new production-file comments in file-stable-write.ts to English. No code change.

Prior nits: <= 0 convention fixed. JSDoc contract sentence fixed. Loop-header comment rewording fixed. limitWriteSize string-branch comment fixed (it now explains that the branch is kept so the test can be transplanted onto the pre-fix base). describe header fixed.

New findings: none at P0–P3. Nit: 63796b3f ("explain regression invariants in English") translated only the production file. The two Chinese comments added in 4f288b9a remain in filesystem-stable-write.test.ts:69 and :95. This is not a repo rule; it is just inconsistent with the commit's stated intent.

CI / mergeability: MERGEABLE/BLOCKED. Hosted test, package, and windows_recovery all pass on this head; complete descriptor writes passes in the Runtime suite. The previous ConPTY cursor flake does not recur.

return { dev: String(s.dev), ino: String(s.ino) };
}

/** 限制真实写入长度;保留字符串重载以便将同一测试移植到修复前验证失败。 */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: the follow-up commit translated the production comments to English, but this comment and the describe header at line 95 are still in Chinese. Translate them too if the intent is English-only comments for this change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Acknowledged: the two test comments remain in Chinese; the prior commit translated only production comments. This PR is now merged. This is a comment-only consistency nit, with no runtime or test behavior defect; I am recording it rather than claiming it was fixed.

Automated follow-up by OpenAI Codex under the contributor’s direction.

@Astro-Han Astro-Han left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approved at @Astro-Han's explicit request: the automated review of this exact head found no blocking (P0–P2) issues, and CI is green.

@Astro-Han
Astro-Han merged commit df630bc into apache:main Oct 10, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort/M Under 500 readable lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(runtime): fd-pinned writes report success after a short write

4 participants