{"record":{"id":"6dd369763377123f","repo":"microsoft/playwright","slug":"cannot-disconnect-from-this-worker","errorCode":null,"errorMessage":"Cannot disconnect from this worker","messagePattern":"Cannot disconnect from this worker","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/playwright-core/src/server/page.ts","lineNumber":1043,"sourceCode":"\n  didClose() {\n    if (this.existingExecutionContext)\n      this.existingExecutionContext.contextDestroyed('Worker was closed');\n    this.emit(Worker.Events.Close, this);\n    this.openScope.close(new Error('Worker closed'));\n  }\n\n  async evaluateExpression(progress: Progress, expression: string, isFunction: boolean | undefined, arg: any): Promise<any> {\n    return progress.race(js.evaluateExpression(await this._executionContextPromise, expression, { returnByValue: true, isFunction }, arg));\n  }\n\n  async evaluateExpressionHandle(progress: Progress, expression: string, isFunction: boolean | undefined, arg: any): Promise<any> {\n    return progress.race(js.evaluateExpression(await this._executionContextPromise, expression, { returnByValue: false, isFunction }, arg));\n  }\n\n  async disconnect(progress: Progress, options: { reason?: string } = {}) {\n    if (!this._onDisconnect)\n      throw new Error('Cannot disconnect from this worker');\n    this._closeReason = options.reason;\n    await progress.race(this._onDisconnect());\n  }\n}\n\nexport class PageBinding extends DisposableObject {\n  static kBindingName = '__playwright__binding__';\n\n  static createInitScript(browserContext: BrowserContext): InitScript {\n    return new InitScript(browserContext, `\n      (() => {\n        const module = {};\n        ${rawBindingsControllerSource.source}\n        const property = '${kBindingsControllerProperty}';\n        if (!globalThis[property])\n          globalThis[property] = new (module.exports.BindingsController())(globalThis, '${PageBinding.kBindingName}');\n      })();\n    `);","sourceCodeStart":1025,"sourceCodeEnd":1061,"githubUrl":"https://github.com/microsoft/playwright/blob/c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6/packages/playwright-core/src/server/page.ts#L1025-L1061","documentation":"Thrown by Worker.disconnect when the Worker was constructed without an onDisconnect callback. The Worker object (for dedicated/service workers) only supports an explicit disconnect when the browser backend supplied a disconnect handler; otherwise the operation is unsupported for that worker type.","triggerScenarios":"Calling the internal Worker disconnect path on a worker whose backend did not register a disconnect routine (e.g. a service worker or a worker type whose lifecycle is managed entirely by the browser). Triggered through low-level protocol use or an internal dispatcher, not a typical user API.","commonSituations":"Custom integrations that drive the server-side Worker API directly; calling disconnect on a worker created by a backend that does not support manual disconnect.","solutions":["Do not call disconnect on worker types that do not provide a disconnect handler; let the worker close naturally or close the page/context that owns it.","If you maintain a custom backend, supply an onDisconnect callback when constructing the Worker so the operation is supported.","Prefer detaching via page/context teardown instead of a per-worker disconnect."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// Only call disconnect when the worker advertises support for it.\n// In user code, prefer letting the owning page/context drive worker teardown.\nfunction canDisconnect(worker) {\n  // No public flag exists; default to NOT calling disconnect and rely on page/context close.\n  return false;\n}","tryCatchPattern":"try {\n  await worker.disconnect();\n} catch (e) {\n  if (/Cannot disconnect from this worker/.test(e.message)) {\n    // unsupported worker type: close the owning page/context instead\n  } else throw e;\n}","preventionTips":["Do not call the internal Worker disconnect API for worker types that do not support it.","Drive worker teardown through page/context lifecycle.","If implementing a custom backend, supply an onDisconnect callback when constructing the Worker."],"tags":["worker","disconnect","internal","lifecycle"],"backgroundTag":null,"analyzedSha":"c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6","analyzedAt":"2026-08-12T07:26:36.950Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}