Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/delimiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class Delimiter<T>
finalized = false;
future = withResolvers<Maybe<Result<T>>>();
computed = false;
settling = false;
routine?: Coroutine;
outcome?: Maybe<Result<T>>;

Expand All @@ -33,6 +34,10 @@ export class Delimiter<T>
this.exit(Nothing());
}

settle(): void {
this.settling = true;
}

*close(): Operation<void> {
let done = this.future.operation;
let interrupted = !this.computed;
Expand All @@ -46,6 +51,8 @@ export class Delimiter<T>
if (!this.outcome) {
this.interrupt();
yield* this.close();
} else if (!this.finalized) {
yield* this.close();
} else {
if (interrupted && this.outcome.exists && !this.outcome.value.ok) {
throw this.outcome.value.error;
Expand All @@ -65,7 +72,7 @@ export class Delimiter<T>
if (!this.routine) {
this.finalized = true;
this.future.resolve(this.outcome);
} else {
} else if (!this.settling) {
this.routine.return(Ok(this.outcome));
}
}
Expand Down
11 changes: 10 additions & 1 deletion lib/task-group.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createContext } from "./context.ts";
import { box } from "./box.ts";
import { DelimiterContext } from "./delimiter.ts";
import { Ok, unbox } from "./result.ts";
import { useScope } from "./scope.ts";
import type { Operation, Task } from "./types.ts";

export class TaskGroup {
Expand Down Expand Up @@ -40,7 +42,14 @@ export function encapsulate<T>(operation: () => Operation<T>): Operation<T> {
try {
return yield* operation();
} finally {
yield* group.halt();
let scope = yield* useScope();
let delimiter = scope.expect(DelimiterContext);
delimiter.settle();
try {
yield* group.halt();
} finally {
delimiter.settling = false;
}
}
});
}
28 changes: 28 additions & 0 deletions test/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Children } from "../lib/contexts.ts";
import {
action,
createScope,
resource,
run,
type Scope,
sleep,
Expand Down Expand Up @@ -166,6 +167,33 @@ describe("run()", () => {
expect(completed).toEqual(true);
});

it("halts only after resource cleanup finishes", async () => {
let events: string[] = [];
let entered = Promise.withResolvers<void>();

let task = run(function* () {
yield* resource(function* (provide) {
try {
yield* provide("resource");
} finally {
events.push("cleanup:entered");
entered.resolve();
yield* sleep(50);
events.push("cleanup:finished");
}
});
});

await entered.promise;

await task.halt();
events.push("halt:resolved");

expect(events.indexOf("halt:resolved")).toBeGreaterThan(
events.indexOf("cleanup:finished"),
);
});

it("can suspend in yielded finally block", async () => {
let things: string[] = [];

Expand Down
Loading