Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Change of behaviour regarding execution of micro tasks since Node.js 26.4.0, maybe related to `mocha`, maybe related to NAN native modules

已关闭
#66,158 7 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
38/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
领域
backend

调研方向

Start with the supplied Mocha test and compare behavior between Node.js 26.3.0, 26.4.0, 26.9.0, and the latest checkout, including the Debug assertion failure. Read the NAN worker callback and .mocharc.json setup described in the report, then trace the microtask and nextTick execution path. Done means reproducing the race reliably, identifying the responsible Node.js behavior, and adding a regression test that passes without the manual checkpoint workaround.

由索引模型根据 Issue 内容生成。

描述

addons c++ promises
Version

v26.9.0

Platform
Happens on all tested platforms: Linux, Windows and macOS.
Subsystem

No response

What steps will reproduce the bug?

The bug is difficult to reproduce. It appears to be a race condition. The best I have is a mocha test that reproduces it 9 out of 10 times on all platforms. At the moment I haven't been able to reproduce it without a NAN native module and mocha, but it does not appear to be a NAN problem.

The problem can happen with https://github.com/mmomtchev/node-gdal-async, latest git version, built with node-pre-gyp.

This mocha test does it:

const gdal = require('..')
const { assert } = require('chai')
const process = require('node:process')

describe('test', () => {
  it('srsAsync', (done) => {
    const ds = gdal.open(`${__dirname}/data/dem_azimuth50_pa.img`)
    ds.srsAsync.then((srs) => {
      assert.isTrue(srs.toWKT().indexOf('PROJCS["WGS_1984_Albers"') > -1)
      done()
    }).catch((err) => done(err))
  })
})

The srsAsync getter simply resolves a raw v8 Promise using a class that inherits from the async worker in NAN. It does not call makeCallback or JavaScript at all:

template <class GDALType> void GDALPromiseWorker<GDALType>::HandleOKCallback() {
  Nan::HandleScope scope;
  auto context = Nan::GetCurrentContext();
  v8::Local<v8::Promise::Resolver> resolver = Nan::New(*resolver_handle);
  resolver->Resolve(context, this->ProduceRVal()).FromJust();
}

Adding a manual execution of the micro tasks queue with performCheckPoint:

Nan::GetCurrentContext()->GetMicrotaskQueue()->PerformCheckpoint(isolate);

in the HandleOKCallback solves the problem, however to the best of my knowledge, a native module is not supposed to do this and there are other more complex cases that are not covered by this workaround.

How often does it reproduce? Is there a required condition?

.mocharc.json has to include tsx - however this is only the trigger and the test file can very well be JavaScript.

What is the expected behavior? Why is that the expected behavior?

After returning from the native module, the micro tasks queue is run.

What do you see instead?

After returning from the native module, the micro tasks queue is not run, leading to Node.js being blocked on the event loop with pending Promise handlers and/or process.nextTick callbacks.

Additional information

The change appeared between Node.js 26.3.0 and Node.js 26.4.0 but the precise modification might be simply a trigger for the race condition.

It does also happen with the latest git checkout.

What is remarkable is that the latest git checkout built in Debug mode crashes with a failed assert while running the micro tasks queue on the same mocha test. I do not know if this is related.

Also simply adding

setInterval(() => console.log('.'), 10)

to any affected program solves the problem completely since the micro tasks are regularly run. If the timer runs every 5s, then you get 5s timeouts when this happens.

主要语言
JavaScript
星标
122k
派生
37.4k
平均合并
4 天 3 小时
30 天内合并 PR
279

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

nodejs/node 的其他 Issue

查看 nodejs/node 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。