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

@tus/server: do not emit chunkFinished on error #446

Merged
merged 2 commits into from
Jun 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion packages/server/src/models/StreamSplitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class StreamSplitter extends stream.Writable {
this.directory = directory
this.filenameTemplate = randomString(10)
this.part = 0
this.on('error', this._finishChunk.bind(this))
this.on('error', this._handleError.bind(this))
}

async _write(chunk: Buffer, _: BufferEncoding, callback: Callback) {
Expand Down Expand Up @@ -85,6 +85,19 @@ export class StreamSplitter extends stream.Writable {
this.currentChunkSize += chunk.length
}

async _handleError() {
// If there was an error, we want to stop allowing to write on disk as we cannot advance further.
// At this point the chunk might be incomplete advancing further might cause data loss.
// some scenarios where this might happen is if the disk is full or if we abort the stream midway.
if (this.fileHandle === null) {
return
}

await this.fileHandle.close()
this.currentChunkPath = null
this.fileHandle = null
}

async _finishChunk(): Promise<void> {
if (this.fileHandle === null) {
return
Expand All @@ -96,6 +109,7 @@ export class StreamSplitter extends stream.Writable {
path: this.currentChunkPath,
size: this.currentChunkSize,
})

this.currentChunkPath = null
this.fileHandle = null
this.currentChunkSize = 0
Expand Down