Fix scope registry race condition
Lint / lint (push) Successful in 1m43s

This commit is contained in:
Julien Valverdé
2026-07-20 02:15:39 +02:00
parent 4ba7943bef
commit 8bbeed9b8d
+12 -7
View File
@@ -1,6 +1,6 @@
/** biome-ignore-all lint/complexity/noBannedTypes: {} is the default type for React props */
/** biome-ignore-all lint/complexity/useArrowFunction: necessary for class prototypes */
import { Context, type Duration, Effect, Equivalence, Exit, Fiber, Function, HashMap, identity, Layer, Option, Pipeable, Predicate, Ref, Scope, Tracer } from "effect"
import { Cause, Context, type Duration, Effect, Equal, Equivalence, Exit, Fiber, Function, HashMap, identity, Layer, Option, Pipeable, Predicate, Ref, Scope, Tracer } from "effect"
import * as React from "react"
@@ -698,13 +698,13 @@ export const useScope = Effect.fnUntraced(function*(
ScopeMap as unknown as Effect.Effect<ScopeMap["Service"]>,
scopeMap => scopeMap.ref,
)),
Effect.let("key", () => ({})),
Effect.let("key", () => Equal.byReference({})),
Effect.bind("scope", () => Scope.make(options?.finalizerExecutionStrategy ?? defaultOptions.finalizerExecutionStrategy)),
Effect.tap(({ scopeMapRef, key, scope }) =>
Ref.update(scopeMapRef, HashMap.set(key, {
Ref.update(scopeMapRef, HashMap.set(key, Equal.byReference({
scope,
closeFiber: Option.none(),
}))
})))
),
// biome-ignore lint/correctness/useExhaustiveDependencies: use of React.DependencyList
)), deps)
@@ -724,13 +724,18 @@ export const useScope = Effect.fnUntraced(function*(
() => Effect.runSyncWith(contextRef.current)(Effect.flatMap(
Effect.sleep(options?.finalizerExecutionDebounce ?? defaultOptions.finalizerExecutionDebounce).pipe(
Effect.andThen(Scope.close(scope, Exit.void)),
Effect.onExit(() => Ref.update(ref, HashMap.remove(key))),
Effect.onExit(exit => Exit.match(exit, {
onSuccess: () => Ref.update(ref, HashMap.remove(key)),
onFailure: cause => Cause.hasInterruptsOnly(cause)
? Effect.void
: Ref.update(ref, HashMap.remove(key)),
})),
Effect.forkDetach,
),
fiber => Ref.update(ref, HashMap.set(key, {
fiber => Ref.update(ref, HashMap.set(key, Equal.byReference({
scope,
closeFiber: Option.some(fiber),
})),
}))),
))
),
)