Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(loop-lag-cpu-clock): block with Bun.sleepSync; Atomics.wait purges the allocator on the thread
garrytanda250c8e1084
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e1b4533cdb..042c731eaa 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -30,7 +30,7 @@ identifiers and attribution are available in the pre-removal Git revision- Backup restore: `extractPgliteDump` no longer fsyncs each file; `syncRestoredTree` still flushes every restored file and directory before the ready receipt.- `scripts/ci-dependency-audit.sh`: advisories require bun's advisory report; registry and network errors retry within `AUDIT_DEADLINE_S` (420 s) and report `audit_registry_unavailable`.- `scripts/ci-reuse.ts` `attemptJobs` (shared by the release gate): a 5xx that outlasts the client's retries restarts the listing at 30 per page.-- Tests: the sync-entry-faults Postgres suite clones one migrated template per file (58 s to 32 s locally); the loop-lag clock probe blocks with `Atomics.wait` instead of a spawned child, after a full collection so collector work the engine owes the thread cannot land in the reading; the write-gate perf check judges a round quiet where its p95 can see load.+- Tests: the sync-entry-faults Postgres suite clones one migrated template per file (58 s to 32 s locally); the loop-lag clock probe blocks with `Bun.sleepSync` instead of a spawned child (spawn setup ran on the thread) or `Atomics.wait` (the allocator purges freed pages on the waiting thread); the write-gate perf check judges a round quiet where its p95 can see load.## [0.60.366.0] - 2026-10-22diff --git a/test/loop-lag-cpu-clock.test.ts b/test/loop-lag-cpu-clock.test.tsindex a8035d96e0..29360f3b16 100644--- a/test/loop-lag-cpu-clock.test.ts+++ b/test/loop-lag-cpu-clock.test.ts@@ -10,17 +10,16 @@* not move. The CPU clock still sees a stretch of work, and two stretches in one turn, the regression the bounds exist to* catch.*- * The block is an Atomics.wait, not a synchronous child: a spawn's own setup runs on the thread (1-4 ms, about 20 ms for a- * process's first spawn), and on a VM that charges host steal to the running thread those few ms read 30 ms once against- * the 25 ms bound (#7231, full-suite). That is the spawn's work, not the off-CPU time this test is about.- *- * The block settles the engine's heap first. A wait hands the heap over, and JavaScriptCore runs collector work it owes- * the thread at that handoff: the last marking phase of a collection in flight (the thread wakes the marker threads and- * waits on them before it parks) and, on waking, the finalize phase that returns emptied blocks with madvise. A shard- * process carries the heap of every earlier test file, so a collection can end just before the block: 37 ms read once- * against the 25 ms bound (#7304, full-suite), and under strace one such block made 14,488 system calls after its- * timeout, nearly all madvise. With a 6M-object heap and 1.5M objects dropped before each of 40 blocks, 3-6 blocks per run read 26-56 ms;- * a full collection and a 1 ms block before the reading leave at most 2.6 ms (4 runs each).+ * The block is Bun.sleepSync, a bare clock_nanosleep. Two earlier blocks charged their own work to the thread:+ * - A synchronous child runs the spawn's setup on the thread (1-4 ms, about 20 ms for a process's first spawn), and on a+ * VM that charges host steal to the running thread those few ms read 30 ms once against the 25 ms bound (#7231).+ * - Atomics.wait runs the allocator's idle work on the waiting thread: JavaScriptCore's waitSyncImpl calls mimalloc's+ * _mi_thread_idle_work, which purges freed page holes with madvise. A shard process carries the heap of every earlier+ * test file, so the 150 ms wait read 37 and 186 ms of thread CPU on #7304's full-suite (shard 4/22) and 79-111 ms+ * locally on that shard's first 47 files, where a symbolized strace showed 4,024 madvise calls from+ * mi_page_purge_holes_now < _mi_thread_idle_work < waitSyncImpl < atomicsFuncWait.+ * With a 6M-object heap and 1.5M objects dropped before each of 40 blocks, Atomics.wait read up to 108 ms and+ * Bun.sleepSync at most 0.3 ms.*/import { expect, test } from 'bun:test';import { execFileSync } from 'node:child_process';@@ -30,13 +29,10 @@ import { threadCpuNow } from './helpers/cpu-time.ts';/** The bound the loop-lag tests hold. */const MAX_LAG_MS = 100;const turn = () => new Promise<void>(resolve => setTimeout(resolve, 20));-const block = (ms: number) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);/** Blocks the thread off the CPU for `ms`; returns the thread CPU the block cost. */const offCpu = (ms: number) => {- Bun.gc(true);- block(1);const start = threadCpuNow();- expect(block(ms)).toBe('timed-out');+ Bun.sleepSync(ms);return threadCpuNow() - start;};