From 39db211414e90c8db8fdad7dc8ce5b4661bcfaef Mon Sep 17 00:00:00 2001 From: Michael Arnaldi Date: Fri, 27 Dec 2024 14:00:14 +0100 Subject: [PATCH] Avoid putting symbols in global to fix incompatibility with Temporal Sandbox. (#4196) --- .changeset/sweet-bears-help.md | 7 +++++++ packages/effect/src/GlobalValue.ts | 14 ++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 .changeset/sweet-bears-help.md diff --git a/.changeset/sweet-bears-help.md b/.changeset/sweet-bears-help.md new file mode 100644 index 00000000000..2c6039a95be --- /dev/null +++ b/.changeset/sweet-bears-help.md @@ -0,0 +1,7 @@ +--- +"effect": patch +--- + +Avoid putting symbols in global to fix incompatibility with Temporal Sandbox. + +After speaking with James Watkins-Harvey we realized current Effect escapes the Temporal Worker sandbox that doesn't look for symbols when restoring global context in the isolate they create leading to memory leaks. diff --git a/packages/effect/src/GlobalValue.ts b/packages/effect/src/GlobalValue.ts index 7308a15584f..5987615fa0c 100644 --- a/packages/effect/src/GlobalValue.ts +++ b/packages/effect/src/GlobalValue.ts @@ -14,13 +14,9 @@ */ import * as version from "./internal/version.js" -const globalStoreId = Symbol.for(`effect/GlobalValue/globalStoreId/${version.getCurrentVersion()}`) +const globalStoreId = `effect/GlobalValue/globalStoreId/${version.getCurrentVersion()}` -if (!(globalStoreId in globalThis)) { - ;(globalThis as any)[globalStoreId] = new Map() -} - -const globalStore = (globalThis as any)[globalStoreId] as Map +let globalStore: Map /** * Retrieves or computes a global value associated with the given `id`. If the value for this `id` @@ -46,6 +42,12 @@ const globalStore = (globalThis as any)[globalStoreId] as Map * @since 2.0.0 */ export const globalValue = (id: unknown, compute: () => A): A => { + if (!globalStore) { + // @ts-expect-error + globalThis[globalStoreId] ??= new Map() + // @ts-expect-error + globalStore = globalThis[globalStoreId] as Map + } if (!globalStore.has(id)) { globalStore.set(id, compute()) }