Skip to content

Promise-based API never resolves on Node.js >= 26.4.0 (native callbacks invoked without a callback scope) #1082

Closed

Description

@albertopasqualetto

Summary

On Node.js 26.4.0 and later, awaiting any promise-returning ibm_db API (ibmdb.open(), db.query(), db.close(), Pool#open(), stmt.execute(), ...) can stall indefinitely. The native operation completes and the promise gets resolved, but the code after await does not run until some unrelated event-loop activity (a timer, I/O) happens. In a process with nothing else pending, for example a test, a CLI, or a server's startup connection, it never resumes and the call looks hung. The callback API is not affected.

The cause is in ibm_db: its native completion callbacks call into JavaScript without a callback scope. Until Node 26.3 an unrelated per-tick callback scope in Node core happened to drain the microtask queue on every loop iteration. Node 26.4.0 removed that scope as a performance optimization (nodejs/node#62969). That PR is labeled backport-requested-v24.x, so Node 24 LTS may start behaving the same way.

Environment

  • ibm_db 4.1.0 (latest), precompiled Windows binary from build.zip, default CLI driver (v12.1)
  • Windows 11 x64
  • Node.js 22.23.4 and 24.22.1 (work), 26.13.0 (stalls); bisected to 26.3.0 (works) / 26.4.0 (stalls)
  • Server: icr.io/db2_community/db2 (Db2 12.1.5) container

I could only test on Windows. The code paths involved are platform-independent.

Reproduction

Start a Db2 server, for example:

docker run -d --name db2 --privileged -p 50000:50000 \
  -e LICENSE=accept -e DB2INST1_PASSWORD=Passw0rd -e DBNAME=TESTDB \
  icr.io/db2_community/db2

repro.js:

const ibmdb = require('ibm_db');

const started = Date.now();
const log = (msg) => console.log(`[${String(Date.now() - started).padStart(5)} ms] ${msg}`);

// Safety net only. Without it nothing keeps the event loop alive while the
// continuation is stuck, and the process would exit before reaching it.
const safety = setTimeout(() => log('5 s safety timer fired'), 5000);

(async () => {
  log(`${process.version}, ibm_db ${require('ibm_db/package.json').version}`);
  const conn = await ibmdb.open(process.env.DB2_CONN);
  log('open() resolved');
  const rows = await conn.query('SELECT 1 AS X FROM SYSIBM.SYSDUMMY1');
  log(`query() resolved: ${JSON.stringify(rows)}`);
  await conn.close();
  log('close() resolved');
  clearTimeout(safety);
})();
DB2_CONN="DATABASE=TESTDB;HOSTNAME=127.0.0.1;PORT=50000;PROTOCOL=TCPIP;UID=db2inst1;PWD=Passw0rd;" node repro.js

Output

Node 22.23.4 and 24.22.1, as expected:

[    5 ms] v22.23.4, ibm_db 4.1.0
[  507 ms] open() resolved
[  792 ms] query() resolved: [{"X":1}]
[  800 ms] close() resolved

Node 26.13.0:

[    2 ms] v26.13.0, ibm_db 4.1.0
[ 5002 ms] 5 s safety timer fired
[ 5003 ms] open() resolved
[ 5016 ms] query() resolved: [{"X":1}]
[ 5023 ms] close() resolved

open() completes in a few hundred milliseconds, but its continuation only runs when the unrelated timer fires. Without the timer it never runs.

The later calls resolve quickly here only because console.log writes to stdout, and that write's completion callback drains the microtask queue as a side effect. This is why the problem can look intermittent: any unrelated I/O can unblock a stuck await. In our application, where nothing else was pending, the first await pool.open() hung forever. In a test that had started a transaction, the stuck session also kept holding its locks on the server.

Summary of runs

Node.js promise API callback API openSync()
22.23.4 ok (~450 ms) not tested not tested
24.22.1 ok (~450 ms) not tested not tested
26.3.0 ok (~990 ms) not tested not tested
26.4.0 stalls until another event not tested not tested
26.13.0 stalls until another event ok (~380 ms) ok (~350 ms)

Root cause

In ibm_db

Async operations are queued with raw uv_queue_work, for example src/odbc_connection.cpp:164:

uv_queue_work(uv_default_loop(), work_req, UV_Open, (uv_after_work_cb)UV_AfterOpen);

The after-work callback then calls the JavaScript callback directly, inside only a HandleScope (src/odbc_connection.cpp:201-218):

void ODBCConnection::UV_AfterOpen(uv_work_t *req, int status)
{
  open_connection_work_data *data = (open_connection_work_data *)(req->data);
  Napi::Env env(data->env);
  Napi::HandleScope scope(env);
  ...
  data->cb->Call({env.Null()});   // Napi::FunctionReference::Call -> napi_call_function

The same pattern appears at every completion site: cb->Call( occurs 49 times across odbc.cpp, odbc_connection.cpp, odbc_result.cpp and odbc_statement.cpp, and MakeCallback / callback scopes are never used. The promise API is built on top of these callbacks: createDeferred() (lib/odbc.js:65) and, for example, deferred.resolve(result) in Database.prototype.open (lib/odbc.js:354, :407).

The Node-API documentation for napi_make_callback says that napi_call_function is enough inside a napi_async_complete_callback (from napi_create_async_work), because Node has already set up the context. For custom async behavior that does not use napi_create_async_work, napi_make_callback may be required: it is what runs the process.nextTicks and promise reactions queued during the callback. Node uses MicrotasksPolicy::kExplicit, so promise reactions queued by deferred.resolve() inside such a call are only run when some callback scope closes.

Why it worked before Node 26.4

In Node ≤ 26.3 (and in 24.x up to at least 24.22.2), Environment::CheckImmediate runs on every loop iteration and calls RunAndClearNativeImmediates(). That function unconditionally opens an InternalCallbackScope, even when there is nothing to run, and closing it drains the microtask queue. That accidental per-iteration drain resolved the pending continuations.

nodejs/node#62969 ("src: fast path empty native immediate drain", released in 26.4.0) returns early from both functions when the queues are empty, before any scope is created. From then on, nothing drains the microtasks left behind by ibm_db's callbacks until an unrelated callback scope runs.

Supporting observations:

  • With an async_hooks trace, on 24.22.1 the continuation runs right after the native callback with no JS callback around it. On 26.13.0 it runs right after the unrelated Timeout.
  • Ordering two concurrent open()s against queueMicrotask and setImmediate: on 24.22.1 the microtask queued in the first callback runs before the next immediate, and on 26.13.0 after it, i.e. only once setImmediate's own callback scope runs.
  • Node core fixed the same class of bug in its own sqlite backup completion in 26.x: nodejs/node#65666, "run backup completion in callback scope... This drains promise reactions and next ticks before the event loop can become idle."

Suggested fix

Invoke the JavaScript callbacks from the UV_After* functions inside a callback scope, so Node drains the microtask queue when they return, for example:

  • create a Napi::AsyncContext when queuing the work and call data->cb->MakeCallback(recv, args, asyncContext) instead of data->cb->Call(args); or
  • open a Napi::CallbackScope (napi_open_callback_scope) around the existing Call; or
  • move the async operations to Napi::AsyncWorker / napi_create_async_work, whose completion callbacks already run in a proper scope.

The first two also give correct async_hooks / AsyncLocalStorage context propagation. I have not built a patched addon, because I had no C++ toolchain on the machine I tested on.

Workaround for users

Use the callback API and settle your own promise from a macrotask, which always runs inside a callback scope:

const viaCallback = (start) =>
  new Promise((resolve, reject) =>
    start((err, value) => setImmediate(() => (err ? reject(err) : resolve(value)))));

const conn = await viaCallback((done) => ibmdb.open(connStr, done));
const rows = await viaCallback((done) => conn.query(sql, params, done));
await viaCallback((done) => conn.close(done));

Calling the *Sync variants also avoids it, at the cost of blocking the event loop.

Activity

  1. bimalkjha commented on Nov 17, 2026

    @bimalkjha
    Member

    @albertopasqualetto This issue has been fixed in ibm_db@4.1.1. Please verify. Thanks.

  2. markddrake commented on Nov 17, 2026

    @markddrake

    Confirmed fixed in 4.1.1, thank you.

    We hit this in YADAMU on Node.js 26.4 and later. Db2 operations stalled whenever a timer was pending, for example while another driver's connection pool was open, and hung when none was. Db2 to SQL Server copies were the clearest case. With ibm_db 4.1.1 compiled from source on Node 26, our minimal reproductions no longer stall or hang. Our Db2 regression suites, including the Db2 <-> SQL Server round trips, pass with and without worker threads.

    YADAMU now requires ibm_db ^4.1.1. Tracked on our side as markddrake/YADAMU---Yet-Another-DAta-Migration-Utility#319.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions