Skip to content

Possible race condition in metadata() when using Stream-based input #3451

Closed

Description

@lucakcustom

Considering the following code

const image_path = "./Images/128x128.png"
const test = async() => {
    const image1 = sharp(image_path).pipe(sharp())
    const image2 = sharp(image_path).pipe(sharp())
    const data1 = await image1.metadata()
    console.log(data1)
    const data2 = await image2.metadata()  // on this instruction the node interpreter will exit
    console.log(data2)
}

When we await for the metadata of data2 the node interpreter will immediately exit without any error. I tried to debug it but I could not find the point in which the node interpreter is closed (I might assume this happens in native, but I'm not sure).

This issue never happens if we work on different images so the following code is totally fine and works as expected

const image_path = "./Images/128x128.png"
const image_path_2 = "./Images/256x256.png"

const test = async() => {
    const image1 = sharp(image_path).pipe(sharp())
    const image2 = sharp(image_path_2).pipe(sharp())
    const data1 = await image1.metadata()
    console.log(data1)
    const data2 = await image2.metadata()
    console.log(data2)
}

Activity

  1. added this to the v0.31.3 milestone on Nov 13, 2022
  2. lovell commented on Nov 13, 2022

    @lovell
    Owner

    Thanks for reporting, there was a possible race condition when awaiting metadata() for Stream-based input, especially when the input Stream was very short i.e. smaller images.

    Commit df97120 fixes this and adds a test that would previously have failed. This will be included as part of v0.31.3.

  3. changed the title [-]Piping twice the same image to sharp results in a sudden and undesired node exit[/-] [+]Possible race condition in metadata() when using Stream-based input[/+] on Nov 13, 2022
  4. lucakcustom commented on Nov 13, 2022

    @lucakcustom
    Author

    Thanks for your answer. Just out of curiosity, why was the interpreter exiting without returning any error? I would have expected some sort of error, even like a segfault, but I was getting nothing in all the environment I tried

  5. lovell commented on Nov 13, 2022

    @lovell
    Owner

    The "finish" event was already firing before the listener was attached, so the Promise returned by metadata() was never resolved or rejected.

    I assume you invoked your sample by adding a call to test() at the top level rather than await test(). This means that once all other logic in the script had completed, the event loop would have been empty and with no callbacks remaining, the process ended.

  6. lucakcustom commented on Nov 14, 2022

    @lucakcustom
    Author

    Now I see. Thanks again, I appreciated your answer.

  7. lovell commented on Dec 21, 2022

    @lovell
    Owner

    v0.31.3 now available, thanks again for reporting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions