Skip to content

Unhandled exception when updating to version 8.0.18 #577

@hcraT

Description

@hcraT

Hi,
first of all thanks for the project.
I would like to report an issue that I now traced back to the 8.0.18 version update. In particular to this commit.

This is not a bug per-se but the change produces a quite confusing error that took a couple of hours to identify. The error that I got after the update is reported below:

ERROR: Exiting because of unhandledRejection:
-- Details: ------------------------
UnhandledPromiseRejection: 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(). The promise rejected with the reason "true".
    at throwUnhandledRejectionsMode (node:internal/process/promises:392:7)
    at processPromiseRejections (node:internal/process/promises:475:17)
    at process.processTicksAndRejections (node:internal/process/task_queues:106:32) {
  code: 'ERR_UNHANDLED_REJECTION'
}

And it happens when a program using the library tries to connect to a TCP server but fails. And the culprit is a missing await of the newly promisified .close() method that was updated in the commit mentioned before.

Below is a small program to reproduce the "bug".

const modbus = require('modbus-serial');

const ip                    = '127.0.0.1';
const port                  = 50002;
const hasCommandLineOptions = process.argv.length > 2;

(async () => {
  console.log('trying to connect');
  let client = new modbus();
  try {
    await client.connectTCP(ip, {port});
    console.log('connected');
  } catch (e) {
    console.log(`Error connecting: ${e}`)
  }

  if (!hasCommandLineOptions) {
    console.log('Closing without awaiting');
    client.close();
  } else {
    console.log('Closing with awaiting and try/catch');
    try {
      await client.close();
    } catch (e) {
      console.log(`Error while closing: ${e}`)
    }
  }

})();

By saving this code in a file named test.js and running it using modbus-serial version 8.0.18 it will produce the issue. If version 8.0.17 is used instead the code will run just fine. If the program is run passing at least one option (like for example node test.js nobug) the program will run properly with the new version of the library.

The confusing aspect of this issue is the fact that a single boolean is thrown by the code (I think it originates from this line. So while the behavior is not wrong the single boolean made it a bit difficult to find what was wrong with the code.

Thank you very much.
Best regards!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions