Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
lucakcustom opened this issue Nov 11, 2022 · 5 comments
Closed

Comments

@lucakcustom
Copy link

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)
}
@lovell
Copy link
Owner

lovell commented Nov 13, 2022

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.

@lovell lovell 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 Nov 13, 2022
@lucakcustom
Copy link
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

@lovell
Copy link
Owner

lovell commented Nov 13, 2022

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.

@lucakcustom
Copy link
Author

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

@lovell
Copy link
Owner

lovell commented Dec 21, 2022

v0.31.3 now available, thanks again for reporting.

@lovell lovell closed this as completed Dec 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants