Skip to content

STOR-5760: Run onHibernate handlers in the local server - #8150

Merged
apeacock1991 merged 1 commit into
mainfrom
apeacock/do-on-hibernate-w3-server
Nov 3, 2026
Merged

apeacock1991 merged 1 commit into
mainfrom
apeacock/do-on-hibernate-w3-server

Conversation

@apeacock1991

Copy link
Copy Markdown
Contributor

Before workerd's server shuts down an idle root Durable Object, or unsafe.evict() evicts one, it now runs the object's onHibernate handler. The handler is still behind the experimental flag.

  • The container owns the run, so canceling the eviction call or the idle timer doesn't cancel the handler. Requests and evictions that arrive meanwhile wait for it, then reach a new instance.
  • If the run fails, for example on a failed flush, the object breaks and the waiting requests get that error.
  • Aborting the object or draining a cluster node cancels the handler. A node whose lease has lapsed doesn't start one.

The handler gets no tracer and no self token yet, so its events don't reach tail workers and ctx.restore() fails inside it.

Tests: three server-test cases (the idle path, an eviction whose caller goes away, an abort during the handler) and a cluster-test drain case.

@apeacock1991
apeacock1991 requested review from a team as code owners November 2, 2026 15:24

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 84d8ee90.

This review, of 605d62f3

💬 Approved with comments — The retirement design holds up: the container owns the handler run, waiters get the shared result, and abort and drain cancel it. What remains is a call-order nit, a self-call deadlock in an unusual edge case, and documentation suggestions, all INFO or SUGGESTION.

Findings: 3 info · 1 suggestion

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: 💬 2 info

Retirement ownership, cancellation and waiter handling look correct. Two minor points.

Recommendation: 💬 Optional Nits

  • [INFO] try_evict calls actor_has_on_hibernate_handler before checking actor_is_shared, but the C++ contract allows that call only while the actor is idle. It is harmless today; checking actor_is_shared first would make the precondition hold. (src/workerd/server/server/actor.rs:1331)
  • [INFO] An onHibernate handler that awaits a request to its own object waits on its own retirement and hangs until the object is aborted or the node drains. Consider documenting this or failing such requests. (src/workerd/server/server/actor.rs:929)
View full review

1. Handler probe runs before the idle check (src/workerd/server/server/actor.rs:1329-1335). Worker::Actor::hasOnHibernateHandler() reads state that requests modify, and its documentation says to call it only while the actor is idle. try_evict calls ffi::actor_has_on_hibernate_handler(&actor) before ffi::actor_is_shared(&actor), and evict_when_idle polls try_evict while requests are still in flight. Today this is harmless because a busy actor is shared, so the call returns false either way. Checking actor_is_shared first would make the C++ precondition hold literally.

2. A handler that calls its own object can deadlock (actor.rs:925-935, 1043-1049). While a retirement is committed, get_actor and start_request make every new request wait on it. If an onHibernate handler awaits a request to its own object (for example env.NS.get(sameId).fetch()), that request waits for the retirement, and the retirement waits for the handler. The cycle ends only on abort or drain. The CPU limit doesn't catch it because an idle await uses no CPU. Handlers that do this are unusual, but a line in the retirement doc comment, or failing requests that originate from the hook, would make the behaviour explicit.


📖 AGENTS.md: 💬 1 suggestion

The server AGENTS.md could describe the new retirement lifecycle.

Recommendation: 💬 Optional Nits

  • [SUGGESTION] Describe the onHibernate retirement lifecycle and its cancellation rules in the actor.rs entry of src/workerd/server/AGENTS.md.
View full review

This change introduces container-owned retirement in actor.rs: once a root's hook starts, eviction callers can go away without canceling it, and concurrent evictions wait for its shared result. Adding these semantics to the actor.rs entry in src/workerd/server/AGENTS.md would help future changes keep them intact.


📝 Documentation: 💬 1 info

The new local-server onHibernate behaviour is not documented.

Recommendation: 💬 Optional Nits

  • [INFO] Document that idle shutdown and unsafe.evict() in the local server now run onHibernate, including how pending requests and handler failures are handled.
View full review

src/workerd/server/server/actor.rs:1321-1353 now runs onHibernate on idle shutdown and on unsafe.evict(). No documentation yet says when the handler runs, that requests arriving meanwhile wait for it, or that a failed run breaks the object. The feature is still experimental, so this can wait until it stabilises.


✅ All other checks passed

🔒 Security: ✅ LGTM

No security concerns. The change affects the local server's actor lifecycle only and adds no new trust boundary, secrets handling or input parsing.


⚡ Performance: ✅ LGTM

The new path runs only when a root Durable Object with an onHibernate handler is evicted or goes idle. The per-request cost is one RefCell borrow and clone of retirement, and retire_actor clears the slot when the run finishes, so nothing grows without bound.


Reviewed commit: 605d62f3

Before workerd's server shuts down an idle root Durable Object, or
unsafe.evict() evicts one, it now runs the object's onHibernate
handler, if it has one and no request holds the object. The handler
runs with its class's channels, and its writes are flushed before
the object shuts down. Facets don't run it.

The container owns the run, so canceling the unsafe.evict() call or
the idle timer doesn't cancel the handler. Requests and evictions
that arrive meanwhile wait for it, then reach a new instance. A
request the handler makes to its own object waits too, until the
handler's wall-clock budget cuts the handler off. If the run fails,
for example because the flush failed, the object breaks and the
waiting requests get that error.

Aborting the object cancels the handler. So does draining a cluster
node, which then gives up its objects without waiting for the
handler's budget. A node whose lease has lapsed doesn't start one.

The handler gets no tracer and no self token, so its events don't
reach tail workers and ctx.restore() fails inside it.

server-test covers the idle path, an eviction whose caller goes away,
and an abort during the handler. cluster-test covers the drain.
@apeacock1991
apeacock1991 force-pushed the apeacock/do-on-hibernate-w3-server branch from 605d62f to 84d8ee9 Compare November 2, 2026 17:12

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💬 Approved with comments — The code has no open findings, and both earlier code-quality notes are fixed. What remains are two INFO documentation notes: the wording of a sentence in AGENTS.md and the still-missing user-facing docs for onHibernate in the local server.

Findings: 2 info

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📝 Documentation: 💬 2 info

Two INFO notes: one AGENTS.md wording point and missing user-facing documentation.

Recommendation: 💬 Optional Nits

  • [INFO] AGENTS.md says "Only abort cancels a running handler", but the handler's wall-clock budget can also end the run. Say that abort is the only cancellation from outside the handler. (src/workerd/server/AGENTS.md:77)
  • [INFO] No user-facing documentation yet says that idle shutdown and unsafe.evict() in the local server now run onHibernate, or how waiting requests and handler failures are handled. This can wait while the feature is experimental.
View full review
  • src/workerd/server/AGENTS.md:77 says "Only abort cancels a running handler", but the same entry says the handler's wall-clock budget can also end it. Say that abort is the only cancellation from outside the handler, and that the budget can still end the run.
  • No user-facing documentation yet describes when the local server runs onHibernate (on idle shutdown and on unsafe.evict()), that requests arriving meanwhile wait for it, or that a failed run breaks the object. The feature is still experimental, so this can wait until it stabilises.

✅ All other checks passed

🔒 Security: ✅ LGTM

No security concerns. The change affects only the actor lifecycle in the local server. It adds no new trust boundary, no secrets handling and no input parsing. The code-scanning alert on the stack trace is in a test worker.


📖 AGENTS.md: ✅ LGTM

The updated actor.rs entry describes the retirement lifecycle, the cancellation rules, and how requests and evictions that arrive during a run wait for it.


📋 Code Quality: ✅ LGTM

  • Handler check before the idle check: fixed. try_evict now calls ffi::actor_is_shared first and returns early for a shared root. It calls ffi::actor_has_on_hibernate_handler only after that.
  • Handler calling its own object: now documented in get_actor (actor.rs:932) and in AGENTS.md. Such a request waits until the handler's wall-clock budget ends the handler, so the wait has a limit.

The rest of the changed actor.rs holds up:

  • retire_actor clears retirement before it delivers the shared result.
  • abort converts a shutdown that has no reason while a retirement is committed.
  • cancel_shutdown now runs from finish_eviction and from the commit path.
  • get_actor re-checks retirement after prepare_storage().await.

⚡ Performance: ✅ LGTM

No performance concerns. The new path runs only when a root Durable Object that has an onHibernate handler is evicted or goes idle. retire_actor clears the retirement slot when the run finishes, so nothing grows without bound.


Reviewed commit: 84d8ee90

@apeacock1991
apeacock1991 merged commit 03cd4c7 into main Nov 3, 2026
36 of 38 checks passed
@apeacock1991
apeacock1991 deleted the apeacock/do-on-hibernate-w3-server branch November 3, 2026 11:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants