Commit dd4e9c2a authored by Jimmy Lai's avatar Jimmy Lai

fix: decouple node tee branch end/drain handling

parent d670adf0
...@@ -55,9 +55,20 @@ export function teeNodeReadable( ...@@ -55,9 +55,20 @@ export function teeNodeReadable(
return states[0].pendingBytes + states[1].pendingBytes return states[0].pendingBytes + states[1].pendingBytes
} }
function areAllOpenBranchesBlocked(): boolean {
for (const state of states) {
if (state.closed) continue
if (!state.waitingDrain && state.pending.length === 0) {
return false
}
}
return true
}
function maybePauseSource() { function maybePauseSource() {
if ( if (
!source.isPaused() && !source.isPaused() &&
areAllOpenBranchesBlocked() &&
totalPendingBytes() >= MAX_NODE_TEE_PENDING_BYTES totalPendingBytes() >= MAX_NODE_TEE_PENDING_BYTES
) { ) {
source.pause() source.pause()
...@@ -68,7 +79,8 @@ export function teeNodeReadable( ...@@ -68,7 +79,8 @@ export function teeNodeReadable(
if ( if (
source.isPaused() && source.isPaused() &&
!sourceEnded && !sourceEnded &&
totalPendingBytes() < MAX_NODE_TEE_PENDING_BYTES / 2 (totalPendingBytes() < MAX_NODE_TEE_PENDING_BYTES / 2 ||
!areAllOpenBranchesBlocked())
) { ) {
source.resume() source.resume()
} }
...@@ -103,19 +115,38 @@ export function teeNodeReadable( ...@@ -103,19 +115,38 @@ export function teeNodeReadable(
} }
} }
function maybeEnd() { function maybeCleanupAfterSourceEnd() {
if (!sourceEnded || failed) return if (!sourceEnded || failed) return
for (const state of states) { for (const state of states) {
if (!state.closed && (state.waitingDrain || state.pending.length > 0)) { if (
return state.closed ||
state.stream.destroyed ||
state.stream.writableEnded
) {
continue
} }
return
} }
cleanup() cleanup()
for (const state of states) {
if (!state.closed && !state.stream.destroyed) {
state.stream.end()
} }
function maybeEndBranch(state: NodeTeeState) {
if (
failed ||
state.closed ||
state.stream.destroyed ||
state.stream.writableEnded
) {
return
} }
if (!sourceEnded) return
if (state.pending.length > 0) return
// Do not wait for drain here. A write() that returned false has already
// enqueued its chunk into the stream's internal buffer. Waiting for
// drain before ending can deadlock when this branch is intentionally
// consumed later than its sibling.
state.stream.end()
maybeCleanupAfterSourceEnd()
} }
function enqueue(state: NodeTeeState, chunk: NodeTeeChunk) { function enqueue(state: NodeTeeState, chunk: NodeTeeChunk) {
...@@ -146,15 +177,24 @@ export function teeNodeReadable( ...@@ -146,15 +177,24 @@ export function teeNodeReadable(
function flushPending(state: NodeTeeState) { function flushPending(state: NodeTeeState) {
if (state.closed || failed) return if (state.closed || failed) return
while (!state.waitingDrain && state.pending.length > 0) { while (state.pending.length > 0) {
if (!sourceEnded && state.waitingDrain) {
break
}
const chunk = state.pending.shift()! const chunk = state.pending.shift()!
state.pendingBytes -= chunkByteLength(chunk) state.pendingBytes -= chunkByteLength(chunk)
if (!state.stream.write(chunk)) { if (!state.stream.write(chunk)) {
state.waitingDrain = true state.waitingDrain = true
if (!sourceEnded) {
break
} }
} }
}
if (sourceEnded) {
maybeEndBranch(state)
return
}
maybeResumeSource() maybeResumeSource()
maybeEnd()
} }
const onSourceData = (chunk: NodeTeeChunk) => { const onSourceData = (chunk: NodeTeeChunk) => {
...@@ -168,7 +208,11 @@ export function teeNodeReadable( ...@@ -168,7 +208,11 @@ export function teeNodeReadable(
const onSourceEnd = () => { const onSourceEnd = () => {
runInContext(() => { runInContext(() => {
sourceEnded = true sourceEnded = true
maybeEnd() flushPending(states[0])
flushPending(states[1])
maybeEndBranch(states[0])
maybeEndBranch(states[1])
maybeCleanupAfterSourceEnd()
}) })
} }
const onSourceError = (error: Error) => { const onSourceError = (error: Error) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment