Skip to content
Source
Sign inSign up
TanStack/ai

[ai-harness] releaseIfIdle leaves the snapshot timer armed, which blocks Durable Object hibernation #2172

Send to my agent
Openethan-huo(GitHub profile, opens in a new tab) opened this issue 0 comments

Which project does this relate to?

@tanstack/ai-harness 0.3.0 (still present on main at e8546f9, packages/ai-harness/src/log.ts)

Describe the bug

SharedLog.releaseIfIdle() gives the log back (this.epoch = undefined; ownership.release(...)) but leaves the snapshot timer armed. The comment in startSnapshotTimer() already says a host that gave the log back writes no snapshot when the timer fires — so the timer can only ever be a no-op after release, yet it stays pending until snapshotAfterMs elapses.

In Node this is harmless because the timer is unref()'d. In runtimes without unref semantics it is not: on Cloudflare Durable Objects a pending setTimeout keeps the object resident, so an idle thread whose log was released cannot hibernate until the snapshot timer fires (and the host pays duration for that window). Any host that relies on "idle ⇒ no pending timers" is affected.

Steps to reproduce

  1. Host a harness in a Durable Object (or any runtime where pending timers keep the instance alive) with a SQLite-backed LogStore.
  2. Run one chat turn so records are appended after the last snapshot (firstUnsnapshottedAt set, timer started).
  3. Let the turn finish; releaseIfIdle() runs and releases ownership.
  4. Inspect pending timers: the snapshot timer is still scheduled for snapshotAfterMs - since.

Expected behavior

Releasing the log also stops the snapshot timer — the next owner writes the snapshot at its next write, as the existing comment describes.

Suggested fix

   private async releaseIfIdle() {
     const { epoch } = this
     const isBusy = [...this.views.values()].some((view) => view.hooks.isBusy())
     if (epoch === undefined || isBusy || this.pending.length > 0) return
     this.epoch = undefined
+    this.stopSnapshotTimer()
     await this.ownership.release(this.logId, epoch)
   }

We run this as a local patch on 0.3.0 and the object hibernates between turns with it.

@tanstack/ai-event-client starts the devtools EventClient connect loop (setInterval every 300 ms, 5 retries) on the first emitted event in server runtimes too. It gives up after ~1.5 s so the impact is small, but it is another timer per isolate wake in a Durable Object; an opt-out (or not connecting when there is no devtools target) would help.

  1. tanstack-source added labelsbug

  2. github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer

Sign in to join this conversation.