fivem icon indicating copy to clipboard operation
fivem copied to clipboard

FXServer server-side JS awaits resume on ~50 ms scheduler cadence

Open kyro95 opened this issue 2 months ago • 1 comments

What happened?

Inside an FXServer JS resource, five sequential SELECT 1 calls each take ~50 ms:

Here's my profiler armonia_profile.json

Expected result

There is no script CPU block matching the 50 ms delay. The delay-per-await matches the 20 Hz server frame (1000/20 = 50 ms) exactly, suggesting V8 microtask/uv_run draining is coupled to ProcessServerFrame rather than the underlying I/O completion.

Reproduction steps

Minimal reproduction

script

import postgres from "postgres";

const sql = postgres(
  "postgres://armonia:[email protected]:5432/armonia",
  { max: 10 }
);

for (let i = 1; i <= 5; i++) {
  const start = performance.now();

  await sql`SELECT 1`;

  console.log(`ping ${i}: ${(performance.now() - start).toFixed(3)}ms`);
}

await sql.end();
  1. Start local PostgreSQL.
  2. Run the same postgres-js SELECT 1 loop in normal Node: ~1 ms warm.
  3. Run the same loop from an FXServer server-side JS resource.
  4. Observe ~50 ms per awaited query.

Importancy

Unknown

Area(s)

FiveM

Specific version(s)

tx:8.1.1fx:35245/Win - Node 22

Additional information

fivem js runtime

[      script:armonia] ping 1: 46.092ms
[      script:armonia] ping 2: 50.428ms
[      script:armonia] ping 3: 51.001ms
[      script:armonia] ping 4: 49.787ms
[      script:armonia] ping 5: 49.765ms

node runtime

ping 1: 23.617ms
ping 2: 1.021ms
ping 3: 1.230ms
ping 4: 0.833ms
ping 5: 0.767ms

bash

C:\Users\gioele>docker exec -it armonia-postgres psql -U armonia -d armonia -c "\timing on" -c "SELECT 1;"
Timing is on.
 ?column?
----------
        1
(1 row)

Time: 0.296 ms

kyro95 avatar Sep 09 '26 10:09 kyro95

Found the cause: timer.js turns setImmediate into a tick timer on the server, and postgres-js only sends a query inside a setImmediate, so every query waits for a server frame. Opened a fix: https://github.com/citizenfx/fivem/pull/4402

With it, the loop from your report goes from ~50 ms to ~1–2 ms per query (tested with postgres-js 3.4.5 on Windows and Linux).

OneEarJoe5 avatar Nov 14 '26 02:11 OneEarJoe5