Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fs): WriteStream pending write fastpath #16856

Merged
merged 41 commits into from
Jan 31, 2025
Merged

Conversation

dylan-conway
Copy link
Member

@dylan-conway dylan-conway commented Jan 29, 2025

What does this PR do?

Fixes out-of-order writes when EAGAIN is received.

Closes #16823

How did you verify your code works?

Added a vite integration test

@robobun
Copy link

robobun commented Jan 29, 2025

Updated 4:23 AM PT - Jan 31st, 2025

@dylan-conway, your commit 2de9cc2 has 2 failures in Build #10908:


🧪   try this PR locally:

bunx bun-pr 16856

// TODO:
chunk_size: usize = 0,
// TODO: configurable?
const chunk_size: usize = 1024 * 4;
Copy link
Member

Choose a reason for hiding this comment

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

should be configurable, though it doesn't have to block this PR.

node:fs streams have this option. we can add an options argument into Bun.file().writer()

https://nodejs.org/api/fs.html#filehandlecreatewritestreamoptions

@dylan-conway dylan-conway marked this pull request as ready for review January 30, 2025 10:42
@@ -3841,6 +3891,7 @@ pub const FileSink = struct {
pub fn deinit(this: *FileSink) void {
this.pending.deinit();
this.writer.deinit();
AutoFlusher.unregisterDeferredMicrotaskWithType(@This(), this, JSC.VirtualMachine.get());
Copy link
Collaborator

Choose a reason for hiding this comment

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

This needs to not run when it's the shell

if (amt == 0) {
this.updateRef(false);
} else {
this.runPending();
Copy link
Collaborator

Choose a reason for hiding this comment

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

We want to run pending when:

  • done
  • If we wrote part of the data

Instead, we should runPending when !this.writer.hasPendingData()

If this leads to a timeout in tests, it's likely due to not draining microtasks since this is being called after the microtask queue is over. If the developer immediately calls write every time the write Promise fulfills, that could maybe lead to an infinite loop but we will have to see.

I think both of these branches if (amt == 0) and this one should be removed. And instead

@Jarred-Sumner Jarred-Sumner merged commit b098c9e into main Jan 31, 2025
66 of 69 checks passed
@Jarred-Sumner Jarred-Sumner deleted the dylan/fix-pending-write branch January 31, 2025 13:14
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.

Vite build fails on 1.2.1
4 participants