Skip to content

Prevent hang when output is requested after Stream input has finished - #4584

Merged
lovell merged 1 commit into
lovell:mainfrom
Jaybhade:fix/stream-input-already-finished
Aug 13, 2026
Merged

Prevent hang when output is requested after Stream input has finished#4584
lovell merged 1 commit into
lovell:mainfrom
Jaybhade:fix/stream-input-already-finished

Conversation

@Jaybhade

@Jaybhade Jaybhade commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Every Stream-based input path waits for the Writable side to emit finish before flattening the accumulated chunks. finish is emitted only once, so any listener registered after it has already fired never runs — the Promise never settles and the callback is never called, with no error and no timeout.

const pipeline = sharp().resize(320, 240);
createReadStream(input).pipe(pipeline);
await once(pipeline, 'finish');   // or await pipelineAsync(readable, pipeline)

await pipeline.toBuffer();        // hangs forever

Measured against main, waiting 2s for each call on an instance whose Writable side has already finished:

call before after
toBuffer() hangs resolves
toBuffer(callback) hangs resolves
toFile(path) hangs resolves
metadata() resolves resolves
metadata(callback) hangs resolves
stats() hangs resolves
stats(callback) hangs resolves
clone() then output hangs resolves
.pipe(writable) resolves resolves

The two that already worked each carry their own workaround for this, added one entry point at a time: metadata() checks writableFinished (df97120, #3451) and Stream output re-emits finish when streamInFinished is set (d8df503, #671). Everything else was left waiting on an event that had gone.

Fix

Both ad-hoc guards are replaced with a single _whenStreamInFinished helper, used everywhere the input stream is awaited:

function _whenStreamInFinished (fn) {
  if (this.writableFinished) {
    fn();
  } else {
    this.once('finish', fn);
  }
}

Calling the handler directly means Stream output no longer has to re-emit finish, which also stops that event being delivered twice to any listener the caller has attached. With nothing left reading streamInFinished, its bookkeeping in _write goes too — and with it the finish listener that was attached to every Stream-fed instance.

An input stream that finishes without producing any data now rejects with the usual unsupported-format error rather than hanging, since writableFinished does not depend on a chunk having arrived.

No public API or type definitions change.

Tests

Eight cases in test/unit/io.js, one per entry point above, each requesting output only after finish has been awaited. Seven of them hang on main and are only reported as failures by the 60s suite timeout; the eighth (metadata() via Promise) passes both before and after, and is included so the path that had the original workaround stays covered.

npm test is otherwise unchanged: 1821 tests, 100% branch coverage retained.

Every Stream-based input path waits for the Writable side to emit
'finish' before flattening the accumulated chunks. 'finish' is emitted
only once, so any of these registered after it has already fired never
runs: the Promise never settles and the callback is never called, with
no error and no timeout.

    const pipeline = sharp().resize(320, 240);
    createReadStream(input).pipe(pipeline);
    await once(pipeline, 'finish');
    await pipeline.toBuffer(); // never resolves

This affected toBuffer, toFile, metadata via callback, stats and clone.
Two paths already worked around it in isolation: metadata via Promise
checked writableFinished, and Stream output re-emitted 'finish' when
streamInFinished was set.

Replace both ad-hoc guards with a shared _whenStreamInFinished helper
and use it everywhere the input stream is awaited. Re-emitting 'finish'
is no longer needed, so the streamInFinished bookkeeping in _write goes
too, along with the 'finish' listener it attached to every instance.
@lovell

lovell commented Aug 13, 2026

Copy link
Copy Markdown
Owner

This looks like a neat solution, thank you very much.

@lovell
lovell merged commit ea5bef2 into lovell:main Aug 13, 2026
32 checks passed
lovell added a commit that referenced this pull request Aug 25, 2026
dadezzz pushed a commit to dadezzz/university_notes that referenced this pull request Aug 30, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [sharp](https://sharp.pixelplumbing.com) ([source](https://github.com/lovell/sharp), [changelog](https://github.com/lovell/sharp/blob/main/docs/src/content/docs/changelog.md)) | [`0.35.3` → `0.35.4`](https://renovatebot.com/diffs/npm/sharp/0.35.3/0.35.4) | ![age](https://developer.mend.io/api/mc/badges/age/npm/sharp/0.35.4?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/sharp/0.35.3/0.35.4?slim=true) |

---

### Release Notes

<details>
<summary>lovell/sharp (sharp)</summary>

### [`v0.35.4`](https://github.com/lovell/sharp/releases/tag/v0.35.4)

[Compare Source](lovell/sharp@v0.35.3...v0.35.4)

<https://github.com/lovell/sharp-libvips/releases/tag/v1.3.3>

- Bound resize dimensions to coordinate limit.

- Bound composite left and top to coordinate limit.
  [#&#8203;4564](lovell/sharp#4564)
  [@&#8203;metsw24-max](https://github.com/metsw24-max)

- Round palette bit depth up for png and gif colours.
  [#&#8203;4569](lovell/sharp#4569)
  [@&#8203;metsw24-max](https://github.com/metsw24-max)

- Ensure tiff.subifd input option is used.
  [#&#8203;4572](lovell/sharp#4572)
  [@&#8203;metsw24-max](https://github.com/metsw24-max)

- Ensure `info.pages` is correct when limiting input page range.
  [#&#8203;4578](lovell/sharp#4578)
  [@&#8203;metsw24-max](https://github.com/metsw24-max)

- Improve support for input Streams finishing before output is requested.
  [#&#8203;4584](lovell/sharp#4584)
  [@&#8203;Jaybhade](https://github.com/Jaybhade)

</details>

---

### Configuration

📅 **Schedule**: (UTC)

- Branch creation
  - At any time (no schedule defined)
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate CLI](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0NC41MC4zIiwidXBkYXRlZEluVmVyIjoiNDQuNTAuMyIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants