Skip to content

ESM instrumentation/hooking fails with Node.js v18.19.0 #3784

Closed
@david-luna

Description

@david-luna

Out CI stated failing recently in the test action only for node v18 on a specific test script tests/instrumentation/fixtures/use-knex-pg.mjs.
https://github.com/elastic/apm-agent-nodejs/actions/runs/7161350478/job/19496836227

The error reveals that the script is ran twice and a racing condition occurs when creating the database in postgres. The reason behind this double load seem to be a backport (from v20) of the loading process of modules.
nodejs/node#44710

We can reproduce with a small project

// package.json
{
  "name": "iitm-double-load",
  "version": "1.0.0",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "import-in-the-middle": "^1.5.0"
  }
}
// file index.mjs
import { isMainThread } from 'worker_threads';

console.log('index module is loaded!!!', isMainThread);

You can see the module is loaded in the main and also in a worker thread

NODE_OPTIONS="--experimental-loader=import-in-the-middle/hook.mjs" NODE_NO_WARNINGS=1 node ./index.mjs
index module is loaded!!! false
index module is loaded!!! true

Metadata

Metadata

Assignees

Labels

agent-nodejsMake available for APM Agents project planning.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions