reg-cli icon indicating copy to clipboard operation
reg-cli copied to clipboard

UnhandledRejection due to Unexpected end of input

Open sk- opened this issue 4 years ago • 6 comments

Describe the bug

When running reg-cli with a bunch of rendered images I get an UnhandledRejection error and the application freezes. I realized that empty pngs trigger this, which I was able to solve by first deleting those files. But it also seems that corrupt files also trigger the error. I'm not sure which file is triggering the error.

Reproduced step

Steps to reproduce the behavior:

  1. Add an empty png file to the source folder.

Expected behavior

Invalid image files should not crash or hand the process.

Actual behavior

(node:49896) UnhandledPromiseRejectionWarning: Error: Unexpected end of input
    at module.exports.ChunkStream._end (/usr/local/lib/node_modules/reg-cli/node_modules/pngjs/lib/chunkstream.js:92:24)
    at module.exports.ChunkStream.end (/usr/local/lib/node_modules/reg-cli/node_modules/pngjs/lib/chunkstream.js:81:10)
    at exports.PNG.PNG.end (/usr/local/lib/node_modules/reg-cli/node_modules/pngjs/lib/png.js:100:16)
    at ReadStream.onend (_stream_readable.js:677:10)
    at Object.onceWrapper (events.js:421:28)
    at ReadStream.emit (events.js:327:22)
    at endReadableNT (_stream_readable.js:1224:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:49896) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:49896) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

sk- avatar Feb 20 '21 18:02 sk-

I checked all the files with both file and identify -verbose and they all look like valid PNGs. My gut feeling is that one of those files is incomplete, but I have no way to know which file is the one causing the issue.

There is a reported issue for the pngjs dependency. It seems that the parser is too strict and does not recover from errors. See https://github.com/lukeapage/pngjs/issues/209.

sk- avatar Feb 20 '21 18:02 sk-

I think the issue may be here, because imgDiff and createDiff can be rejected.

https://github.com/reg-viz/reg-cli/blob/3f4935d1650b811da2dc241cc7ad92913da3335a/src/diff.js#L76-L78

sk- avatar Feb 20 '21 19:02 sk-

FWIW i had to use the following script to find the offending file:

const fs = require('fs');
const { PNG } = require('pngjs');

const filename = process.argv[2];
const png = new PNG();
fs.createReadStream(filename)
  .pipe(png)
  .on('parsed', () => {
    console.log(`Successfully parsed ${filename}`);
  })
  .on('error', (error) => {
    console.log(error);
    console.error(`Error parsing PNG-file: ${filename}!`);
  });

sk- avatar Feb 22 '21 13:02 sk-

@sk- Thanks!! If you are ok, could you please provide your invalid files to reproduce in my env.

bokuweb avatar Feb 22 '21 14:02 bokuweb

@bokuweb Unfortunately I don't have the files anymore. But It seemed like a truncated png file as when I opened it, it showed only half of it.

Note that an empty file with a .png extension will trigger the same issue.

sk- avatar Feb 22 '21 14:02 sk-

@sk- I see, I'll try it later. thanks.

bokuweb avatar Feb 22 '21 14:02 bokuweb