agents icon indicating copy to clipboard operation
agents copied to clipboard

routeAgentEmail() keeps DO non-hibernateable for ~100-120s after onEmail() returns

Open stevenmenke opened this issue 7 months ago • 3 comments

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 activeTime expected behavior for routeAgentEmail(), 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 from routeAgentEmail keep 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

stevenmenke avatar Apr 05 '26 10:04 stevenmenke

good catch I'll investigate

threepointone avatar Apr 05 '26 13:04 threepointone

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?

threepointone avatar Apr 05 '26 18:04 threepointone

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

threepointone avatar Apr 07 '26 10:04 threepointone