routeAgentEmail() keeps DO non-hibernateable for ~100-120s after onEmail() returns
Hi! I ran into a billing issue with routeAgentEmail() and onEmail() where the DO stays active for ~100-120s after the handler finishes, even though actual work takes ~1.4s. Wanted to report it in case I'm missing something or others hit the same thing.
What happened
I'm using routeAgentEmail() in my Worker's email() handler to route inbound emails to an Agent DO. The onEmail() handler parses the email, makes two service binding RPC calls to another DO (via env.CORE.dispatch()), and returns. The handler itself completes in ~1.4s.
But the DO bills ~100-120s of wall-clock duration per email. The handler is done, CPU drops to zero, no more SQL or subrequests — but activeTime keeps accumulating for another ~100s.
Evidence
Reproduced twice on the same DO instance:
| Test | Handler time (Date.now() delta) |
Platform WallTimeMs |
Platform CPUTimeMs |
|---|---|---|---|
| Email 1 | 1,346ms | 121,746ms | 17ms |
| Email 2 | 1,409ms | 102,333ms | 66ms |
GraphQL durableObjectsPeriodicGroups per-minute breakdown for Email 1:
| Minute (UTC) | activeTime | cpuTime | rowsRead | rowsWritten | subrequests |
|---|---|---|---|---|---|
| 12:09 | 59,999ms | 21ms | 37 | 1 | 1 |
| 12:10 | 60,000ms | 0ms | 0 | 0 | 0 |
| 12:11 | 1,819ms | 0ms | 0 | 0 | 0 |
The DO does all its work in minute 12:09 (21ms CPU, 37 rows read). Then it sits idle for a full minute (zero CPU, zero SQL, zero subrequests) while still accumulating activeTime, and another 1.8s into the third minute.
For comparison, the same DO instance handling regular RPC calls (not via routeAgentEmail) shows no idle billing — it becomes hibernation-eligible immediately after the handler returns:
| Event type | Handler time | Platform WallTimeMs |
|---|---|---|
worker_rpc (regular) |
228ms | 228ms |
worker_rpc via routeAgentEmail |
1,346ms | 121,746ms |
What I think is happening
routeAgentEmail() creates a DO stub via getAgentByName() and calls agent._onEmail(serialisableEmail) as an RPC. After onEmail() returns, the RPC session appears to keep the DO non-hibernateable.
I suspect this is related to workerd#6087 — RPC targets preventing DO hibernation. The calling Worker's email() execution context seems to stay alive for ~100-120s (possibly CF Email Routing holding it open for SMTP transaction completion?), and the RPC session pins the DO for that entire duration.
The four hibernation conditions should be met after onEmail() returns (no setTimeout, no pending fetch, no standard WebSocket API, no event processing), but the RPC session from the caller seems to override this.
Workaround
Processing the email in the Worker's email() handler directly (MIME parsing, content extraction, service binding calls) and only calling the DO for short-lived SQLite reads/writes avoids the issue. Each short RPC completes in ~10ms and the DO hibernates immediately after.
This works, but it kind of defeats the purpose of having onEmail() as a first-class method on the Agent class if using it costs 100x in duration billing.
What I'd find helpful
- Is the ~100-120s idle
activeTimeexpected behavior forrouteAgentEmail(), or is this a bug? - Does CF Email Routing hold the calling Worker's execution context open after
onEmail()returns? If so, does the RPC session fromrouteAgentEmailkeep the DO non-hibernateable for that duration? - Would it be possible to close/dispose the RPC session as soon as
_onEmail()resolves, rather than tying it to the caller's execution context lifetime?
Environment
[email protected][email protected]- DO extending
Agentwithhibernate: true(default) compatibility_date: 2026-04-03- CF Email Routing with catch-all → worker rule
good catch I'll investigate
This one's a bit hard to track down, wanna try npm i https://pkg.pr.new/agents@1263 and tell me if it works for you?
I'm going to land the PR since it doesn't break existing behaviour, but lmk when you can whether it fixes what you saw