Repository navigation
STOR-5760: Run onHibernate handlers in the local server - #8150
Conversation
There was a problem hiding this comment.
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_evictcallsactor_has_on_hibernate_handlerbefore checkingactor_is_shared, but the C++ contract allows that call only while the actor is idle. It is harmless today; checkingactor_is_sharedfirst 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.rsentry 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.
605d62f to
84d8ee9
Compare
There was a problem hiding this comment.
💬 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
abortcancels 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:77says "Onlyabortcancels a running handler", but the same entry says the handler's wall-clock budget can also end it. Say thatabortis 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_evictnow callsffi::actor_is_sharedfirst and returns early for a shared root. It callsffi::actor_has_on_hibernate_handleronly 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_actorclearsretirementbefore it delivers the shared result.abortconverts a shutdown that has no reason while a retirement is committed.cancel_shutdownnow runs fromfinish_evictionand from the commit path.get_actorre-checksretirementafterprepare_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
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 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-testcases (the idle path, an eviction whose caller goes away, an abort during the handler) and acluster-testdrain case.