Skip to content
Open
Changes from all 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
21 changes: 18 additions & 3 deletions lib/CurlMimePart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,21 +424,35 @@ CurlMimePart.prototype.setDataStream = function (
): typeof CurlMimePart.prototype {
let streamEnded = false
let streamError: Error | null = null
let paused = false

// Defer unpause to the next event loop iteration to avoid calling
// curl_easy_pause() while libcurl is still processing the READFUNC_PAUSE
// return value from the read callback. Without this, the synchronous
// unpause can re-enter libcurl and cause a hang (observed on Linux).
const deferredUnpause = () => {
if (paused) {
paused = false
setImmediate(() => {
unpause()
})
}
}

const onReadable = () => {
unpause()
deferredUnpause()
}

const onEnd = () => {
streamEnded = true
unpause()
deferredUnpause()
cleanup()
}

const onError = (err: Error) => {
streamError = err
streamEnded = true
unpause()
deferredUnpause()
cleanup()
}

Expand Down Expand Up @@ -473,6 +487,7 @@ CurlMimePart.prototype.setDataStream = function (
if (streamEnded) {
return null
}
paused = true
return CurlReadFunc.Pause
}

Expand Down
Loading