Prevent possible race condition when reading metadata #3451

This commit is contained in:
Lovell Fuller 2022-11-13 10:04:55 +00:00
parent 3a64a0529a
commit df971207b8
3 changed files with 25 additions and 2 deletions

View File

@ -4,6 +4,11 @@
Requires libvips v8.13.3
### v0.31.3 - TBD
* Prevent possible race condition awaiting metadata of Stream-based input.
[#3451](https://github.com/lovell/sharp/issues/3451)
### v0.31.2 - 4th November 2022
* Upgrade to libvips v8.13.3 for upstream bug fixes.

View File

@ -469,7 +469,7 @@ function metadata (callback) {
} else {
if (this._isStreamInput()) {
return new Promise((resolve, reject) => {
this.on('finish', () => {
const finished = () => {
this._flattenBufferIn();
sharp.metadata(this.options, (err, metadata) => {
if (err) {
@ -478,7 +478,12 @@ function metadata (callback) {
resolve(metadata);
}
});
});
};
if (this.writableFinished) {
finished();
} else {
this.once('finish', finished);
}
});
} else {
return new Promise((resolve, reject) => {

View File

@ -439,6 +439,19 @@ describe('Image metadata', function () {
);
});
it('Stream in, finish event fires before metadata is requested', (done) => {
const create = { width: 1, height: 1, channels: 3, background: 'red' };
const image1 = sharp({ create }).png().pipe(sharp());
const image2 = sharp({ create }).png().pipe(sharp());
process.nextTick(async () => {
const data1 = await image1.metadata();
assert.strictEqual('png', data1.format);
const data2 = await image2.metadata();
assert.strictEqual('png', data2.format);
done();
});
});
it('Stream', function (done) {
const readable = fs.createReadStream(fixtures.inputJpg);
const pipeline = sharp().metadata(function (err, metadata) {