{"record":{"id":"0b9de0c063391eb4","repo":"mastra-ai/mastra","slug":"factory-storage-domain-this-name-is-already-b","errorCode":null,"errorMessage":"Factory storage domain '${this.name}' is already bound to another storage instance","messagePattern":"Factory storage domain '(.+?)' is already bound to another storage instance","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/factory-storage.ts","lineNumber":230,"sourceCode":" * Base class for application domains owned by a {@link FactoryStorage}.\n * Domains are bound once when registered and share their owner's connection.\n */\nexport abstract class FactoryStorageDomain extends StorageDomain {\n  override readonly name: string;\n  #storage?: FactoryStorage;\n\n  protected constructor(name: string) {\n    if (!name.trim()) {\n      throw new Error('Factory storage domain name must not be empty');\n    }\n    super({ component: 'STORAGE', name });\n    this.name = name;\n  }\n\n  /** @internal Bound by {@link FactoryStorage.registerDomain}. */\n  __bindFactoryStorage(storage: FactoryStorage): void {\n    if (this.#storage && this.#storage !== storage) {\n      throw new Error(`Factory storage domain '${this.name}' is already bound to another storage instance`);\n    }\n    this.#storage = storage;\n  }\n\n  protected get storage(): FactoryStorage {\n    if (!this.#storage) {\n      throw new Error(`Factory storage domain '${this.name}' has not been registered`);\n    }\n    return this.#storage;\n  }\n\n  /**\n   * Initialize this domain (via its owning storage) if it hasn't been yet.\n   * Lets consumers holding a domain handle run the same fail-soft readiness\n   * check as {@link FactoryStorage.ensureDomainReady} without also needing a\n   * reference to the storage backend.\n   */\n  ensureReady(): Promise<void> {","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/factory-storage.ts#L212-L248","documentation":"A FactoryStorageDomain instance can only ever be attached to one FactoryStorage instance. __bindFactoryStorage (called from FactoryStorage.registerDomain) throws if the domain is already bound to a different storage, preventing cross-instance leakage where a domain would read/write the wrong backing storage.","triggerScenarios":"Registering the same domain instance with two different FactoryStorage instances; reusing a singleton domain across two Mastra storages; re-registering domains after replacing the storage object in tests; accidentally exporting one domain instance and importing it in two configurations.","commonSituations":"Test setups that build multiple FactoryStorage instances but share domain instances; hot-reload/duplicate-module issues creating two storage instances that both bind the same domain object; dependency-injection wiring that passes a cached domain into several storages.","solutions":["Create a fresh domain instance per FactoryStorage instead of sharing one","If rebinding is intentional, construct a new domain rather than reusing the bound one","Audit DI/test setup so each storage gets its own set of domains (factory functions help: () => new WorkspacesDomain())","Check for duplicate module instantiation (double imports / ESM+CJS mixing) producing two FactoryStorage instances"],"exampleFix":"// before\nconst workspaces = new WorkspacesDomain();\nawait storageA.registerDomain(workspaces);\nawait storageB.registerDomain(workspaces); // throws\n// after\nawait storageA.registerDomain(new WorkspacesDomain());\nawait storageB.registerDomain(new WorkspacesDomain());","handlingStrategy":"try-catch","validationCode":"function canBind(domain: FactoryStorageDomain, storage: FactoryStorage): boolean {\n  // attempt binding on a throwaway basis via the public registerDomain API in tests\n  return !(domain as unknown as { #storage?: FactoryStorage }).hasOwnProperty?.('storage');\n}","typeGuard":null,"tryCatchPattern":"try {\n  await storage.registerDomain(domain);\n} catch (err) {\n  if (err instanceof Error && err.message.includes('already bound to another storage instance')) {\n    await storage.registerDomain(new (domain.constructor as new () => typeof domain)());\n  } else {\n    throw err;\n  }\n}","preventionTips":["Use factory functions to create fresh domain instances per storage","Never register a singleton domain with multiple FactoryStorage instances","In tests, build storage + domains together in a makeStorage() helper","Watch for duplicate module imports that create two storage instances during hot reload"],"tags":["storage","factory","lifecycle","dependency-injection"],"backgroundTag":"singleton-rebinding-conflict","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}