ReactiveX / IxJS

The Interactive Extensions for JavaScript
https://reactivex.io/IxJS/
MIT License
1.32k stars 73 forks source link

Spurious 'unhandledRejection' events #320

Closed jrvidal closed 3 years ago

jrvidal commented 3 years ago

IxJS version: 4.2.0

Code to reproduce:

const { batch, withAbort } = require("ix/asynciterable/operators");
const { AsyncSink } = require("ix/asynciterable");

process.on("unhandledRejection", (reason, promise) => {
  console.log("Unhandled Rejection at:", promise, "reason:", reason);
});

(async () => {
  const sink = new AsyncSink();

  let it = batch()(sink)[Symbol.asyncIterator]();

  const next = it.next();

  sink.error(new Error("sink error"));

  try {
    await next;
    throw new Error("Unexpected");
  } catch (e) {
    console.log("did throw");
  }
})().catch((e) => console.log("error handled", e));

Expected behavior: No "Unhandled rejection" message should be printed.

Actual behavior: An "Unhandled rejection" error gets printed:

did throw
Unhandled Rejection at: Promise {
  <rejected> Error: sink error
      at /testcase/index.js:15:14
      at Object.<anonymous> (/testcase/index.js:23:3)
      at Module._compile (internal/modules/cjs/loader.js:1063:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
      at Module.load (internal/modules/cjs/loader.js:928:32)
      at Function.Module._load (internal/modules/cjs/loader.js:769:14)
      at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
      at internal/main/run_main_module.js:17:47
} reason: Error: sink error
    at /testcase/index.js:15:14
    at Object.<anonymous> (/testcase/index.js:23:3)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47

Additional information:

I think the root cause is here, where a rejected promise gets created, but not immediately returned to some caller. The fix is simple, but I'm having a hard time reproducing it in a test.

trxcllnt commented 3 years ago

Fix released in ix@4.4.0.