When a server resource using node_version '22' is stopped or restarted (restart, ensure, stop then start), its V8 isolate is left behind, and the server can abort later with:
PostQueuedCompletionStatus: (6) The handle is invalid
(The text after (6) is the localized Windows message, for example Descripteur non valide on a French Windows.)
I found the cause and submitted a fix: fix(scripting/node): dispose isolate before freeing its uv loop by Firou91 · Pull Request #4294 · citizenfx/fivem · GitHub
This topic gives the details, a minimal repro and a workaround, so that other server owners who hit this crash can find it.
1. Client (production/canary) and FXServer version
- Client: N/A. This is a server-side bug, and no client needs to be connected to reproduce it.
- FXServer: Windows artifacts 35574 and 37579, and current
masterbuilt from source. Windows 11. Not tested on Linux.
2. What I expected to happen
Restarting a JS resource tears down its runtime cleanly. The server keeps running and memory returns to its previous level.
3. What actually happens
- Memory leak on every stop/restart: each stop or restart of a node22 resource leaks the whole isolate. Private memory grows by 10 to 26 MB per restart (324 → 515 MB over 20 restarts of the repro below).
- Delayed abort: some time later, the server aborts with the message above.
- On my server, the trigger is Prisma’s wasm query compiler (Prisma 6.19,
engineType = "client"). - The abort happens 9 to 17 minutes after
restart <resource>. - Sessions without a JS resource restart never crash.
- On my server, the trigger is Prisma’s wasm query compiler (Prisma 6.19,
- In-place reloads are fine: reloading code inside the same runtime (an
eval()-based hot reload) does not destroy the isolate and is not affected.
Every dump has the same stack, on a node platform worker thread:
Stack (server built from source, with symbols)
libuv!uv_fatal_error
libuv!uv_async_send
libnode22!node::PerIsolatePlatformData::PostTask
libnode22!v8::internal::wasm::WasmEngine::TriggerGC
libnode22!v8::internal::wasm::WasmEngine::AddPotentiallyDeadCode
libnode22!v8::internal::wasm::WasmCode::DecRefOnPotentiallyDeadCode
[...]
libnode22!v8::internal::wasm::`anonymous namespace'::BackgroundCompileJob::Run
libnode22!v8::platform::DefaultJobWorker::Run
libnode22!node::`anonymous namespace'::PlatformWorkerThread
4. Category of bug
Server, JS scripting runtime (citizen-scripting-node, Node 22).
5. Reproducible steps (with example script)
- Put the two files below in
resources/reproand runensure repro. The resource instantiates a wasm module and calls each of its functions once. - Run
restart repro3 times. - Run
set repro_hot 1, thenrestart reproonce more. The new instance calls the wasm functions in a hot loop. - The server aborts about 5 seconds later.
fxmanifest.lua
fx_version 'cerulean'
game 'common'
node_version '22'
server_script 'server.js'
server.js
// Every instance of this resource instantiates the same wasm bytes, so V8 shares
// one NativeModule between the current isolate and the isolates leaked by
// previous restarts. With `repro_hot 1`, the functions are made hot: dynamic
// tier-up replaces their Liftoff code, the dead code triggers a wasm code GC,
// and V8 posts a foreground task to every isolate using the module.
const FUNCTION_COUNT = 3000;
function uleb(n) {
const out = [];
do {
let byte = n & 0x7f;
n >>>= 7;
if (n !== 0) byte |= 0x80;
out.push(byte);
} while (n !== 0);
return out;
}
function section(id, content) {
return [id, ...uleb(content.length), ...content];
}
function buildModule() {
const types = [1, 0x60, 1, 0x7f, 1, 0x7f];
const funcs = [...uleb(FUNCTION_COUNT)];
const exports = [...uleb(FUNCTION_COUNT)];
const code = [...uleb(FUNCTION_COUNT)];
for (let i = 0; i < FUNCTION_COUNT; i++) {
funcs.push(0);
const name = [...Buffer.from(`f${i}`)];
exports.push(...uleb(name.length), ...name, 0x00, ...uleb(i));
const body = [
0x01, 0x01, 0x7f, // one i32 local (acc)
0x03, 0x40, // loop
0x20, 0x01, 0x41, 0x1f, 0x6c, // acc * 31
0x20, 0x00, 0x6a, // + n
0x41, i & 0x3f, 0x6a, // + K
0x21, 0x01, // acc =
0x20, 0x00, 0x41, 0x01, 0x6b, 0x22, 0x00, // n = n - 1
0x0d, 0x00, // br_if loop (n != 0)
0x0b, // end loop
0x20, 0x01, // return acc
0x0b,
];
code.push(...uleb(body.length), ...body);
}
return new Uint8Array([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
...section(1, types),
...section(3, funcs),
...section(7, exports),
...section(10, code),
]);
}
const hot = GetConvar('repro_hot', '0') === '1';
const instance = new WebAssembly.Instance(new WebAssembly.Module(buildModule()));
const fns = Array.from({ length: FUNCTION_COUNT }, (_, i) => instance.exports[`f${i}`]);
// Cold instances only run each function once (lazy Liftoff compilation).
let sink = 0;
for (const fn of fns) sink ^= fn(1);
let next = 0;
let rounds = 0;
if (hot) {
setInterval(() => {
for (let k = 0; k < 50; k++) {
sink ^= fns[next](200000);
next = (next + 1) % FUNCTION_COUNT;
if (next === 0) rounds++;
}
}, 0);
}
setInterval(() => console.log(`[repro] alive hot=${hot} rounds=${rounds} next=${next} sink=${sink}`), 5000);
console.log(`[repro] started hot=${hot}`);
Cause
NodeScriptRuntime::Destroy() frees the resource’s uv loop, but it never unregisters the isolate from the node platform and never disposes it. The platform keeps a handle on the freed loop. The next task that a V8 worker thread posts for that isolate calls uv_async_send() on freed memory, and libuv aborts once that memory has been reused.
The wasm code GC is a reliable trigger. Isolates that compiled the same wasm bytes share the compiled code. When the new instance makes that code hot, V8 posts a GC task to every isolate using it, including the leaked ones.
Fix
PR #4294 makes Destroy() follow node’s embedder teardown sequence: the isolate is disposed, and the platform releases its handles before the loop is closed. Only one file changes (NodeScriptRuntime.cpp). Results on a full server built from the branch:
| Scenario | master |
With the fix |
|---|---|---|
| 3 restarts, then a hot restart | aborts 5 s later | no crash after 5 min |
| Many restarts, then a hot restart | 20 restarts: 324 → 515 MB, then aborts | 2 x 100 restarts: 301-305 MB, no crash |
| 5 stop/start, 5 ensure, 5 restart (with open DB client sockets) | 706 MB | 311 MB |
| Restart time | 198-359 ms | 190-343 ms |
The PR’s CI build currently fails before compiling anything, because the Windows runner no longer has the MSBuild path hard-coded in the build script. The other recent PRs fail the same way, so the failure is unrelated to this change.
Workaround until it’s merged
Don’t stop or restart node22 resources on a running server. Restart the whole server instead (for example with a txAdmin restart), or reload the code in place if your resource supports it.
If someone from the team has time to review the PR, thanks in advance! I’m happy to give more details or test other scenarios.