GeezerVison is showing github.com — Fixed stream writes dropping data after short write (https://github.c… · zen-fs/core@ebb628a
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/node/streams.ts +16-2Lines changed: 16 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,6 +28,9 @@ export interface StreamOperations { | 28 | 28 | /** The default size of the chunks read by streams using `fs` operations, in bytes */ | 29 | 29 | const operationsChunkSize = 64 * 1024; | 30 | 30 |
| | 31 | +/** How many writes in a row may write nothing before a stream gives up */ | | 32 | +const maxWriteRetries = 5; | | 33 | + | 31 | 34 | /** | 32 | 35 | * Calls a callback-based operation, resolving with its result. | 33 | 36 | */ | @@ -109,8 +112,19 @@ export class OperationsHandle { | 109 | 112 |
| 110 | 113 | return new WritableStream({ | 111 | 114 | async write(chunk) { | 112 | | - const bytesWritten = await write(chunk); | 113 | | - if (position !== undefined) position += bytesWritten; | | 115 | + let retries = 0; | | 116 | + for (let data = chunk; data.byteLength;) { | | 117 | + const bytesWritten = await write(data).catch((error: NodeJS.ErrnoException) => { | | 118 | + if (error?.code != 'EAGAIN') throw error; | | 119 | + return 0; | | 120 | + }); | | 121 | + | | 122 | + retries = bytesWritten ? 0 : retries + 1; | | 123 | + if (retries > maxWriteRetries) throw Object.assign(new Error('write failed'), { code: 'ERR_SYSTEM_ERROR' }); | | 124 | + | | 125 | + if (position !== undefined) position += bytesWritten; | | 126 | + data = data.subarray(bytesWritten); | | 127 | + } | 114 | 128 | }, | 115 | 129 | }); | 116 | 130 | } | |
Collapse file Copy file name to clipboardExpand all lines: tests/common/streams.test.ts +35Lines changed: 35 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,4 +18,39 @@ suite('Streams', () => { | 18 | 18 | fs.unlinkSync('/hwm.bin'); | 19 | 19 | } | 20 | 20 | }); | | 21 | + | | 22 | + suite('fs option with short writes', () => { | | 23 | + type WriteCallback = (error: Error | null, bytesWritten: number, buffers?: Uint8Array[]) => void; | | 24 | + | | 25 | + const open = (_: unknown, flags: string, mode: number, callback: (error: Error | null, fd: number) => void) => | | 26 | + fs.open('/short.txt', flags, mode, callback as any); | | 27 | + | | 28 | + function finish(stream: fs.WriteStream, data: string): Promise<void> { | | 29 | + return new Promise((resolve, reject) => { | | 30 | + stream.on('error', reject); | | 31 | + stream.end(data, () => stream.on('close', resolve)); | | 32 | + }); | | 33 | + } | | 34 | + | | 35 | + test('writev resumes after the bytes that were written', async () => { | | 36 | + fs.writeFileSync('/short.txt', '0123456789'); | | 37 | + const writev = (fd: number, buffers: Uint8Array[], position: number | null, callback: WriteCallback) => { | | 38 | + const data = Buffer.concat(buffers); | | 39 | + fs.write(fd, data, 0, Math.min(data.length, 2), position ?? undefined, (error, bytesWritten = 0) => | | 40 | + callback(error, bytesWritten, buffers), | | 41 | + ); | | 42 | + }; | | 43 | + const stream = fs.createWriteStream('/short.txt', { flags: 'r+', start: 2, fs: { open, writev, close: fs.close } as any }); | | 44 | + stream.cork(); | | 45 | + stream.write('abc'); | | 46 | + await finish(stream, 'de'); | | 47 | + assert.equal(fs.readFileSync('/short.txt', 'utf8'), '01abcde789'); | | 48 | + }); | | 49 | + | | 50 | + test('fails after repeatedly writing nothing', async () => { | | 51 | + const write = (...args: unknown[]) => (args.at(-1) as WriteCallback)(null, 0); | | 52 | + const stream = fs.createWriteStream('/short.txt', { fs: { open, write, close: fs.close } }); | | 53 | + await assert.rejects(finish(stream, 'abc')); | | 54 | + }); | | 55 | + }); | 21 | 56 | }); |
|