nodeca / probe-image-size

Get image size without full download. Supported image types: JPG, GIF, PNG, WebP, BMP, TIFF, SVG, PSD, ICO.
MIT License
978 stars 77 forks source link

MaxListenersExceededWarning with fs.createReadStream #78

Open jmikrut opened 1 year ago

jmikrut commented 1 year ago

Hi there and thank you for this package!

I am seeing a MaxListenersExceededWarning while using createReadStream and probe-image-size like follows:

    const readStream = fs.createReadStream(filePath);
    const result = await probeImageSize(readStream);

It seems to be coming from this package. Here is the full error with node --trace-warnings:

(node:74217) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 end listeners added to [PassThrough]. Use emitter.setMaxListeners() to increase limit
    at _addListener (node:events:587:17)
    at PassThrough.addListener (node:events:605:10)
    at PassThrough.Readable.on (node:internal/streams/readable:887:35)
    at eos (node:internal/streams/end-of-stream:196:10)
    at pipe (node:internal/streams/pipeline:392:10)
    at pipelineImpl (node:internal/streams/pipeline:323:25)
    at pipeline (node:internal/streams/pipeline:151:10)
    at probeStream (/MY-PROJECT-PATH/node_modules/probe-image-size/stream.js:63:8)
    at Object.get_image_size [as default] (/MY-PROJECT-PATH/node_modules/probe-image-size/index.js:13:12)

I've adjusted the code to not use a createReadStream and instead read the full file to memory first as follows:

    const data = fs.readFileSync(filePath)
    const result = await probe.sync(data)

Issue goes away. Is this indeed caused by something within probe-image-size? It appears so on my end but can't figure out how to resolve / diagnose further.

One other thing I should note is that we did try to destroy and close the readStream after having used it. For example:

    const readStream = fs.createReadStream(filePath);
    const result = await probeImageSize(readStream);
    readStream.destroy() // also tried `close()`

But this does not fix the warning either.

Any ideas?

rlidwka commented 1 year ago

Didn't I fix this in https://github.com/nodeca/probe-image-size/commit/a6da059439614c399b79c09ea1d406a3e4300b27? Maybe you're using old version (which may happen because of some other dependency).

Or the error might've reappeared elsewhere. In this case, please provide full example that we can run as is.