Skip to content

fix(fs): keep a descriptor from writing its bytes over a file renamed over its path or created after an unlink - #149

Merged
Project516 merged 1 commit into
masterfrom
p516/fix-stale-fd-overwrite
Dec 5, 2026
Merged

Project516 merged 1 commit into
masterfrom
p516/fix-stale-fd-overwrite

Conversation

@Project516

Copy link
Copy Markdown
Owner

What this changes

A descriptor no longer writes its buffered bytes over a file that has since replaced the one it opened. java-web buffers writes in the open descriptor and writes them out by path. On Linux the bytes go to the opened file (its inode), so when another file is renamed over the path, or the file is unlinked, the path keeps the new file, or none. Before this change, if two VMs in one page's worker shared the in-memory file system and one renamed a file over a path (the usual atomic save) while the other still held unwritten bytes for the old file, the other VM's next flush, sync or close overwrote the new file. Since #140 a cjFileBlob read does that flush too. An unlinked file was also created again by such a flush.

  • The in-memory backend gives each file an identity (Backend.identity) that it keeps across writes and renames.
  • A descriptor records the identity of the file it opened, and writes out only while its path still names that file. Descriptors that share a file's bytes in one VM also require the same identity.
  • Backends without file identity behave as before. Host files keep their inode handling.

Inside one VM a rename or unlink already moved or detached that VM's descriptors, so this only changes what one VM sees of another.

Tests

  • New test in test/fs.test.ts: a descriptor's bytes are written while its path names the same file (rewritten by path), and not after a file is renamed over the path or the file is removed. A rename keeps a file's identity. Without the check, the renamed-over file reads back the stale bytes.
  • Program tests compared with HotSpot: FileOps, FileStores, FileTimes, MappedFile, MappedFiles, OpenFileDescriptors, OpenFileInodes, PosixPermissions, StreamingFile, ZipStreams.
  • test/fs.test.ts, nio.test.ts, snapshot.test.ts pass. pnpm typecheck.

@coderabbitai

coderabbitai Bot commented Dec 5, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 2 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 2e370819-bf50-493e-88e0-dc56ff8e2ea3
Commits

Reviewing files that changed from the base of the PR and between 710ae77 and aaa102b.

Files selected for processing: 3

  • src/fs.ts
  • src/natives/io.ts
  • test/fs.test.ts
  • 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

@project516-review-bot project516-review-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The PR adds a file-identity check so an in-memory descriptor only flushes while its path still names the file it opened. Backend.identity and VFS.identity are added, MemoryBackend assigns stable ids to entries across writes, OpenFile.contents.ident is recorded at open and used in shared-descriptor matching, and flushFile skips the write when the path's current identity differs from the descriptor's. Backends without identity are unchanged. The logic is consistent across the rewrite, rename-over, and unlink cases, and the test exercises exactly those paths.


review-bot, model nvidia/nemotron-3.5-super-vl:free, verdict approve

Comment thread src/natives/io.ts Resolved
Comment thread src/fs.ts Resolved
@Project516
Project516 merged commit 0ccc505 into master Dec 5, 2026
13 checks passed
@Project516
Project516 deleted the p516/fix-stale-fd-overwrite branch December 5, 2026 02:12
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