lovell / lovell/sharp

Timeout not applying to encoding?

Open
#4,257 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
JavaScript
Stars
32.7k
Forks
1.4k
Avg merge
1d 14h
Merged PRs (30d)
5

Description

Question about an existing feature

The .timeout() method seems to only apply to certain operations, not the actual encoding of the image - is that right? Is there a way one could abort an ongoing transform/encode operation? If so, would it be possible to make the timeout apply to this step as well?

What are you trying to achieve?

I'd like to "give up" on processing/encoding images if they take longer than a given threshold. Without having been able to reproduce this consistently, it seems processing sometimes gets "stuck" on an image for a very long time, and I'd love to be able to simply drop the operation entirely - eg prevent it from using any more resources, and explicitly fail.

While I can set a timeout in user-land code and proceed as if it timed out, I cannot abort the actual encoding process, so it will continue taking CPU time until it eventually finishes/errors.

Additionally, it would be nice to be able to abort transformations, eg:

import sharp from 'sharp'

const abortController = new AbortController()
setTimeout(() => abortController.abort(), 1000)

try {
  const image = await sharp('input.jpg')
    .resize(1000, 1000)
    .toFormat('avif', {
      signal: abortController.signal
    })
    .toBuffer()
} catch (err) {
  console.log(err instanceof AbortError) // true
}
When you searched for similar issues, what did you find that might be related?
  • #3331 lead me to understand that it was the encoding step that does not respect the timeout
  • #2239 seems to have been raised prior to timeout() being added, but also calls out that there is no way to cancel/abort an ongoing process
Please provide a minimal, standalone code sample, without other dependencies, that demonstrates this question

Repository that illustrates the problem: https://github.com/rexxars/sharp-timeout-issue

Please provide sample image(s) that help explain this question

See repo above, but any sufficiently large image will do

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the existing .timeout() behavior and the encoding path reached by toFormat(...).toBuffer(), using the concerns linked from issues #3331 and #2239 as context. Done would require a decided approach for aborting or timing out encoding and transformations, with behavior verified for the AbortController example.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
backend, performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.