fix(media): bodies hold no file between reads — a GC-closed handle killed the relay (4.15.1)
FileMediaStore.body() kept one FileHandle for the whole response and closed it only at the end or on cancel. A player that seeks or closes mid-film often triggers neither; the handle then fell to the garbage collector, which Bun 1.4 treats as fatal. The Prism relay restarted five times this way. Each piece now opens and closes the file. Regression test runs in its own process and checks no descriptor is open between reads. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -9,3 +9,13 @@ describe('FileMediaStore', () => {
|
||||
await expect(store.totalBytes()).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('FileMediaStore bodies', () => {
|
||||
test('a body nobody finishes reading holds no file for the garbage collector to close', async () => {
|
||||
const proc = Bun.spawn(['bun', 'run', `${import.meta.dir}/fixtures/abandoned-body.ts`], { stdout: 'pipe', stderr: 'pipe' });
|
||||
const [out, err, code] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text(), proc.exited]);
|
||||
expect(err).not.toContain('closed during garbage collection');
|
||||
expect(out.trim()).toBe('survived');
|
||||
expect(code).toBe(0);
|
||||
}, 30_000);
|
||||
});
|
||||
|
||||
37
packages/shade-media/tests/fixtures/abandoned-body.ts
vendored
Normal file
37
packages/shade-media/tests/fixtures/abandoned-body.ts
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
// Run as its own process by file-store.test.ts: a body a client walks away
|
||||
// from must not leave anything for the garbage collector to close, because
|
||||
// Bun treats a FileHandle closed by GC as fatal and the relay dies with it.
|
||||
import { mkdirSync, mkdtempSync, readdirSync, readlinkSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { FileMediaStore } from '../../src/server.js';
|
||||
|
||||
const dir = mkdtempSync(join(tmpdir(), 'shade-abandoned-'));
|
||||
const id = 'ab'.repeat(16);
|
||||
mkdirSync(join(dir, 'objects', 'ab'), { recursive: true });
|
||||
writeFileSync(join(dir, 'objects', 'ab', `${id}.smf`), new Uint8Array(3 * 1024 * 1024));
|
||||
const store = new FileMediaStore(dir);
|
||||
|
||||
// Never read, read once and dropped: what a seek or a closed player leaves.
|
||||
for (let i = 0; i < 20; i++) {
|
||||
const body = await store.body(id, 0, 3 * 1024 * 1024);
|
||||
if (i % 2 === 1) await body.getReader().read();
|
||||
}
|
||||
// Between reads nothing may be open: a descriptor held here is one the
|
||||
// garbage collector closes later, which Bun 1.4 turns into a crash.
|
||||
const held = readdirSync('/proc/self/fd').filter((fd) => {
|
||||
try {
|
||||
return readlinkSync(`/proc/self/fd/${fd}`).endsWith(`${id}.smf`);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
if (held.length > 0) {
|
||||
console.log(`held ${held.length}`);
|
||||
process.exit(1);
|
||||
}
|
||||
for (let i = 0; i < 5; i++) {
|
||||
Bun.gc(true);
|
||||
await new Promise((r) => setTimeout(r, 20));
|
||||
}
|
||||
console.log('survived');
|
||||
Reference in New Issue
Block a user