@ricsam/isolate 0.1.6 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -48,6 +48,35 @@ function isLinkerConflictError(error) {
48
48
  const message = error instanceof Error ? error.message : String(error ?? "");
49
49
  return message.includes(LINKER_CONFLICT_ERROR);
50
50
  }
51
+ function isDisposedRuntimeError(error) {
52
+ const message = error instanceof Error ? error.message : String(error ?? "");
53
+ return /runtime has been disposed|runtime was permanently disposed|runtime was soft-disposed|isolated is disposed/i.test(message);
54
+ }
55
+ function formatLifecycleReason(action, reason) {
56
+ const trimmedReason = reason?.trim();
57
+ return trimmedReason ? `${action}(${trimmedReason})` : `${action}()`;
58
+ }
59
+ function formatLogValue(value) {
60
+ if (typeof value === "string") {
61
+ return JSON.stringify(value);
62
+ }
63
+ if (typeof value === "number" || typeof value === "boolean" || value === null) {
64
+ return String(value);
65
+ }
66
+ try {
67
+ return JSON.stringify(value);
68
+ } catch {
69
+ return JSON.stringify(String(value));
70
+ }
71
+ }
72
+ function summarizeRequest(request) {
73
+ try {
74
+ const url = new URL(request.url);
75
+ return `${request.method} ${url.origin}${url.pathname}${url.search}`;
76
+ } catch {
77
+ return `${request.method} ${request.url}`;
78
+ }
79
+ }
51
80
 
52
81
  class IsolateServer {
53
82
  namespaceId;
@@ -55,6 +84,8 @@ class IsolateServer {
55
84
  runtime = null;
56
85
  lastStartOptions = null;
57
86
  lifecycleLock = Promise.resolve();
87
+ activeRequestCount = 0;
88
+ lastRuntimeRetirement = null;
58
89
  closed = true;
59
90
  fetch = {
60
91
  dispatchRequest: (request, options) => this.dispatchRequestWithRetry(request, options),
@@ -101,32 +132,78 @@ class IsolateServer {
101
132
  this.runtime = await this.createAndInitializeRuntime(options);
102
133
  });
103
134
  }
104
- async reload() {
135
+ async reload(reason) {
105
136
  const startOptions = this.lastStartOptions;
106
137
  if (!startOptions) {
107
138
  throw new Error("Server not configured. Call start() first.");
108
139
  }
140
+ const lifecycleReason = formatLifecycleReason("IsolateServer.reload", reason);
109
141
  this.closed = false;
110
142
  await this.withLifecycleLock(async () => {
111
- if (this.runtime) {
112
- const runtime = this.runtime;
143
+ const previousRuntime = this.runtime;
144
+ this.log("reload requested", {
145
+ namespaceId: this.namespaceId,
146
+ runtimeId: previousRuntime?.id ?? null,
147
+ reason: lifecycleReason,
148
+ activeRequests: this.activeRequestCount
149
+ });
150
+ if (previousRuntime) {
113
151
  this.runtime = null;
114
- await this.disposeRuntime(runtime, {
152
+ this.recordRuntimeRetirement(previousRuntime, "reload", lifecycleReason, true);
153
+ await this.disposeRuntime(previousRuntime, {
115
154
  hard: true,
116
- reason: "IsolateServer.reload()"
155
+ reason: lifecycleReason
117
156
  });
118
157
  }
119
- this.runtime = await this.createAndInitializeRuntime(startOptions);
158
+ try {
159
+ const nextRuntime = await this.createAndInitializeRuntime(startOptions);
160
+ this.runtime = nextRuntime;
161
+ } catch (error) {
162
+ this.log("reload failed", {
163
+ namespaceId: this.namespaceId,
164
+ previousRuntimeId: previousRuntime?.id ?? null,
165
+ reason: lifecycleReason,
166
+ activeRequests: this.activeRequestCount,
167
+ error: error instanceof Error ? error.message : String(error)
168
+ }, "warn");
169
+ throw error;
170
+ }
171
+ if (previousRuntime && this.lastRuntimeRetirement?.runtimeId === previousRuntime.id && this.lastRuntimeRetirement.action === "reload") {
172
+ this.lastRuntimeRetirement.replacementRuntimeId = this.runtime?.id;
173
+ }
174
+ this.log("reload completed", {
175
+ namespaceId: this.namespaceId,
176
+ previousRuntimeId: previousRuntime?.id ?? null,
177
+ runtimeId: this.runtime?.id ?? null,
178
+ reason: lifecycleReason,
179
+ activeRequests: this.activeRequestCount
180
+ });
120
181
  });
121
182
  }
122
- async close() {
183
+ async close(reason) {
184
+ const lifecycleReason = formatLifecycleReason("IsolateServer.close", reason);
123
185
  await this.withLifecycleLock(async () => {
124
- if (this.runtime) {
125
- const runtime = this.runtime;
186
+ const previousRuntime = this.runtime;
187
+ this.log("close requested", {
188
+ namespaceId: this.namespaceId,
189
+ runtimeId: previousRuntime?.id ?? null,
190
+ reason: lifecycleReason,
191
+ activeRequests: this.activeRequestCount
192
+ });
193
+ if (previousRuntime) {
126
194
  this.runtime = null;
127
- await this.disposeRuntime(runtime);
195
+ this.recordRuntimeRetirement(previousRuntime, "close", lifecycleReason, false);
196
+ await this.disposeRuntime(previousRuntime, {
197
+ reason: lifecycleReason
198
+ });
128
199
  }
129
200
  this.closed = true;
201
+ this.log("close completed", {
202
+ namespaceId: this.namespaceId,
203
+ previousRuntimeId: previousRuntime?.id ?? null,
204
+ reason: lifecycleReason,
205
+ activeRequests: this.activeRequestCount
206
+ });
130
207
  });
131
208
  }
132
209
  getRuntime() {
@@ -205,19 +282,73 @@ class IsolateServer {
205
282
  }
206
283
  return this.runtime;
207
284
  }
285
+ recordRuntimeRetirement(runtime, action, reason, hard) {
286
+ const record = {
287
+ runtimeId: runtime.id,
288
+ action,
289
+ reason,
290
+ hard,
291
+ at: Date.now(),
292
+ activeRequests: this.activeRequestCount
293
+ };
294
+ this.lastRuntimeRetirement = record;
295
+ return record;
296
+ }
297
+ getLastRuntimeRetirementLogFields() {
298
+ if (!this.lastRuntimeRetirement) {
299
+ return {
300
+ lastRetirementAction: "unknown"
301
+ };
302
+ }
303
+ return {
304
+ lastRetirementAction: this.lastRuntimeRetirement.action,
305
+ lastRetirementReason: this.lastRuntimeRetirement.reason,
306
+ lastRetirementRuntimeId: this.lastRuntimeRetirement.runtimeId,
307
+ lastRetirementHard: this.lastRuntimeRetirement.hard,
308
+ lastRetirementAgeMs: Date.now() - this.lastRuntimeRetirement.at,
309
+ lastRetirementActiveRequests: this.lastRuntimeRetirement.activeRequests,
310
+ lastRetirementReplacementRuntimeId: this.lastRuntimeRetirement.replacementRuntimeId ?? null
311
+ };
312
+ }
313
+ log(message, fields, level = "log") {
314
+ const suffix = Object.entries(fields).filter(([, value]) => value !== undefined).map(([key, value]) => `${key}=${formatLogValue(value)}`).join(" ");
315
+ const logger = level === "warn" ? console.warn : console.log;
316
+ logger(`[isolate-server] ${message}${suffix ? `; ${suffix}` : ""}`);
317
+ }
208
318
  async dispatchRequestWithRetry(request, options) {
209
- const runtime = await this.getActiveRuntime();
319
+ this.activeRequestCount += 1;
210
320
  try {
211
- return await runtime.fetch.dispatchRequest(request, options);
212
- } catch (error) {
213
- if (!isLinkerConflictError(error)) {
214
- throw error;
321
+ const runtime = await this.getActiveRuntime();
322
+ try {
323
+ return await runtime.fetch.dispatchRequest(request, options);
324
+ } catch (error) {
325
+ if (!isLinkerConflictError(error) && !isDisposedRuntimeError(error)) {
326
+ throw error;
327
+ }
328
+ const requestSummary = summarizeRequest(request);
329
+ if (isLinkerConflictError(error)) {
330
+ await this.reload(`request-linker-conflict: ${requestSummary}`);
331
+ } else if (this.runtime?.id === runtime.id) {
332
+ this.runtime = null;
333
+ }
334
+ const retryRuntime = await this.getActiveRuntime();
335
+ this.log(isLinkerConflictError(error) ? "request recovered after linker conflict" : "request recovered after disposed runtime", {
336
+ namespaceId: this.namespaceId,
337
+ request: requestSummary,
338
+ requestId: options?.requestId ?? null,
339
+ metadataKeys: Object.keys(options?.metadata ?? {}),
340
+ previousRuntimeId: runtime.id,
341
+ runtimeId: retryRuntime.id,
342
+ activeRequests: this.activeRequestCount,
343
+ error: error instanceof Error ? error.message : String(error),
344
+ ...this.getLastRuntimeRetirementLogFields()
345
+ }, "warn");
346
+ return retryRuntime.fetch.dispatchRequest(request, options);
215
347
  }
216
- await this.reload();
217
- const retryRuntime = await this.getActiveRuntime();
218
- return retryRuntime.fetch.dispatchRequest(request, options);
348
+ } finally {
349
+ this.activeRequestCount -= 1;
219
350
  }
220
351
  }
221
352
  }
222
353
 
223
- //# debugId=92DF47A502A2457564756E2164756E21
354
+ //# debugId=7EA2EF999F10580F64756E2164756E21
@@ -2,9 +2,9 @@
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/internal/server/index.ts"],
4
4
  "sourcesContent": [
5
- "import type {\n DaemonConnection,\n DispatchOptions,\n RemoteRuntime,\n RuntimeOptions,\n UpgradeRequest,\n WebSocketCommand,\n} from \"../client/index.cjs\";\nimport { isBenignDisposeError } from \"../client/index.cjs\";\n\nconst LINKER_CONFLICT_ERROR = \"Module is currently being linked by another linker\";\n\nfunction isLinkerConflictError(error: unknown): boolean {\n const message = error instanceof Error ? error.message : String(error ?? \"\");\n return message.includes(LINKER_CONFLICT_ERROR);\n}\n\nexport interface IsolateServerOptions {\n namespaceId: string;\n getConnection: () => Promise<DaemonConnection>;\n}\n\nexport interface IsolateServerStartOptions {\n runtimeOptions: RuntimeOptions;\n entry: string;\n entryFilename?: string;\n onWebSocketCommand?: (cmd: WebSocketCommand) => void;\n}\n\nexport interface IsolateServerFetch {\n dispatchRequest(request: Request, options?: DispatchOptions): Promise<Response>;\n getUpgradeRequest(): Promise<UpgradeRequest | null>;\n dispatchWebSocketOpen(connectionId: string): Promise<void>;\n dispatchWebSocketMessage(connectionId: string, message: string | ArrayBuffer): Promise<void>;\n dispatchWebSocketClose(connectionId: string, code: number, reason: string): Promise<void>;\n dispatchWebSocketError(connectionId: string, error: Error): Promise<void>;\n hasServeHandler(): Promise<boolean>;\n hasActiveConnections(): Promise<boolean>;\n}\n\nexport class IsolateServer {\n private readonly namespaceId: string;\n private readonly getConnection: () => Promise<DaemonConnection>;\n private runtime: RemoteRuntime | null = null;\n private lastStartOptions: IsolateServerStartOptions | null = null;\n private lifecycleLock: Promise<void> = Promise.resolve();\n private closed = true;\n\n readonly fetch: IsolateServerFetch = {\n dispatchRequest: (request, options) => this.dispatchRequestWithRetry(request, options),\n getUpgradeRequest: async () => {\n const runtime = await this.getActiveRuntime();\n return runtime.fetch.getUpgradeRequest();\n },\n dispatchWebSocketOpen: async (connectionId) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketOpen(connectionId);\n },\n dispatchWebSocketMessage: async (connectionId, message) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketMessage(connectionId, message);\n },\n dispatchWebSocketClose: async (connectionId, code, reason) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketClose(connectionId, code, reason);\n },\n dispatchWebSocketError: async (connectionId, error) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketError(connectionId, error);\n },\n hasServeHandler: async () => {\n const runtime = await this.getActiveRuntime();\n return runtime.fetch.hasServeHandler();\n },\n hasActiveConnections: async () => {\n const runtime = await this.getActiveRuntime();\n return runtime.fetch.hasActiveConnections();\n },\n };\n\n constructor(options: IsolateServerOptions) {\n this.namespaceId = options.namespaceId;\n this.getConnection = options.getConnection;\n }\n\n async start(options: IsolateServerStartOptions): Promise<void> {\n this.lastStartOptions = options;\n this.closed = false;\n\n await this.withLifecycleLock(async () => {\n if (this.runtime) {\n return;\n }\n\n this.runtime = await this.createAndInitializeRuntime(options);\n });\n }\n\n async reload(): Promise<void> {\n const startOptions = this.lastStartOptions;\n if (!startOptions) {\n throw new Error(\"Server not configured. Call start() first.\");\n }\n\n this.closed = false;\n await this.withLifecycleLock(async () => {\n if (this.runtime) {\n const runtime = this.runtime;\n this.runtime = null;\n await this.disposeRuntime(runtime, {\n hard: true,\n reason: \"IsolateServer.reload()\",\n });\n }\n\n this.runtime = await this.createAndInitializeRuntime(startOptions);\n });\n }\n\n async close(): Promise<void> {\n await this.withLifecycleLock(async () => {\n if (this.runtime) {\n const runtime = this.runtime;\n this.runtime = null;\n await this.disposeRuntime(runtime);\n }\n\n this.closed = true;\n });\n }\n\n getRuntime(): RemoteRuntime | null {\n return this.runtime;\n }\n\n private async withLifecycleLock<T>(operation: () => Promise<T>): Promise<T> {\n const previous = this.lifecycleLock;\n let release!: () => void;\n this.lifecycleLock = new Promise<void>((resolve) => {\n release = resolve;\n });\n\n await previous;\n try {\n return await operation();\n } finally {\n release();\n }\n }\n\n private buildRuntimeOptions(options: IsolateServerStartOptions): RuntimeOptions {\n if (options.onWebSocketCommand) {\n return {\n ...options.runtimeOptions,\n onWebSocketCommand: options.onWebSocketCommand,\n };\n }\n\n return options.runtimeOptions;\n }\n\n private async createAndInitializeRuntime(\n options: IsolateServerStartOptions,\n allowRetry: boolean = true\n ): Promise<RemoteRuntime> {\n const connection = await this.getConnection();\n const namespace = connection.createNamespace(this.namespaceId);\n const runtimeOptions = this.buildRuntimeOptions(options);\n const runtime = await namespace.createRuntime(runtimeOptions);\n\n try {\n await runtime.eval(\n `import ${JSON.stringify(options.entry)};`,\n options.entryFilename ?? \"/isolate_server_entry.js\"\n );\n return runtime;\n } catch (error) {\n await this.disposeRuntime(runtime);\n if (!allowRetry || !isLinkerConflictError(error)) {\n throw error;\n }\n\n const retryRuntime = await namespace.createRuntime(runtimeOptions);\n try {\n await retryRuntime.eval(\n `import ${JSON.stringify(options.entry)};`,\n options.entryFilename ?? \"/isolate_server_entry.js\"\n );\n return retryRuntime;\n } catch (retryError) {\n await this.disposeRuntime(retryRuntime);\n throw retryError;\n }\n }\n }\n\n private async disposeRuntime(\n runtime: RemoteRuntime,\n options?: { hard?: boolean; reason?: string }\n ): Promise<void> {\n try {\n await runtime.dispose(options);\n } catch (error) {\n if (!isBenignDisposeError(error)) {\n throw error;\n }\n }\n }\n\n private async ensureStarted(): Promise<void> {\n if (this.runtime) {\n return;\n }\n\n if (!this.lastStartOptions) {\n throw new Error(\"Server not configured. Call start() first.\");\n }\n\n if (this.closed) {\n this.closed = false;\n }\n\n await this.start(this.lastStartOptions);\n }\n\n private async getActiveRuntime(): Promise<RemoteRuntime> {\n await this.ensureStarted();\n if (!this.runtime) {\n throw new Error(\"Server runtime failed to start.\");\n }\n return this.runtime;\n }\n\n private async dispatchRequestWithRetry(\n request: Request,\n options?: DispatchOptions\n ): Promise<Response> {\n const runtime = await this.getActiveRuntime();\n try {\n return await runtime.fetch.dispatchRequest(request, options);\n } catch (error) {\n if (!isLinkerConflictError(error)) {\n throw error;\n }\n\n await this.reload();\n const retryRuntime = await this.getActiveRuntime();\n return retryRuntime.fetch.dispatchRequest(request, options);\n }\n }\n}\n"
5
+ "import type {\n DaemonConnection,\n DispatchOptions,\n RemoteRuntime,\n RuntimeOptions,\n UpgradeRequest,\n WebSocketCommand,\n} from \"../client/index.cjs\";\nimport { isBenignDisposeError } from \"../client/index.cjs\";\n\nconst LINKER_CONFLICT_ERROR = \"Module is currently being linked by another linker\";\n\nfunction isLinkerConflictError(error: unknown): boolean {\n const message = error instanceof Error ? error.message : String(error ?? \"\");\n return message.includes(LINKER_CONFLICT_ERROR);\n}\n\nfunction isDisposedRuntimeError(error: unknown): boolean {\n const message = error instanceof Error ? error.message : String(error ?? \"\");\n return /runtime has been disposed|runtime was permanently disposed|runtime was soft-disposed|isolated is disposed/i.test(\n message\n );\n}\n\ntype RuntimeRetirementAction = \"reload\" | \"close\";\n\ninterface RuntimeRetirementRecord {\n runtimeId: string;\n action: RuntimeRetirementAction;\n reason: string;\n hard: boolean;\n at: number;\n activeRequests: number;\n replacementRuntimeId?: string;\n}\n\nfunction formatLifecycleReason(action: string, reason?: string): string {\n const trimmedReason = reason?.trim();\n return trimmedReason ? `${action}(${trimmedReason})` : `${action}()`;\n}\n\nfunction formatLogValue(value: unknown): string {\n if (typeof value === \"string\") {\n return JSON.stringify(value);\n }\n if (typeof value === \"number\" || typeof value === \"boolean\" || value === null) {\n return String(value);\n }\n\n try {\n return JSON.stringify(value);\n } catch {\n return JSON.stringify(String(value));\n }\n}\n\nfunction summarizeRequest(request: Request): string {\n try {\n const url = new URL(request.url);\n return `${request.method} ${url.origin}${url.pathname}${url.search}`;\n } catch {\n return `${request.method} ${request.url}`;\n }\n}\n\nexport interface IsolateServerOptions {\n namespaceId: string;\n getConnection: () => Promise<DaemonConnection>;\n}\n\nexport interface IsolateServerStartOptions {\n runtimeOptions: RuntimeOptions;\n entry: string;\n entryFilename?: string;\n onWebSocketCommand?: (cmd: WebSocketCommand) => void;\n}\n\nexport interface IsolateServerFetch {\n dispatchRequest(request: Request, options?: DispatchOptions): Promise<Response>;\n getUpgradeRequest(): Promise<UpgradeRequest | null>;\n dispatchWebSocketOpen(connectionId: string): Promise<void>;\n dispatchWebSocketMessage(connectionId: string, message: string | ArrayBuffer): Promise<void>;\n dispatchWebSocketClose(connectionId: string, code: number, reason: string): Promise<void>;\n dispatchWebSocketError(connectionId: string, error: Error): Promise<void>;\n hasServeHandler(): Promise<boolean>;\n hasActiveConnections(): Promise<boolean>;\n}\n\nexport class IsolateServer {\n private readonly namespaceId: string;\n private readonly getConnection: () => Promise<DaemonConnection>;\n private runtime: RemoteRuntime | null = null;\n private lastStartOptions: IsolateServerStartOptions | null = null;\n private lifecycleLock: Promise<void> = Promise.resolve();\n private activeRequestCount = 0;\n private lastRuntimeRetirement: RuntimeRetirementRecord | null = null;\n private closed = true;\n\n readonly fetch: IsolateServerFetch = {\n dispatchRequest: (request, options) => this.dispatchRequestWithRetry(request, options),\n getUpgradeRequest: async () => {\n const runtime = await this.getActiveRuntime();\n return runtime.fetch.getUpgradeRequest();\n },\n dispatchWebSocketOpen: async (connectionId) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketOpen(connectionId);\n },\n dispatchWebSocketMessage: async (connectionId, message) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketMessage(connectionId, message);\n },\n dispatchWebSocketClose: async (connectionId, code, reason) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketClose(connectionId, code, reason);\n },\n dispatchWebSocketError: async (connectionId, error) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketError(connectionId, error);\n },\n hasServeHandler: async () => {\n const runtime = await this.getActiveRuntime();\n return runtime.fetch.hasServeHandler();\n },\n hasActiveConnections: async () => {\n const runtime = await this.getActiveRuntime();\n return runtime.fetch.hasActiveConnections();\n },\n };\n\n constructor(options: IsolateServerOptions) {\n this.namespaceId = options.namespaceId;\n this.getConnection = options.getConnection;\n }\n\n async start(options: IsolateServerStartOptions): Promise<void> {\n this.lastStartOptions = options;\n this.closed = false;\n\n await this.withLifecycleLock(async () => {\n if (this.runtime) {\n return;\n }\n\n this.runtime = await this.createAndInitializeRuntime(options);\n });\n }\n\n async reload(reason?: string): Promise<void> {\n const startOptions = this.lastStartOptions;\n if (!startOptions) {\n throw new Error(\"Server not configured. Call start() first.\");\n }\n\n const lifecycleReason = formatLifecycleReason(\"IsolateServer.reload\", reason);\n this.closed = false;\n await this.withLifecycleLock(async () => {\n const previousRuntime = this.runtime;\n this.log(\"reload requested\", {\n namespaceId: this.namespaceId,\n runtimeId: previousRuntime?.id ?? null,\n reason: lifecycleReason,\n activeRequests: this.activeRequestCount,\n });\n\n if (previousRuntime) {\n this.runtime = null;\n this.recordRuntimeRetirement(previousRuntime, \"reload\", lifecycleReason, true);\n await this.disposeRuntime(previousRuntime, {\n hard: true,\n reason: lifecycleReason,\n });\n }\n\n try {\n const nextRuntime = await this.createAndInitializeRuntime(startOptions);\n this.runtime = nextRuntime;\n } catch (error) {\n this.log(\"reload failed\", {\n namespaceId: this.namespaceId,\n previousRuntimeId: previousRuntime?.id ?? null,\n reason: lifecycleReason,\n activeRequests: this.activeRequestCount,\n error: error instanceof Error ? error.message : String(error),\n }, \"warn\");\n throw error;\n }\n\n if (\n previousRuntime &&\n this.lastRuntimeRetirement?.runtimeId === previousRuntime.id &&\n this.lastRuntimeRetirement.action === \"reload\"\n ) {\n this.lastRuntimeRetirement.replacementRuntimeId = this.runtime?.id;\n }\n\n this.log(\"reload completed\", {\n namespaceId: this.namespaceId,\n previousRuntimeId: previousRuntime?.id ?? null,\n runtimeId: this.runtime?.id ?? null,\n reason: lifecycleReason,\n activeRequests: this.activeRequestCount,\n });\n });\n }\n\n async close(reason?: string): Promise<void> {\n const lifecycleReason = formatLifecycleReason(\"IsolateServer.close\", reason);\n await this.withLifecycleLock(async () => {\n const previousRuntime = this.runtime;\n this.log(\"close requested\", {\n namespaceId: this.namespaceId,\n runtimeId: previousRuntime?.id ?? null,\n reason: lifecycleReason,\n activeRequests: this.activeRequestCount,\n });\n\n if (previousRuntime) {\n this.runtime = null;\n this.recordRuntimeRetirement(previousRuntime, \"close\", lifecycleReason, false);\n await this.disposeRuntime(previousRuntime, {\n reason: lifecycleReason,\n });\n }\n\n this.closed = true;\n this.log(\"close completed\", {\n namespaceId: this.namespaceId,\n previousRuntimeId: previousRuntime?.id ?? null,\n reason: lifecycleReason,\n activeRequests: this.activeRequestCount,\n });\n });\n }\n\n getRuntime(): RemoteRuntime | null {\n return this.runtime;\n }\n\n private async withLifecycleLock<T>(operation: () => Promise<T>): Promise<T> {\n const previous = this.lifecycleLock;\n let release!: () => void;\n this.lifecycleLock = new Promise<void>((resolve) => {\n release = resolve;\n });\n\n await previous;\n try {\n return await operation();\n } finally {\n release();\n }\n }\n\n private buildRuntimeOptions(options: IsolateServerStartOptions): RuntimeOptions {\n if (options.onWebSocketCommand) {\n return {\n ...options.runtimeOptions,\n onWebSocketCommand: options.onWebSocketCommand,\n };\n }\n\n return options.runtimeOptions;\n }\n\n private async createAndInitializeRuntime(\n options: IsolateServerStartOptions,\n allowRetry: boolean = true\n ): Promise<RemoteRuntime> {\n const connection = await this.getConnection();\n const namespace = connection.createNamespace(this.namespaceId);\n const runtimeOptions = this.buildRuntimeOptions(options);\n const runtime = await namespace.createRuntime(runtimeOptions);\n\n try {\n await runtime.eval(\n `import ${JSON.stringify(options.entry)};`,\n options.entryFilename ?? \"/isolate_server_entry.js\"\n );\n return runtime;\n } catch (error) {\n await this.disposeRuntime(runtime);\n if (!allowRetry || !isLinkerConflictError(error)) {\n throw error;\n }\n\n const retryRuntime = await namespace.createRuntime(runtimeOptions);\n try {\n await retryRuntime.eval(\n `import ${JSON.stringify(options.entry)};`,\n options.entryFilename ?? \"/isolate_server_entry.js\"\n );\n return retryRuntime;\n } catch (retryError) {\n await this.disposeRuntime(retryRuntime);\n throw retryError;\n }\n }\n }\n\n private async disposeRuntime(\n runtime: RemoteRuntime,\n options?: { hard?: boolean; reason?: string }\n ): Promise<void> {\n try {\n await runtime.dispose(options);\n } catch (error) {\n if (!isBenignDisposeError(error)) {\n throw error;\n }\n }\n }\n\n private async ensureStarted(): Promise<void> {\n if (this.runtime) {\n return;\n }\n\n if (!this.lastStartOptions) {\n throw new Error(\"Server not configured. Call start() first.\");\n }\n\n if (this.closed) {\n this.closed = false;\n }\n\n await this.start(this.lastStartOptions);\n }\n\n private async getActiveRuntime(): Promise<RemoteRuntime> {\n await this.ensureStarted();\n if (!this.runtime) {\n throw new Error(\"Server runtime failed to start.\");\n }\n return this.runtime;\n }\n\n private recordRuntimeRetirement(\n runtime: RemoteRuntime,\n action: RuntimeRetirementAction,\n reason: string,\n hard: boolean\n ): RuntimeRetirementRecord {\n const record: RuntimeRetirementRecord = {\n runtimeId: runtime.id,\n action,\n reason,\n hard,\n at: Date.now(),\n activeRequests: this.activeRequestCount,\n };\n this.lastRuntimeRetirement = record;\n return record;\n }\n\n private getLastRuntimeRetirementLogFields(): Record<string, unknown> {\n if (!this.lastRuntimeRetirement) {\n return {\n lastRetirementAction: \"unknown\",\n };\n }\n\n return {\n lastRetirementAction: this.lastRuntimeRetirement.action,\n lastRetirementReason: this.lastRuntimeRetirement.reason,\n lastRetirementRuntimeId: this.lastRuntimeRetirement.runtimeId,\n lastRetirementHard: this.lastRuntimeRetirement.hard,\n lastRetirementAgeMs: Date.now() - this.lastRuntimeRetirement.at,\n lastRetirementActiveRequests: this.lastRuntimeRetirement.activeRequests,\n lastRetirementReplacementRuntimeId: this.lastRuntimeRetirement.replacementRuntimeId ?? null,\n };\n }\n\n private log(\n message: string,\n fields: Record<string, unknown>,\n level: \"log\" | \"warn\" = \"log\"\n ): void {\n const suffix = Object.entries(fields)\n .filter(([, value]) => value !== undefined)\n .map(([key, value]) => `${key}=${formatLogValue(value)}`)\n .join(\" \");\n\n const logger = level === \"warn\" ? console.warn : console.log;\n logger(`[isolate-server] ${message}${suffix ? `; ${suffix}` : \"\"}`);\n }\n\n private async dispatchRequestWithRetry(\n request: Request,\n options?: DispatchOptions\n ): Promise<Response> {\n this.activeRequestCount += 1;\n try {\n const runtime = await this.getActiveRuntime();\n try {\n return await runtime.fetch.dispatchRequest(request, options);\n } catch (error) {\n if (!isLinkerConflictError(error) && !isDisposedRuntimeError(error)) {\n throw error;\n }\n\n const requestSummary = summarizeRequest(request);\n if (isLinkerConflictError(error)) {\n await this.reload(`request-linker-conflict: ${requestSummary}`);\n } else if (this.runtime?.id === runtime.id) {\n this.runtime = null;\n }\n\n const retryRuntime = await this.getActiveRuntime();\n this.log(\n isLinkerConflictError(error)\n ? \"request recovered after linker conflict\"\n : \"request recovered after disposed runtime\",\n {\n namespaceId: this.namespaceId,\n request: requestSummary,\n requestId: options?.requestId ?? null,\n metadataKeys: Object.keys(options?.metadata ?? {}),\n previousRuntimeId: runtime.id,\n runtimeId: retryRuntime.id,\n activeRequests: this.activeRequestCount,\n error: error instanceof Error ? error.message : String(error),\n ...this.getLastRuntimeRetirementLogFields(),\n },\n \"warn\"\n );\n return retryRuntime.fetch.dispatchRequest(request, options);\n }\n } finally {\n this.activeRequestCount -= 1;\n }\n }\n}\n"
6
6
  ],
7
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQqC,IAArC;AAEA,IAAM,wBAAwB;AAE9B,SAAS,qBAAqB,CAAC,OAAyB;AAAA,EACtD,MAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU,OAAO,SAAS,EAAE;AAAA,EAC3E,OAAO,QAAQ,SAAS,qBAAqB;AAAA;AAAA;AA0BxC,MAAM,cAAc;AAAA,EACR;AAAA,EACA;AAAA,EACT,UAAgC;AAAA,EAChC,mBAAqD;AAAA,EACrD,gBAA+B,QAAQ,QAAQ;AAAA,EAC/C,SAAS;AAAA,EAER,QAA4B;AAAA,IACnC,iBAAiB,CAAC,SAAS,YAAY,KAAK,yBAAyB,SAAS,OAAO;AAAA,IACrF,mBAAmB,YAAY;AAAA,MAC7B,MAAM,UAAU,MAAM,KA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UACnE,MAAM;AAAA,QACR;AAAA,QAEA,MAAM,iBAAiB,iBAAiB,OAAO;AAAA,QAC/C,IAAI,sBAAsB,KAAK,GAAG;AAAA,UAChC,MAAM,KAAK,OAAO,4BAA4B,gBAAgB;AAAA,QAChE,EAAO,SAAI,KAAK,SAAS,OAAO,QAAQ,IAAI;AAAA,UAC1C,KAAK,UAAU;AAAA,QACjB;AAAA,QAEA,MAAM,eAAe,MAAM,KAAK,iBAAiB;AAAA,QACjD,KAAK,IACH,sBAAsB,KAAK,IACvB,4CACA,4CACJ;AAAA,UACE,aAAa,KAAK;AAAA,UAClB,SAAS;AAAA,UACT,WAAW,SAAS,aAAa;AAAA,UACjC,cAAc,OAAO,KAAK,SAAS,YAAY,CAAC,CAAC;AAAA,UACjD,mBAAmB,QAAQ;AAAA,UAC3B,WAAW,aAAa;AAAA,UACxB,gBAAgB,KAAK;AAAA,UACrB,OAAO,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AAAA,aACzD,KAAK,kCAAkC;AAAA,QAC5C,GACA,MACF;AAAA,QACA,OAAO,aAAa,MAAM,gBAAgB,SAAS,OAAO;AAAA;AAAA,cAE5D;AAAA,MACA,KAAK,sBAAsB;AAAA;AAAA;AAGjC;",
8
+ "debugId": "7EA2EF999F10580F64756E2164756E21",
9
9
  "names": []
10
10
  }
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@ricsam/isolate",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "type": "commonjs"
5
5
  }
@@ -122,11 +122,11 @@ async function createAppServerAdapter(getConnection, options) {
122
122
  await server.fetch.dispatchWebSocketError(connectionId, error);
123
123
  }
124
124
  },
125
- reload: async () => {
125
+ reload: async (reason) => {
126
126
  diagnostics.lifecycleState = "reloading";
127
127
  try {
128
- bindingsAdapter.reset("AppServer.reload()");
129
- await server.reload();
128
+ bindingsAdapter.reset(reason ? `AppServer.reload(${reason})` : "AppServer.reload()");
129
+ await server.reload(reason);
130
130
  runtimeId = server.getRuntime()?.id ?? options.key;
131
131
  } finally {
132
132
  diagnostics.lifecycleState = "idle";
@@ -136,14 +136,16 @@ async function createAppServerAdapter(getConnection, options) {
136
136
  diagnostics.lifecycleState = "disposing";
137
137
  try {
138
138
  if (disposeOptions?.hard) {
139
- bindingsAdapter.reset(disposeOptions?.reason ?? "AppServer.dispose(hard)");
140
- await server.reload();
141
- bindingsAdapter.abort(disposeOptions?.reason ?? "AppServer.dispose(hard)");
142
- await server.close();
139
+ const hardDisposeReason = disposeOptions?.reason ? `AppServer.dispose(hard): ${disposeOptions.reason}` : "AppServer.dispose(hard)";
140
+ bindingsAdapter.reset(hardDisposeReason);
141
+ await server.reload(hardDisposeReason);
142
+ bindingsAdapter.abort(hardDisposeReason);
143
+ await server.close(hardDisposeReason);
143
144
  return;
144
145
  }
145
- bindingsAdapter.abort(disposeOptions?.reason ?? "AppServer.dispose()");
146
- await server.close();
146
+ const disposeReason = disposeOptions?.reason ? `AppServer.dispose(): ${disposeOptions.reason}` : "AppServer.dispose()";
147
+ bindingsAdapter.abort(disposeReason);
148
+ await server.close(disposeReason);
147
149
  } finally {
148
150
  diagnostics.lifecycleState = "idle";
149
151
  }
@@ -155,4 +157,4 @@ async function createAppServerAdapter(getConnection, options) {
155
157
  };
156
158
  }
157
159
 
158
- //# debugId=035A30B7A1437CB364756E2164756E21
160
+ //# debugId=34E29BF0A88C356464756E2164756E21
@@ -2,9 +2,9 @@
2
2
  "version": 3,
3
3
  "sources": ["../../../src/server/app-server.ts"],
4
4
  "sourcesContent": [
5
- "import { IsolateServer } from \"../internal/server/index.cjs\";\nimport type { DaemonConnection } from \"../internal/client/index.cjs\";\nimport { createRuntimeDiagnostics } from \"../bridge/diagnostics.cjs\";\nimport { createRuntimeBindingsAdapter } from \"../bridge/runtime-bindings.cjs\";\nimport { withRequestContext } from \"../bridge/request-context.cjs\";\nimport type { AppServer, CreateAppServerOptions, RequestResult } from \"../types.cjs\";\n\nexport async function createAppServerAdapter(\n getConnection: () => Promise<DaemonConnection>,\n options: CreateAppServerOptions,\n): Promise<AppServer> {\n const diagnostics = createRuntimeDiagnostics();\n const server = new IsolateServer({\n namespaceId: options.key,\n getConnection,\n });\n\n let runtimeId = options.key;\n const bindingsAdapter = createRuntimeBindingsAdapter(\n options.bindings,\n () => runtimeId,\n diagnostics,\n );\n await server.start({\n entry: options.entry,\n entryFilename: options.entryFilename,\n runtimeOptions: {\n ...bindingsAdapter.runtimeOptions,\n cwd: options.cwd,\n memoryLimitMB: options.memoryLimitMB,\n executionTimeout: options.executionTimeout,\n testEnvironment: options.features?.tests ?? false,\n },\n onWebSocketCommand: (command) => {\n options.webSockets?.onCommand?.(command);\n },\n });\n runtimeId = server.getRuntime()?.id ?? options.key;\n\n async function handleRequest(request: Request, handleOptions?: { requestId?: string; signal?: AbortSignal; metadata?: Record<string, string> }): Promise<RequestResult> {\n diagnostics.activeRequests += 1;\n diagnostics.lifecycleState = \"active\";\n try {\n return await withRequestContext(\n {\n requestId: handleOptions?.requestId,\n metadata: handleOptions?.metadata,\n signal: handleOptions?.signal ?? request.signal,\n },\n async () => {\n const response = await server.fetch.dispatchRequest(request, {\n signal: handleOptions?.signal,\n requestId: handleOptions?.requestId,\n metadata: handleOptions?.metadata,\n });\n const upgradeRequest = await server.fetch.getUpgradeRequest();\n if (upgradeRequest?.requested) {\n return {\n type: \"websocket\",\n upgradeData: {\n requested: upgradeRequest.requested,\n connectionId: upgradeRequest.connectionId,\n },\n };\n }\n\n return {\n type: \"response\",\n response,\n };\n },\n );\n } catch (error) {\n diagnostics.lastError = error instanceof Error ? error.message : String(error);\n throw error;\n } finally {\n diagnostics.activeRequests -= 1;\n diagnostics.lifecycleState = \"idle\";\n }\n }\n\n return {\n handle: handleRequest,\n ws: {\n open: async (connectionId) => {\n await server.fetch.dispatchWebSocketOpen(connectionId);\n },\n message: async (connectionId, data) => {\n await server.fetch.dispatchWebSocketMessage(connectionId, data);\n },\n close: async (connectionId, code, reason) => {\n await server.fetch.dispatchWebSocketClose(connectionId, code, reason);\n },\n error: async (connectionId, error) => {\n await server.fetch.dispatchWebSocketError(connectionId, error);\n },\n },\n reload: async () => {\n diagnostics.lifecycleState = \"reloading\";\n try {\n bindingsAdapter.reset(\"AppServer.reload()\");\n await server.reload();\n runtimeId = server.getRuntime()?.id ?? options.key;\n } finally {\n diagnostics.lifecycleState = \"idle\";\n }\n },\n dispose: async (disposeOptions) => {\n diagnostics.lifecycleState = \"disposing\";\n try {\n if (disposeOptions?.hard) {\n bindingsAdapter.reset(disposeOptions?.reason ?? \"AppServer.dispose(hard)\");\n await server.reload();\n bindingsAdapter.abort(disposeOptions?.reason ?? \"AppServer.dispose(hard)\");\n await server.close();\n return;\n }\n bindingsAdapter.abort(disposeOptions?.reason ?? \"AppServer.dispose()\");\n await server.close();\n } finally {\n diagnostics.lifecycleState = \"idle\";\n }\n },\n diagnostics: async () => ({\n ...diagnostics,\n reused: server.getRuntime()?.reused,\n }),\n };\n}\n"
5
+ "import { IsolateServer } from \"../internal/server/index.cjs\";\nimport type { DaemonConnection } from \"../internal/client/index.cjs\";\nimport { createRuntimeDiagnostics } from \"../bridge/diagnostics.cjs\";\nimport { createRuntimeBindingsAdapter } from \"../bridge/runtime-bindings.cjs\";\nimport { withRequestContext } from \"../bridge/request-context.cjs\";\nimport type { AppServer, CreateAppServerOptions, RequestResult } from \"../types.cjs\";\n\nexport async function createAppServerAdapter(\n getConnection: () => Promise<DaemonConnection>,\n options: CreateAppServerOptions,\n): Promise<AppServer> {\n const diagnostics = createRuntimeDiagnostics();\n const server = new IsolateServer({\n namespaceId: options.key,\n getConnection,\n });\n\n let runtimeId = options.key;\n const bindingsAdapter = createRuntimeBindingsAdapter(\n options.bindings,\n () => runtimeId,\n diagnostics,\n );\n await server.start({\n entry: options.entry,\n entryFilename: options.entryFilename,\n runtimeOptions: {\n ...bindingsAdapter.runtimeOptions,\n cwd: options.cwd,\n memoryLimitMB: options.memoryLimitMB,\n executionTimeout: options.executionTimeout,\n testEnvironment: options.features?.tests ?? false,\n },\n onWebSocketCommand: (command) => {\n options.webSockets?.onCommand?.(command);\n },\n });\n runtimeId = server.getRuntime()?.id ?? options.key;\n\n async function handleRequest(request: Request, handleOptions?: { requestId?: string; signal?: AbortSignal; metadata?: Record<string, string> }): Promise<RequestResult> {\n diagnostics.activeRequests += 1;\n diagnostics.lifecycleState = \"active\";\n try {\n return await withRequestContext(\n {\n requestId: handleOptions?.requestId,\n metadata: handleOptions?.metadata,\n signal: handleOptions?.signal ?? request.signal,\n },\n async () => {\n const response = await server.fetch.dispatchRequest(request, {\n signal: handleOptions?.signal,\n requestId: handleOptions?.requestId,\n metadata: handleOptions?.metadata,\n });\n const upgradeRequest = await server.fetch.getUpgradeRequest();\n if (upgradeRequest?.requested) {\n return {\n type: \"websocket\",\n upgradeData: {\n requested: upgradeRequest.requested,\n connectionId: upgradeRequest.connectionId,\n },\n };\n }\n\n return {\n type: \"response\",\n response,\n };\n },\n );\n } catch (error) {\n diagnostics.lastError = error instanceof Error ? error.message : String(error);\n throw error;\n } finally {\n diagnostics.activeRequests -= 1;\n diagnostics.lifecycleState = \"idle\";\n }\n }\n\n return {\n handle: handleRequest,\n ws: {\n open: async (connectionId) => {\n await server.fetch.dispatchWebSocketOpen(connectionId);\n },\n message: async (connectionId, data) => {\n await server.fetch.dispatchWebSocketMessage(connectionId, data);\n },\n close: async (connectionId, code, reason) => {\n await server.fetch.dispatchWebSocketClose(connectionId, code, reason);\n },\n error: async (connectionId, error) => {\n await server.fetch.dispatchWebSocketError(connectionId, error);\n },\n },\n reload: async (reason) => {\n diagnostics.lifecycleState = \"reloading\";\n try {\n bindingsAdapter.reset(reason ? `AppServer.reload(${reason})` : \"AppServer.reload()\");\n await server.reload(reason);\n runtimeId = server.getRuntime()?.id ?? options.key;\n } finally {\n diagnostics.lifecycleState = \"idle\";\n }\n },\n dispose: async (disposeOptions) => {\n diagnostics.lifecycleState = \"disposing\";\n try {\n if (disposeOptions?.hard) {\n const hardDisposeReason = disposeOptions?.reason\n ? `AppServer.dispose(hard): ${disposeOptions.reason}`\n : \"AppServer.dispose(hard)\";\n bindingsAdapter.reset(hardDisposeReason);\n await server.reload(hardDisposeReason);\n bindingsAdapter.abort(hardDisposeReason);\n await server.close(hardDisposeReason);\n return;\n }\n const disposeReason = disposeOptions?.reason\n ? `AppServer.dispose(): ${disposeOptions.reason}`\n : \"AppServer.dispose()\";\n bindingsAdapter.abort(disposeReason);\n await server.close(disposeReason);\n } finally {\n diagnostics.lifecycleState = \"idle\";\n }\n },\n diagnostics: async () => ({\n ...diagnostics,\n reused: server.getRuntime()?.reused,\n }),\n };\n}\n"
6
6
  ],
7
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAA8B,IAA9B;AAEyC,IAAzC;AAC6C,IAA7C;AACmC,IAAnC;AAGA,eAAsB,sBAAsB,CAC1C,eACA,SACoB;AAAA,EACpB,MAAM,cAAc,4CAAyB;AAAA,EAC7C,MAAM,SAAS,IAAI,4BAAc;AAAA,IAC/B,aAAa,QAAQ;AAAA,IACrB;AAAA,EACF,CAAC;AAAA,EAED,IAAI,YAAY,QAAQ;AAAA,EACxB,MAAM,kBAAkB,qDACtB,QAAQ,UACR,MAAM,WACN,WACF;AAAA,EACA,MAAM,OAAO,MAAM;AAAA,IACjB,OAAO,QAAQ;AAAA,IACf,eAAe,QAAQ;AAAA,IACvB,gBAAgB;AAAA,SACX,gBAAgB;AAAA,MACnB,KAAK,QAAQ;AAAA,MACb,eAAe,QAAQ;AAAA,MACvB,kBAAkB,QAAQ;AAAA,MAC1B,iBAAiB,QAAQ,UAAU,SAAS;AAAA,IAC9C;AAAA,IACA,oBAAoB,CAAC,YAAY;AAAA,MAC/B,QAAQ,YAAY,YAAY,OAAO;AAAA;AAAA,EAE3C,CAAC;AAAA,EACD,YAAY,OAAO,WAAW,GAAG,MAAM,QAAQ;AAAA,EAE/C,eAAe,aAAa,CAAC,SAAkB,eAAyH;AAAA,IACt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,YAAY,iBAAiB;AAAA;AAAA;AAAA,IAGjC,aAAa,aAAa;AAAA,SACrB;AAAA,MACH,QAAQ,OAAO,WAAW,GAAG;AAAA,IAC/B;AAAA,EACF;AAAA;",
8
+ "debugId": "34E29BF0A88C356464756E2164756E21",
9
9
  "names": []
10
10
  }
@@ -287,6 +287,45 @@ function logRuntimeLifecycle(state, event, instance, reason, level = "log") {
287
287
  const reasonSuffix = reason ? `: ${reason}` : "";
288
288
  logger(`[isolate-daemon] ${event} runtime ${formatRuntimeLabel(instance)}${poisonedSuffix}${reasonSuffix}; ${formatRuntimePoolSnapshot(collectRuntimePoolSnapshot(state))}`);
289
289
  }
290
+ function formatDebugValue(value) {
291
+ if (typeof value === "string") {
292
+ return JSON.stringify(value);
293
+ }
294
+ if (typeof value === "number" || typeof value === "boolean" || value === null) {
295
+ return String(value);
296
+ }
297
+ try {
298
+ return JSON.stringify(value);
299
+ } catch {
300
+ return JSON.stringify(String(value));
301
+ }
302
+ }
303
+ function formatDebugFields(fields) {
304
+ return Object.entries(fields).filter(([, value]) => value !== undefined).map(([key, value]) => `${key}=${formatDebugValue(value)}`).join(" ");
305
+ }
306
+ function isDisposedOperationError(error) {
307
+ const text = getErrorText(error).toLowerCase();
308
+ return text.includes("disposed");
309
+ }
310
+ function buildRuntimeDebugFields(instance, connection, extras = {}) {
311
+ return {
312
+ isolateId: instance.isolateId,
313
+ namespaceId: instance.namespaceId ?? null,
314
+ pooled: instance.isDisposed,
315
+ ownerMatches: instance.ownerConnection === connection.socket,
316
+ connectionOwnsIsolate: connection.isolates.has(instance.isolateId),
317
+ runtimeAbortAborted: instance.runtimeAbortController?.signal.aborted ?? null,
318
+ disposedForMs: instance.disposedAt !== undefined ? Date.now() - instance.disposedAt : null,
319
+ callbackConnectionPresent: !!instance.callbackContext?.connection,
320
+ reconnectPending: !!instance.callbackContext?.reconnectionPromise,
321
+ callbackRegistrations: instance.callbacks.size,
322
+ ...extras
323
+ };
324
+ }
325
+ function logSuspiciousRuntimeOperation(state, operation, instance, connection, extras = {}, level = "warn") {
326
+ const logger = level === "warn" ? console.warn : console.log;
327
+ logger(`[isolate-daemon] ${operation} runtime ${formatRuntimeLabel(instance)}; ${formatRuntimePoolSnapshot(collectRuntimePoolSnapshot(state))} ${formatDebugFields(buildRuntimeDebugFields(instance, connection, extras))}`);
328
+ }
290
329
  async function hardDeleteRuntime(instance, state, reason) {
291
330
  const wasPooled = instance.isDisposed;
292
331
  try {
@@ -319,6 +358,8 @@ async function hardDeleteRuntime(instance, state, reason) {
319
358
  }
320
359
  var RECONNECTION_TIMEOUT_MS = 30000;
321
360
  function softDeleteRuntime(instance, state, reason) {
361
+ const runtimeAbortWasAborted = instance.runtimeAbortController?.signal.aborted ?? false;
362
+ const hadCallbackConnection = !!instance.callbackContext?.connection;
322
363
  if (instance.runtimeAbortController && !instance.runtimeAbortController.signal.aborted) {
323
364
  instance.runtimeAbortController.abort(new Error(reason ?? "Runtime was soft-disposed"));
324
365
  }
@@ -356,8 +397,19 @@ function softDeleteRuntime(instance, state, reason) {
356
397
  instance.returnedIterators?.clear();
357
398
  instance.runtime.clearModuleCache();
358
399
  logRuntimeLifecycle(state, "soft-disposed", instance, reason);
400
+ console.warn(`[isolate-daemon] soft-dispose state runtime ${formatRuntimeLabel(instance)}; ${formatRuntimePoolSnapshot(collectRuntimePoolSnapshot(state))} ${formatDebugFields({
401
+ runtimeAbortWasAborted,
402
+ runtimeAbortAborted: instance.runtimeAbortController?.signal.aborted ?? null,
403
+ hadCallbackConnection,
404
+ callbackConnectionPresent: !!instance.callbackContext?.connection,
405
+ reconnectPending: !!instance.callbackContext?.reconnectionPromise,
406
+ callbackRegistrations: instance.callbacks.size
407
+ })}`);
359
408
  }
360
409
  function reuseNamespacedRuntime(instance, connection, message, state) {
410
+ const disposedForMs = instance.disposedAt !== undefined ? Date.now() - instance.disposedAt : null;
411
+ const runtimeAbortWasAborted = instance.runtimeAbortController?.signal.aborted ?? false;
412
+ const reconnectWasPending = !!instance.callbackContext?.reconnectionPromise;
361
413
  instance.ownerConnection = connection.socket;
362
414
  instance.isDisposed = false;
363
415
  instance.isPoisoned = false;
@@ -468,6 +520,11 @@ function reuseNamespacedRuntime(instance, connection, message, state) {
468
520
  instance.returnedIterators = new Map;
469
521
  instance.nextLocalCallbackId = 1e6;
470
522
  logRuntimeLifecycle(state, "reused pooled", instance);
523
+ logSuspiciousRuntimeOperation(state, "reuse state", instance, connection, {
524
+ disposedForMs,
525
+ runtimeAbortWasAborted,
526
+ reconnectWasPending
527
+ }, "log");
471
528
  }
472
529
  async function waitForConnection(callbackContext) {
473
530
  if (callbackContext.connection) {
@@ -1138,6 +1195,12 @@ async function handleDispatchRequest(message, connection, state) {
1138
1195
  sendError(connection.socket, message.requestId, ErrorCode.ISOLATE_NOT_FOUND, `Isolate not found: ${message.isolateId}`);
1139
1196
  return;
1140
1197
  }
1198
+ if (instance.isDisposed) {
1199
+ logSuspiciousRuntimeOperation(state, "dispatchRequest received for pooled", instance, connection, {
1200
+ method: message.request.method,
1201
+ url: message.request.url
1202
+ });
1203
+ }
1141
1204
  instance.lastActivity = Date.now();
1142
1205
  const dispatchAbortController = new AbortController;
1143
1206
  connection.dispatchAbortControllers.set(message.requestId, dispatchAbortController);
@@ -1183,6 +1246,13 @@ async function handleDispatchRequest(message, connection, state) {
1183
1246
  }
1184
1247
  } catch (err) {
1185
1248
  const error = err;
1249
+ if (instance.isDisposed || isDisposedOperationError(error)) {
1250
+ logSuspiciousRuntimeOperation(state, "dispatchRequest failed", instance, connection, {
1251
+ method: message.request.method,
1252
+ url: message.request.url,
1253
+ error: error.message
1254
+ });
1255
+ }
1186
1256
  sendError(connection.socket, message.requestId, ErrorCode.SCRIPT_ERROR, error.message, { name: error.name, stack: error.stack });
1187
1257
  } finally {
1188
1258
  connection.dispatchAbortControllers.delete(message.requestId);
@@ -1301,12 +1371,20 @@ async function handleFetchGetUpgradeRequest(message, connection, state) {
1301
1371
  sendError(connection.socket, message.requestId, ErrorCode.ISOLATE_NOT_FOUND, `Isolate not found: ${message.isolateId}`);
1302
1372
  return;
1303
1373
  }
1374
+ if (instance.isDisposed) {
1375
+ logSuspiciousRuntimeOperation(state, "getUpgradeRequest received for pooled", instance, connection);
1376
+ }
1304
1377
  instance.lastActivity = Date.now();
1305
1378
  try {
1306
1379
  const upgradeRequest = instance.runtime.fetch.getUpgradeRequest();
1307
1380
  sendOk(connection.socket, message.requestId, upgradeRequest);
1308
1381
  } catch (err) {
1309
1382
  const error = err;
1383
+ if (instance.isDisposed || isDisposedOperationError(error)) {
1384
+ logSuspiciousRuntimeOperation(state, "getUpgradeRequest failed", instance, connection, {
1385
+ error: error.message
1386
+ });
1387
+ }
1310
1388
  sendError(connection.socket, message.requestId, ErrorCode.SCRIPT_ERROR, error.message, { name: error.name, stack: error.stack });
1311
1389
  }
1312
1390
  }
@@ -1316,12 +1394,20 @@ async function handleFetchHasServeHandler(message, connection, state) {
1316
1394
  sendError(connection.socket, message.requestId, ErrorCode.ISOLATE_NOT_FOUND, `Isolate not found: ${message.isolateId}`);
1317
1395
  return;
1318
1396
  }
1397
+ if (instance.isDisposed) {
1398
+ logSuspiciousRuntimeOperation(state, "hasServeHandler received for pooled", instance, connection);
1399
+ }
1319
1400
  instance.lastActivity = Date.now();
1320
1401
  try {
1321
1402
  const hasHandler = instance.runtime.fetch.hasServeHandler();
1322
1403
  sendOk(connection.socket, message.requestId, hasHandler);
1323
1404
  } catch (err) {
1324
1405
  const error = err;
1406
+ if (instance.isDisposed || isDisposedOperationError(error)) {
1407
+ logSuspiciousRuntimeOperation(state, "hasServeHandler failed", instance, connection, {
1408
+ error: error.message
1409
+ });
1410
+ }
1325
1411
  sendError(connection.socket, message.requestId, ErrorCode.SCRIPT_ERROR, error.message, { name: error.name, stack: error.stack });
1326
1412
  }
1327
1413
  }
@@ -1331,12 +1417,20 @@ async function handleFetchHasActiveConnections(message, connection, state) {
1331
1417
  sendError(connection.socket, message.requestId, ErrorCode.ISOLATE_NOT_FOUND, `Isolate not found: ${message.isolateId}`);
1332
1418
  return;
1333
1419
  }
1420
+ if (instance.isDisposed) {
1421
+ logSuspiciousRuntimeOperation(state, "hasActiveConnections received for pooled", instance, connection);
1422
+ }
1334
1423
  instance.lastActivity = Date.now();
1335
1424
  try {
1336
1425
  const hasConnections = instance.runtime.fetch.hasActiveConnections();
1337
1426
  sendOk(connection.socket, message.requestId, hasConnections);
1338
1427
  } catch (err) {
1339
1428
  const error = err;
1429
+ if (instance.isDisposed || isDisposedOperationError(error)) {
1430
+ logSuspiciousRuntimeOperation(state, "hasActiveConnections failed", instance, connection, {
1431
+ error: error.message
1432
+ });
1433
+ }
1340
1434
  sendError(connection.socket, message.requestId, ErrorCode.SCRIPT_ERROR, error.message, { name: error.name, stack: error.stack });
1341
1435
  }
1342
1436
  }
@@ -1993,4 +2087,4 @@ export {
1993
2087
  handleConnection
1994
2088
  };
1995
2089
 
1996
- //# debugId=665A564AAE317D3264756E2164756E21
2090
+ //# debugId=46DF7C47935922C664756E2164756E21