{"record":{"id":"3ebfdbbd2b4694be","repo":"dotnet/runtime","slug":"diag-module-already-loaded","errorCode":null,"errorMessage":"Diag module already loaded","messagePattern":"Diag module already loaded","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/mono/browser/runtime/diagnostics/globals.ts","lineNumber":16,"sourceCode":"\n// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT license.\n\nimport type { DiagnosticHelpers, GlobalObjects, LoaderHelpers, RuntimeHelpers, DotnetModuleInternal } from \"../types/internal\";\n\nexport let _diagnosticModuleLoaded = false; // please keep it in place also as rollup guard\n\nexport let diagnosticHelpers: DiagnosticHelpers = null as any;\nexport let runtimeHelpers: RuntimeHelpers = null as any;\nexport let loaderHelpers: LoaderHelpers = null as any;\nexport let Module: DotnetModuleInternal = null as any;\n\nexport function setRuntimeGlobalsImpl (globalObjects: GlobalObjects): void {\n    if (_diagnosticModuleLoaded) {\n        throw new Error(\"Diag module already loaded\");\n    }\n    _diagnosticModuleLoaded = true;\n    diagnosticHelpers = globalObjects.diagnosticHelpers;\n    runtimeHelpers = globalObjects.runtimeHelpers;\n    loaderHelpers = globalObjects.loaderHelpers;\n    Module = globalObjects.module;\n}\n","sourceCodeStart":1,"sourceCodeEnd":24,"githubUrl":"https://github.com/dotnet/runtime/blob/60108ba66eb7d1d12f595480091b4ad80a24b172/src/mono/browser/runtime/diagnostics/globals.ts#L1-L24","documentation":"Thrown by `setRuntimeGlobalsImpl` in the diagnostics globals when it is invoked a second time within the same runtime instance. The diagnostics module is initialised exactly once; the `_diagnosticModuleLoaded` flag guards against double initialisation, which would otherwise rebind the diagnostic helpers and corrupt the websocket handle map.","triggerScenarios":"Calling `setRuntimeGlobals(globalObjects)` (index.ts:21) twice — typically because `dotnet.createDotnetRuntime` was called twice, or because the diagnostics entry point was imported twice (e.g. via two script tags or duplicate bundler entries). The second call hits the guard at globals.ts:15-17.","commonSituations":"Loading `dotnet.diagnostics.js` more than once on a page; calling `createDotnetRuntime` twice in tests without reloading; bundler misconfiguration that includes the diagnostics module in two chunks.","solutions":["Ensure `dotnet.createDotnetRuntime` (and thus `setRuntimeGlobals`) is called exactly once per page load.","In tests, reset the module registry (or reload the iframe/Worker) between cases instead of re-initialising.","Check for duplicate `<script>` tags or duplicate bundler imports of the diagnostics module."],"exampleFix":"// before: createDotnetRuntime called twice\nawait dotnet.createDotnetRuntime(opts1);\nawait dotnet.createDotnetRuntime(opts2); // throws 'Diag module already loaded'\n\n// after: create once and reuse the runtime\nconst runtime = await dotnet.createDotnetRuntime(opts1);\n// reuse `runtime` everywhere","handlingStrategy":"validation","validationCode":"function canInitDiag () {\n  return !_diagnosticModuleLoaded;\n}\nif (canInitDiag()) setRuntimeGlobals(globalObjects);","typeGuard":"function isDiagModuleFresh (): boolean {\n  return !_diagnosticModuleLoaded;\n}","tryCatchPattern":null,"preventionTips":["Call `createDotnetRuntime` exactly once per page/Worker.","Reload the Worker/iframe between tests that need a fresh runtime.","Audit bundler chunks so the diagnostics module appears in only one."],"tags":["diagnostics","lifecycle","singleton","initialization"],"backgroundTag":null,"analyzedSha":"60108ba66eb7d1d12f595480091b4ad80a24b172","analyzedAt":"2026-08-10T18:54:11.478Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}