Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/build/deps/webkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* for local mode. Override via `--webkit-version=<hash>` to test a branch.
* From https://github.com/oven-sh/WebKit releases.
*/
export const WEBKIT_VERSION = "1600131e46b5af48bbda3559af8d8a3327230b6e";
export const WEBKIT_VERSION = "5718a6ec579b98362ea7276a426deedcc6281ef5";

/**
* WebKit (JavaScriptCore) — the JS engine.
Expand Down
145 changes: 145 additions & 0 deletions test/js/bun/wasm/compile-rss-fixture.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions test/js/bun/wasm/compile-rss.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe, isASAN, isDebug } from "harness";
import path from "node:path";

// Unfixed, the 8 idle wasm compiler threads keep 35 to 39 MiB of freed compile temporaries until
// they exit after 10 s. Fixed, they release it about 100 ms after the last compile and RSS returns
// to where it started.
const idleTargetMiB = 10;

// Debug and ASAN builds link a JavaScriptCore that does not allocate through mimalloc, so the
// per-thread retention this test checks for does not exist there.
test.skipIf(isDebug || isASAN)(
"WebAssembly.compile does not retain memory in idle compiler threads (#41438)",
async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), path.join(import.meta.dir, "compile-rss-fixture.mjs"), String(idleTargetMiB)],
env: {
...bunEnv,
// The retained amount scales with the compiler thread count. Pin it so the test does not
// depend on the core count of the machine.
BUN_JSC_numberOfWasmCompilerThreads: "8",
},
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stderr).toBe("");
const result = JSON.parse(stdout.trim().split("\n").at(-1)!);
// The whole measurement is in the object so a failure prints it. The compiles have to grow RSS
// well past the target first, or the idle check means nothing.
expect({
...result,
grew: result.peakDeltaMiB > idleTargetMiB * 2,
released: result.idleDeltaMiB < idleTargetMiB,
}).toMatchObject({ grew: true, released: true });
expect(exitCode).toBe(0);
},
);
116 changes: 116 additions & 0 deletions test/js/web/atomics.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, test } from "bun:test";
import { bunEnv, bunExe, isASAN } from "harness";

describe("Atomics", () => {
describe("basic operations", () => {
Expand Down Expand Up @@ -307,3 +308,118 @@ describe("Atomics", () => {
});
});
});

// Free blocks inside pages that are still in use belong to the thread that owns the pages. They go back to the OS
// when that thread tells mimalloc that it is idle, which a wait that takes a while does.
test.skipIf(isASAN /* malloc is not mimalloc */)(
"Atomics.wait lets mimalloc release this thread's free memory",
async () => {
await using proc = Bun.spawn({
cmd: [
bunExe(),
"-e",
`
const { heapStats } = require("bun:jsc");
const purgeCalls = () => heapStats().mimalloc.purge_calls;
const spin = ms => { const start = performance.now(); while (performance.now() - start < ms); };

// the characters of these strings are allocated and freed by this thread
let strings = [];
for (let i = 0; i < 100000; i++) strings.push(Buffer.alloc(900 + (i % 5) * 8, 97).toString("latin1"));
// (far enough apart that whole OS pages are free in between, also where those are 16 KB)
strings = strings.filter((_, i) => i % 64 === 0);
Bun.gc(true);

// what needs no idle thread settles first, without going idle
let before = purgeCalls();
for (let stable = 0, tries = 0; stable < 3 && tries < 50; tries++) {
spin(60);
const now = purgeCalls();
stable = now === before ? stable + 1 : 0;
before = now;
}

const view = new Int32Array(new SharedArrayBuffer(4));
let released = 0;
for (let i = 0; i < 10 && released < 500; i++) {
if (Atomics.wait(view, 0, 0, 250) !== "timed-out") throw new Error("unexpected result");
released = purgeCalls() - before;
}
console.log(released >= 500, strings.length);
`,
],
env: bunEnv,
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect({ stdout, stderr, exitCode }).toEqual({ stdout: "true 1563\n", stderr: "", exitCode: 0 });
},
);
Comment thread
claude[bot] marked this conversation as resolved.
Resolved

// 100 ms into a wait the waiter drops the lock of the waiter list to release its memory, and takes it again.
test.skipIf(isASAN /* malloc is not mimalloc */)(
"a notify that arrives while Atomics.wait releases this thread's free memory is not lost",
async () => {
await using proc = Bun.spawn({
cmd: [
bunExe(),
"-e",
`
const view = new Int32Array(new SharedArrayBuffer(16));
const [VALUE, WAITING, LAST] = [0, 1, 2];
const worker = new Worker(
URL.createObjectURL(
new Blob(
[
\`
self.onmessage = event => {
const view = new Int32Array(event.data);
const results = [];
for (;;) {
// Free memory in between what is in use, so that the release has something to do and takes a while.
let strings = [];
for (let i = 0; i < 100000; i++) strings.push(Buffer.alloc(900 + (i % 5) * 8, 97).toString("latin1"));
strings = strings.filter((_, i) => i % 64 === 0);
Bun.gc(true);
Atomics.store(view, 1, 1);
// A waiter that misses its notification is off the list already: it sleeps for the whole
// timeout and then still answers "ok".
const start = performance.now();
const result = Atomics.wait(view, 0, 0, 10000);
results.push(performance.now() - start > 5000 ? "late" : result);
Atomics.store(view, 0, 0);
if (Atomics.load(view, 2)) return postMessage(results);
}
};
\`,
],
{ type: "application/javascript" },
),
),
);
worker.postMessage(view.buffer);
const results = new Promise(resolve => (worker.onmessage = event => resolve(event.data)));
const delays = [];
for (let delay = 100; delay <= 107; delay += 1) delays.push(delay);
for (const delay of delays) {
while (Atomics.load(view, WAITING) !== 1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 nit (optional): if the worker never reaches its first wait, maintainers get a 30 s hang with no diagnostic instead of a quick, named failure. The main thread spins without a bound in while (Atomics.load(view, WAITING) !== 1); at test/js/web/atomics.test.ts:405, and because it is spinning synchronously a worker error event can never be delivered, and no worker.onerror is set anyway. Fix: bound each spin with a deadline that throws a message naming the round, and wire worker.onerror to reject results, so a worker startup or runtime failure ends the subprocess with output the test can show rather than a timeout kill. [also at: test/js/web/atomics.test.ts:400 - nit: when the worker throws or never reaches its first wait, the parent spins forever and the test only dies by timeout with no diagnostic.]

Why this was flagged

The worker script in test/js/web/atomics.test.ts:369-396 is loaded from a blob URL inside a bun -e subprocess; if it fails to start or throws before Atomics.store(view, 1, 1) (for example a regression in blob-URL workers, Buffer in workers, or Bun.gc), the WAITING slot never becomes 1. The parent thread at test/js/web/atomics.test.ts:405 loops on Atomics.load(view, WAITING) !== 1 with no deadline and no await, so it never returns to the event loop and never sees the worker's error event; worker.onerror is not set either (only worker.onmessage at line 401). The subprocess therefore hangs until the explicit 30_000 ms test timeout at line 424 fires and await using proc kills it, so the failure shows as a bare timeout with empty stdout and stderr instead of the worker's error. REVIEW.md asks that every failure event be wired to reject the awaited promise and that conditions be polled within a bounded window; on the base branch this test does not exist, so this is a quality defect of the new test rather than a runtime regression.

Verification: nit — triggers only when the worker fails to start or throws before its first Atomics.store(view, 1, 1). test/js/web/atomics.test.ts:405 while (Atomics.load(view, WAITING) !== 1); is an unbounded synchronous spin; no worker.onerror. The main thread never yields from the spin, so the error task never runs and the test only dies at the 30_000 ms timeout (line 423) with no diagnostic.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving this one. A worker that never gets to its first wait still fails the test, by the timeout, and that would be a failure of workers in general that many other tests report first.

Atomics.store(view, WAITING, 0);
const start = performance.now();
while (performance.now() - start < delay);
if (delay === delays.at(-1)) Atomics.store(view, LAST, 1);
Atomics.store(view, VALUE, 1);
Atomics.notify(view, VALUE);
}
// ("not-equal" if the worker was held up for that long before it got to wait)
console.log(JSON.stringify((await results).filter(result => result !== "ok" && result !== "not-equal")), delays.length);
process.exit(0);
`,
],
env: bunEnv,
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect({ stdout, stderr, exitCode }).toEqual({ stdout: "[] 8\n", stderr: "", exitCode: 0 });
},
30_000,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
);
Loading