Skip to content

Commit

Permalink
Avoid putting symbols in global to fix incompatibility with Temporal …
Browse files Browse the repository at this point in the history
…Sandbox. (#4196)
  • Loading branch information
mikearnaldi authored Dec 27, 2024
1 parent 302b57d commit 39db211
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .changeset/sweet-bears-help.md
Original file line number Diff line number Diff line change
@@ -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.
14 changes: 8 additions & 6 deletions packages/effect/src/GlobalValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown, any>
let globalStore: Map<unknown, any>

/**
* Retrieves or computes a global value associated with the given `id`. If the value for this `id`
Expand All @@ -46,6 +42,12 @@ const globalStore = (globalThis as any)[globalStoreId] as Map<unknown, any>
* @since 2.0.0
*/
export const globalValue = <A>(id: unknown, compute: () => A): A => {
if (!globalStore) {
// @ts-expect-error
globalThis[globalStoreId] ??= new Map()
// @ts-expect-error
globalStore = globalThis[globalStoreId] as Map<unknown, any>
}
if (!globalStore.has(id)) {
globalStore.set(id, compute())
}
Expand Down

0 comments on commit 39db211

Please sign in to comment.