From 7598b46795cb461095962c5d3402eeb1dd40321b Mon Sep 17 00:00:00 2001 From: James Long Date: Thu, 3 Dec 2026 00:26:09 +0000 Subject: [PATCH] fix: drop the driver's test-only observerEntries hook; test pruning through behaviour observerEntries was a module global the Driver constructor overwrote with a closure over itself, so the last driver (its VM, globals and host context) stayed reachable after its run ended. It existed only for tests, so it is removed rather than made opt-in. The per-call pruning tests now check behaviour: WeakRefs to the ObservedCall each finished durable call's before hook saw are collected while the run is still alive, and a finished run is not retained after execute returns. Thanks to #197 / @kommander for finding the retention and for the regression test this adapts. --- src/runtime/driver.ts | 4 -- test/host-observe-promise.test.ts | 71 ++++++++++++++++++++--------- test/host-observe.test.ts | 75 +++++++++++++++++++++---------- 3 files changed, 102 insertions(+), 48 deletions(-) diff --git a/src/runtime/driver.ts b/src/runtime/driver.ts index 09b92fce..c8a7d1a7 100644 --- a/src/runtime/driver.ts +++ b/src/runtime/driver.ts @@ -43,9 +43,6 @@ const yieldToHost = (): Promise => /** For tests: `onPark` is called each time a run has nothing to do but wait on host calls in flight. */ export const parking: { onPark?: () => void } = {} -/** For tests: how many per-call observer entries the latest driver keeps (see `reported` and `observed`). */ -export const observerEntries: { last?: () => number } = {} - /** * Runs the VM (see `Vm`): the only place program execution meets the host. It calls `vm.run` until the program ends, * and does what each stop asks: yields to the event loop for a slice, and starts the host calls the VM queued, @@ -82,7 +79,6 @@ export class Driver { /** The run's opaque context, given to every host call. */ private readonly context: unknown, ) { - observerEntries.last = () => this.reported.size + this.observed.size } /** diff --git a/test/host-observe-promise.test.ts b/test/host-observe-promise.test.ts index 39ea08bc..bf28bb00 100644 --- a/test/host-observe-promise.test.ts +++ b/test/host-observe-promise.test.ts @@ -8,7 +8,6 @@ import { Interrupted, type Settled, } from "../src/runtime/contract.js" -import { observerEntries } from "../src/runtime/driver.js" // A hook that records its event, so the order of a call's life is visible. const record = (events: Array, event: string) => { @@ -20,6 +19,17 @@ const result = (value: unknown) => ? (value as { readonly status: string; readonly value?: unknown }) : undefined +// Forces full collections over a few turns until `gone` holds (Bun's collector scans the stack conservatively, so a +// stale slot or a timer the run left can keep an object a few more rounds). Returns whether it held. +const collect = async (gone: () => boolean) => { + for (let i = 0; i < 50; i++) { + await Bun.sleep(5) + Bun.gc(true) + if (gone()) return true + } + return gone() +} + describe("Host.observe for Fn (Promise API)", () => { test("with no hooks it is the function itself", () => { const fn = Host.fn(() => 1) @@ -1187,8 +1197,6 @@ describe("Host.observe for DurableFn (Promise API)", () => { await expect(aborted).rejects.toMatchObject({ name: "AbortError" }) expect(events).toEqual(["before", "wait", "after interrupted", "cancel"]) expect(events.filter((event) => event === "after interrupted")).toHaveLength(1) - // Wait reported, cancel stayed quiet, and the call's entries are gone once cancel ran. - expect(observerEntries.last?.()).toBe(0) }) test("a suspend during wait fires no after and no cancel", async () => { @@ -1255,8 +1263,10 @@ describe("Host.observe for DurableFn (Promise API)", () => { expect(events).toEqual(["before", "after success job 3", "before", "after failure hand nope"]) }) - test("many finished durable calls leave no per-call driver entries", async () => { - const sizes: Array = [] + test("the driver drops each finished durable call's observed state while the run goes on", async () => { + const refs: Array> = [] + const parked = Promise.withResolvers() + const release = Promise.withResolvers() const job = Host.durableFn({ start: () => "d", wait: () => 1, @@ -1264,28 +1274,47 @@ describe("Host.observe for DurableFn (Promise API)", () => { }) const runtime = Runtime.make({ globals: () => ({ - job: Host.observe(job, { after: () => {} }), + job: Host.observe(job, { + before: (call) => void refs.push(new WeakRef(call)), + after: () => {}, + }), plain: job, - probe: Host.fn(() => { - sizes.push(observerEntries.last?.() ?? -1) + gate: Host.fn(() => { + parked.resolve() + return release.promise }), }), }) - expect( - await runtime.execute( - [ - "let total = 0", - "for (let i = 0; i < 20; i++) { total += await job(); probe() }", - "for (let i = 0; i < 20; i++) { total += await plain(); probe() }", - "return total", - ].join("\n"), - ), - ).toMatchObject({ ok: true, value: 40 }) - expect(sizes).toHaveLength(40) - expect(sizes.every((size) => size === 0)).toBe(true) - expect(observerEntries.last?.()).toBe(0) + const execution = runtime.execute( + [ + "let total = 0", + "for (let i = 0; i < 20; i++) total += await job()", + "for (let i = 0; i < 20; i++) total += await plain()", + "return total + (await gate())", + ].join("\n"), + ) + await parked.promise + expect(refs).toHaveLength(20) + // The run is still alive, waiting on gate: only the driver could still hold the finished calls. + await collect(() => refs.every((ref) => ref.deref() === undefined)) + expect(refs.filter((ref) => ref.deref() !== undefined)).toHaveLength(0) + release.resolve(2) + expect(await execution).toMatchObject({ ok: true, value: 42 }) }) + test("a finished run is not retained after execute returns", async () => { + let ref: WeakRef | undefined + await (async () => { + const marker = { buf: new Uint8Array(1024 * 1024) } + ref = new WeakRef(marker) + const runtime = Runtime.make({ + globals: () => ({ ping: Host.fn(() => marker.buf.byteLength) }), + }) + expect(await runtime.execute("return ping()")).toMatchObject({ ok: true, value: 1024 * 1024 }) + })() + await collect(() => ref?.deref() === undefined) + expect(ref?.deref()).toBeUndefined() + }) test("observing a durable function does not change the snapshot's host identity", async () => { const hold = Host.durableFn({ start: () => "d", diff --git a/test/host-observe.test.ts b/test/host-observe.test.ts index 41492420..faa0cba1 100644 --- a/test/host-observe.test.ts +++ b/test/host-observe.test.ts @@ -2,7 +2,6 @@ import { describe, expect, test } from "bun:test" import { Context, Effect, Exit, Fiber } from "effect" import { Host, Runtime, toBytes } from "../src/effect/index.js" import { HostDefect, HostWork, Interrupted, type Settled } from "../src/runtime/contract.js" -import { observerEntries } from "../src/runtime/driver.js" const run = (effect: Effect.Effect) => Effect.runPromise(effect) @@ -14,6 +13,17 @@ const result = (value: unknown) => ? (value as { readonly status: string; readonly value?: unknown }) : undefined +// Forces full collections over a few turns until `gone` holds (Bun's collector scans the stack conservatively, so a +// stale slot or a timer the run left can keep an object a few more rounds). Returns whether it held. +const collect = async (gone: () => boolean) => { + for (let i = 0; i < 50; i++) { + await Bun.sleep(5) + Bun.gc(true) + if (gone()) return true + } + return gone() +} + describe("Host.observe for Fn", () => { test("with no hooks it is the function itself", () => { const fn = Host.fn(() => 1) @@ -1176,8 +1186,6 @@ describe("Host.observe for DurableFn", () => { ) expect(events).toEqual(["before", "wait", "after interrupted", "cancel"]) expect(events.filter((event) => event === "after interrupted")).toHaveLength(1) - // Wait reported, cancel stayed quiet, and the call's entries are gone once cancel ran. - expect(observerEntries.last?.()).toBe(0) }) test("a suspend during wait fires no after and no cancel", async () => { @@ -1237,8 +1245,10 @@ describe("Host.observe for DurableFn", () => { expect(events).toEqual(["before", "after success job 3", "before", "after failure hand nope"]) }) - test("many finished durable calls leave no per-call driver entries", async () => { - const sizes: Array = [] + test("the driver drops each finished durable call's observed state while the run goes on", async () => { + const refs: Array> = [] + const parked = Promise.withResolvers() + const release = Promise.withResolvers() const job = Host.durableFn({ start: () => Effect.succeed("d"), wait: () => Effect.succeed(1), @@ -1246,30 +1256,49 @@ describe("Host.observe for DurableFn", () => { }) const runtime = Runtime.make({ globals: () => ({ - job: Host.observe(job, { after: () => Effect.void }), + job: Host.observe(job, { + before: (call) => Effect.sync(() => void refs.push(new WeakRef(call))), + after: () => Effect.void, + }), plain: job, - probe: Host.fn(() => { - sizes.push(observerEntries.last?.() ?? -1) + gate: Host.fn(() => { + parked.resolve() + return Effect.promise(() => release.promise) }), }), }) - expect( - await run( - runtime.execute( - [ - "let total = 0", - "for (let i = 0; i < 20; i++) { total += await job(); probe() }", - "for (let i = 0; i < 20; i++) { total += await plain(); probe() }", - "return total", - ].join("\n"), - ), - ), - ).toMatchObject({ ok: true, value: 40 }) - expect(sizes).toHaveLength(40) - expect(sizes.every((size) => size === 0)).toBe(true) - expect(observerEntries.last?.()).toBe(0) + const execution = run( + runtime.execute( + [ + "let total = 0", + "for (let i = 0; i < 20; i++) total += await job()", + "for (let i = 0; i < 20; i++) total += await plain()", + "return total + (await gate())", + ].join("\n"), + ), + ) + await parked.promise + expect(refs).toHaveLength(20) + // The run is still alive, waiting on gate: only the driver could still hold the finished calls. + await collect(() => refs.every((ref) => ref.deref() === undefined)) + expect(refs.filter((ref) => ref.deref() !== undefined)).toHaveLength(0) + release.resolve(2) + expect(await execution).toMatchObject({ ok: true, value: 42 }) }) + test("a finished run is not retained after execute returns", async () => { + let ref: WeakRef | undefined + await (async () => { + const marker = { buf: new Uint8Array(1024 * 1024) } + ref = new WeakRef(marker) + const runtime = Runtime.make({ + globals: () => ({ ping: Host.fn(() => marker.buf.byteLength) }), + }) + expect(await run(runtime.execute("return ping()"))).toMatchObject({ ok: true, value: 1024 * 1024 }) + })() + await collect(() => ref?.deref() === undefined) + expect(ref?.deref()).toBeUndefined() + }) test("durable hooks and phases run with the run's services", async () => { class Counter extends Context.Service number }>()("Counter") {} const counter = () => {