Skip to content

Commit a32df22

Browse files
committed
♻️ Let the resource halt its own producer (#806)
The region stream registered an `ensure` that halted the producer it had just spawned. It never did anything: a resource body is a task, `createTask` wraps it in `encapsulate()`, and that helper's `finally` runs the task group's `halt()`, which halts *and awaits* every spawned child in reverse order. The explicit call halted a task the next teardown step was about to halt anyway. What the comment claimed — that registering after the `spawn` is what makes teardown halt and join the producer before the resource returns — was the mechanism misread. Spawning inside this scope is already the whole of it. Nothing about the lifetime changes, which is the point: SR8 still asserts the exact order and still fails if the producer runs past the handler, verified by giving the handler two more reads and watching `ran:2` appear. Reported by review on PR #812.
1 parent 71dedc2 commit a32df22

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

‎packages/core/src/expansion-region.ts‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
* reads of one region are two expansions rather than two views of one. Leaving
2626
* the scope — a handler returning, failing, or being cancelled — halts that
2727
* producer and waits for it, which is what stops authored work from outliving
28-
* the invocation that asked for it.
28+
* the invocation that asked for it. That is the resource's own teardown doing
29+
* it, not anything arranged here.
2930
*
3031
* Cancellation stays cancellation: a halted producer never becomes an ordinary
3132
* failure, and a read halted while waiting takes its demand with it rather than
3233
* leaving one behind to swallow a later chunk.
3334
*/
3435

35-
import { ensure, Err, Ok, resource, spawn, withResolvers } from "effection";
36+
import { Err, Ok, resource, spawn, withResolvers } from "effection";
3637
import type { Operation, Result, Stream, Subscription } from "effection";
3738

3839
import type { ExpansionChunk } from "./execution-declarations.ts";
@@ -107,11 +108,12 @@ export function regionStream(produce: RegionProducer): Stream<ExpansionChunk, vo
107108
}
108109
}
109110

110-
// The producer is this subscription's own, and the `ensure` below is
111-
// registered after it so teardown halts and joins it before the resource
112-
// returns — a producer blocked on demand cannot survive the handler that
113-
// asked for it.
114-
const producer = yield* spawn(function* () {
111+
// The producer is this subscription's own, and spawning it here is what
112+
// binds its lifetime: this resource's body is a task, so leaving the scope
113+
// halts every child it spawned and waits for each one. A producer blocked
114+
// on demand therefore cannot survive the handler that asked for it, and
115+
// nothing here has to arrange that separately.
116+
yield* spawn(function* () {
115117
// Authored work starts when the first read does, not when the stream is
116118
// subscribed: a handler that holds a region and never reads it runs none
117119
// of its body.
@@ -156,10 +158,6 @@ export function regionStream(produce: RegionProducer): Stream<ExpansionChunk, vo
156158
},
157159
};
158160

159-
yield* ensure(function* () {
160-
yield* producer.halt();
161-
});
162-
163161
yield* provide(subscription);
164162
});
165163
}

0 commit comments

Comments
 (0)