Skip to content

Commit

Permalink
Avoid treating completed requests as interrupted when race conditions…
Browse files Browse the repository at this point in the history
… occur (#2637)
  • Loading branch information
mikearnaldi committed Apr 27, 2024
1 parent 0517e3e commit 18de56b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-glasses-worry.md
@@ -0,0 +1,5 @@
---
"effect": patch
---

Avoid treating completed requests as interrupted when race conditions occur
23 changes: 20 additions & 3 deletions packages/effect/src/internal/fiberRuntime.ts
Expand Up @@ -3518,9 +3518,26 @@ export const invokeWithInterrupt: <A, E, R>(
const counts = entries.map((_) => _.listeners.count)
const checkDone = () => {
if (counts.every((count) => count === 0)) {
cleanup.forEach((f) => f())
onInterrupt?.()
cb(core.interruptFiber(processing))
if (
entries.every((_) => {
if (_.result.state.current._tag === "Pending") {
return true
} else if (
_.result.state.current._tag === "Done" &&
core.exitIsExit(_.result.state.current.effect) &&
_.result.state.current.effect._tag === "Failure" &&
internalCause.isInterrupted(_.result.state.current.effect.cause)
) {
return true
} else {
return false
}
})
) {
cleanup.forEach((f) => f())
onInterrupt?.()
cb(core.interruptFiber(processing))
}
}
}
processing.addObserver((exit) => {
Expand Down
16 changes: 16 additions & 0 deletions packages/effect/test/Effect/query.test.ts
Expand Up @@ -160,6 +160,22 @@ const EnvLive = Layer.mergeAll(
const provideEnv = Effect.provide(EnvLive)

describe("Effect", () => {
it.effect("avoid false interruption when concurrency happens in resolver", () =>
Effect.gen(function*() {
class RequestUserById extends Request.TaggedClass("RequestUserById")<number, never, {
id: string
}> {}
let count = 0
const resolver = Resolver.makeBatched((i) => {
count++
return Effect.forEach(i, Request.complete(Exit.succeed(1)), { concurrency: "unbounded" })
})
yield* Effect.request(new RequestUserById({ id: "1" }), resolver).pipe(
Effect.withRequestCaching(true),
Effect.repeatN(3)
)
expect(count).toBe(1)
}))
it.effect("requests are executed correctly", () =>
provideEnv(
Effect.gen(function*($) {
Expand Down

0 comments on commit 18de56b

Please sign in to comment.