Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mcp-destroy-stale-session-meta.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@executor-js/cloud": patch
---

An MCP request that reaches a session while the session is being torn down now gets the "Session not found" protocol error instead of a 500.
13 changes: 13 additions & 0 deletions packages/hosts/cloudflare/src/mcp/agent-session-durable-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,15 @@ export abstract class McpAgentSessionDOBase<
* second time.
*/
private disposingRuntime: Promise<void> | null = null;
/**
* Set once `destroy()` begins. The SDK's teardown drops its tables and wipes
* storage (the condemned marker included) before it aborts the isolate, and
* requests still land on this instance in between. From this point the
* in-memory session meta no longer describes stored state, so it must not
* vouch for the session: a request that trusted it would restore the runtime
* against dropped tables and fail as an unhandled error.
*/
private destroyStarted = false;
private onStartPromise: Promise<void> | null = null;
private lastActivityMs = 0;
private resolvedSessionName: string | undefined = undefined;
Expand Down Expand Up @@ -650,6 +659,9 @@ export abstract class McpAgentSessionDOBase<

private loadSessionMeta(): Effect.Effect<SessionMeta | null> {
return Effect.promise(async () => {
// See `destroyStarted`: once teardown begins, the cached meta outlives
// the storage it mirrors.
if (this.destroyStarted) return null;
if (this.sessionMeta) return this.sessionMeta;
const stored = await this.ctx.storage.get<SessionMeta>(SESSION_META_KEY);
// Backfill `resource` for sessions persisted before scoped toolkits added
Expand Down Expand Up @@ -1784,6 +1796,7 @@ export abstract class McpAgentSessionDOBase<
}

override async destroy(): Promise<void> {
this.destroyStarted = true;
await this.cleanup();
await super.destroy();
}
Expand Down
Loading