Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 48 additions & 24 deletions src/runtime/webcore/FileReader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ pub struct FileReader {
pub(crate) event_loop: Cell<EventLoopHandle>,
pub(crate) lazy: JsCell<Lazy>,
pub(crate) buffered: JsCell<Vec<u8>>,
/// A read error that arrived with no pending pull. The next `on_pull` returns it.
pub(crate) read_error: JsCell<Option<sys::Error>>,
/// Read-only after construction.
pub(crate) highwater_mark: usize,
pub(crate) flowing: Cell<bool>,
Expand Down Expand Up @@ -83,6 +85,7 @@ impl Default for FileReader {
event_loop: Cell::new(EventLoopHandle::init(core::ptr::null_mut())),
lazy: JsCell::new(Lazy::None),
buffered: JsCell::new(Vec::new()),
read_error: JsCell::new(None),
highwater_mark: 16384,
flowing: Cell::new(true),
sink: JsCell::new(SinkHandle::None),
Expand Down Expand Up @@ -779,7 +782,7 @@ impl FileReader {
// `drained` here — freeing `self.buffered` would be a no-op.
drop(drained);

if self.reader().is_done() {
if self.reader_finished() {
return streams::Result::IntoArrayAndDone(streams::IntoArray {
value: array,
len: drained_len as u64,
Expand All @@ -792,22 +795,22 @@ impl FileReader {
}
}

if self.reader().is_done() {
if self.reader_finished() {
return streams::Result::OwnedAndDone(drained);
} else {
return streams::Result::Owned(drained);
}
}

if self.reader().is_done() {
return streams::Result::Done;
return self.end_of_reader();
}

if !self.reader().has_pending_read() && self.flowing.get() {
// SAFETY: the reader cell is live for `self`'s lifetime; `read_into` is the raw re-entrancy-safe entry (EOF/error dispatch runs user JS).
let (amount_read, state) = unsafe { IOReader::read_into(self.reader.get(), buffer) };
bun_core::scoped_log!(FileReader, "onPull({}) = {}", buffer.len(), amount_read);
let done = state == ReadState::Eof || self.reader().is_done();
let done = state == ReadState::Eof || self.reader_finished();
if amount_read > 0 {
let into = streams::IntoArray {
value: array,
Expand All @@ -828,8 +831,8 @@ impl FileReader {
streams::Result::Owned(drained)
};
}
if done {
return streams::Result::Done;
if done || self.reader().is_done() {
return self.end_of_reader();
}
}

Expand Down Expand Up @@ -934,41 +937,62 @@ impl FileReader {
self.buffered.set(Vec::new());
}

// Pin across `sink.end()` / `p.run()`: they run user JS, which can
// reach on_reader_done and drop the across-read ref before the
// `waiting_for_on_reader_done` read below.
Comment thread
robobun marked this conversation as resolved.
Resolved
let parent = self.parent();
// SAFETY: see `parent()`.
unsafe { (*parent).increment_count() };

let sink = *self.sink.get();
if sink.is_some() {
self.sink.set(SinkHandle::None);
self.sink_paused.set(false);
sink.end(Some(streams::StreamError::Error(err)));
let parent = self.parent();
if self.waiting_for_on_reader_done.get() && !self.done.get() {
self.waiting_for_on_reader_done.set(false);
// SAFETY: see `parent()`.
let _ = unsafe { Source::decrement_count(parent) };
}
return;
} else if self.pending.get().state == streams::PendingState::Pending {
self.pending.with_mut(|p| {
p.result = streams::Result::Err(streams::StreamError::Error(err));
});
self.pending.with_mut(|p| p.run());
} else {
// `p.run()` would no-op and the pull promise would never settle.
self.read_error.set(Some(err));
}

self.pending.with_mut(|p| {
p.result = streams::Result::Err(streams::StreamError::Error(err));
});
// Pin across `p.run()`: it runs user JS, and anything there that
// reaches on_reader_done would drop the across-read ref and let a GC
// free this box before the `waiting_for_on_reader_done` read below.
let parent = self.parent();
// SAFETY: see `parent()`.
unsafe { (*parent).increment_count() };
self.pending.with_mut(|p| p.run());

if self.waiting_for_on_reader_done.get() && !self.done.get() {
self.waiting_for_on_reader_done.set(false);
// SAFETY: see `parent()`; the pin above keeps the count > 0.
let _ = unsafe { Source::decrement_count(parent) };
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
self.close_after_error();
// SAFETY: see `parent()`; the pin keeps the count >= 1, so this never
// frees. Tail call, `self` is not accessed after.
let _ = unsafe { Source::decrement_count(parent) };
}

/// An errored stream is never cancelled, so release the poll and the fd here.
fn close_after_error(&self) {
if self.done.get() {
return;
}
self.done.set(true);
self.reader().update_ref(false);
self.reader().deinit();
}

/// Done, with no stored read error left for one more pull to return.
fn reader_finished(&self) -> bool {
self.reader().is_done() && self.read_error.get().is_none()
}

/// The stored read error, or a clean end.
fn end_of_reader(&self) -> streams::Result {
match self.read_error.replace(None) {
Some(err) => streams::Result::Err(streams::StreamError::Error(err)),
None => streams::Result::Done,
}
}

pub(crate) fn set_raw_mode(&self, _flag: bool) -> sys::Result<()> {
#[cfg(not(windows))]
{
Expand Down
113 changes: 111 additions & 2 deletions test/js/web/streams/streams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
readableStreamToText,
} from "bun";
import { describe, expect, it, test } from "bun:test";
import { bunEnv, bunExe, isASAN, isDebug, isMacOS, isWindows, tempDir, tmpdirSync } from "harness";
import { bunEnv, bunExe, isASAN, isDebug, isLinux, isMacOS, isWindows, tempDir, tmpdirSync } from "harness";
import { mkfifo } from "mkfifo";
import { createReadStream, realpathSync, unlinkSync, writeFileSync } from "node:fs";
import { closeSync, createReadStream, openSync, realpathSync, unlinkSync, writeFileSync } from "node:fs";
import { join } from "node:path";

it("TransformStream", async () => {
Expand Down Expand Up @@ -1946,6 +1946,115 @@ it("Bun.file().stream() read text from large file", async () => {
}
});

// A POSIX file is read synchronously inside the stream's pull, so a failing
// read(2) arrives with no pending read to reject. Windows reads files through
// libuv, where the error always lands on a pending read.
describe.skipIf(isWindows)("Bun.file().stream() surfaces read() errors", () => {
// read(2) on /proc/self/mem fails with EIO: nothing is mapped at address 0.
const eioPath = "/proc/self/mem";
const itEIO = isLinux ? it : it.skip;

async function expectReadError(promise, code) {
const err = await promise.then(
() => null,
e => e,
);
expect(err).toBeInstanceOf(Error);
expect(err.code).toBe(code);
return err;
}

itEIO("for await rejects with the read error", async () => {
const chunks = [];
await expectReadError(
(async () => {
for await (const chunk of Bun.file(eioPath).stream()) chunks.push(chunk);
})(),
"EIO",
);
expect(chunks).toHaveLength(0);
});

itEIO("getReader().read() rejects with the read error", async () => {
await expectReadError(Bun.file(eioPath).stream().getReader().read(), "EIO");
});

itEIO("pipeTo() rejects with the read error", async () => {
await expectReadError(
Bun.file(eioPath)
.stream()
.pipeTo(new WritableStream({ write() {} })),
"EIO",
);
});

itEIO("Bun.file().text() reports the same read error", async () => {
const err = await expectReadError(Bun.file(eioPath).text(), "EIO");
expect(err.syscall).toBe("read");
});

it("a stream over a write-only fd rejects with EBADF", async () => {
using dir = tempDir("file-stream-read-error", { "x.bin": "hello" });
const fd = openSync(join(String(dir), "x.bin"), "w");
try {
await expectReadError(Bun.file(fd).stream().getReader().read(), "EBADF");
} finally {
closeSync(fd);
}
});

// A pollable fd (here a non-blocking pty master, as node-pty sets it up) is
// read when its poll fires. A read error must release that poll, or the
// process never exits. The slave hangup fails the master read with EIO on
// Linux and ends it on macOS.
it("a read error on a pollable fd releases the poll so the process can exit", async () => {
await using proc = Bun.spawn({
cmd: [
bunExe(),
"-e",
`
const { CString, dlopen } = require("bun:ffi");
const { closeSync, constants, openSync, writeSync } = require("node:fs");
const arch = process.arch === "x64" ? "x86_64" : process.arch === "arm64" ? "aarch64" : process.arch;
const candidates = process.platform === "darwin" ? ["libSystem.B.dylib"] : ["libc.so.6", "libc.musl-" + arch + ".so.1"];
let libc, lastError;
for (const lib of candidates) {
try {
libc = dlopen(lib, {
posix_openpt: { args: ["int"], returns: "int" },
grantpt: { args: ["int"], returns: "int" },
unlockpt: { args: ["int"], returns: "int" },
ptsname: { args: ["int"], returns: "ptr" },
}).symbols;
break;
} catch (err) {
lastError = err;
}
}
if (!libc) throw lastError;
const master = libc.posix_openpt(constants.O_RDWR | constants.O_NOCTTY | constants.O_NONBLOCK);
if (master < 0 || libc.grantpt(master) !== 0 || libc.unlockpt(master) !== 0) throw new Error("pty setup failed");
const slave = openSync(new CString(libc.ptsname(master)).toString(), constants.O_RDWR | constants.O_NOCTTY);
const reader = Bun.file(master).stream().getReader();
writeSync(slave, "x");
const first = await reader.read();
console.log("first", Buffer.from(first.value).toString());
// This read finds no data and waits on the poll. The hangup fires it.
const pending = reader.read();
closeSync(slave);
const result = await pending.then(r => (r.done ? "done" : "data"), e => e.code);
console.log("settled", result);
`,
],
env: bunEnv,
stderr: "pipe",
});
const [stdout, , exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stdout).toMatch(/^first x\nsettled (EIO|done)\n$/);
expect(exitCode).toBe(0);
});
});

it("fs.createReadStream(filename) should be able to break inside async loop", async () => {
for (let i = 0; i < 10; i++) {
const fileStream = createReadStream(join(import.meta.dir, "..", "fetch", "fixture.png"));
Expand Down
Loading