Skip to content

Commit 00a48d2

Browse files
Ilya Privenmjameswh
andauthored
fix(worker): abort reason should be CancellationFailure (#1561)
Co-authored-by: James Watkins-Harvey <mjameswh@users.noreply.github.com>
1 parent 81884a6 commit 00a48d2

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

packages/test/src/activities/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,16 @@ export async function throwAnError(useApplicationFailure: boolean, message: stri
5555
}
5656
}
5757

58-
export async function waitForCancellation(): Promise<void> {
59-
await Context.current().cancelled;
58+
export async function waitForCancellation(throwIfAborted?: boolean): Promise<void> {
59+
try {
60+
await Context.current().cancelled;
61+
} catch (e) {
62+
if (throwIfAborted) {
63+
Context.current().cancellationSignal.throwIfAborted();
64+
} else {
65+
throw e;
66+
}
67+
}
6068
}
6169

6270
export async function fakeProgress(sleepIntervalMs = 1000, numIters = 1000): Promise<void> {

packages/test/src/test-worker-activities.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ test('Worker runs an activity and reports failure', async (t) => {
9999
});
100100
});
101101

102-
test('Worker cancels activity and reports cancellation', async (t) => {
102+
const workerCancelsActivityMacro = test.macro(async (t, throwIfAborted?: boolean) => {
103103
const { worker } = t.context;
104104
await runWorker(t, async () => {
105105
const taskToken = Buffer.from(uuid4());
@@ -109,7 +109,7 @@ test('Worker cancels activity and reports cancellation', async (t) => {
109109
start: {
110110
activityType: 'waitForCancellation',
111111
workflowExecution: { workflowId: 'wfid', runId: 'runId' },
112-
input: toPayloads(defaultPayloadConverter),
112+
input: toPayloads(defaultPayloadConverter, throwIfAborted),
113113
},
114114
},
115115
});
@@ -131,6 +131,10 @@ test('Worker cancels activity and reports cancellation', async (t) => {
131131
});
132132
});
133133

134+
test('Worker cancels activity and reports cancellation', workerCancelsActivityMacro);
135+
136+
test('Worker cancels activity and reports cancellation when using throwIfAborted', workerCancelsActivityMacro, true);
137+
134138
test('Activity Context AbortSignal cancels a fetch request', async (t) => {
135139
const { worker } = t.context;
136140
await runWorker(t, async () => {

packages/worker/src/activity.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ export class Activity {
5858
const promise = new Promise<never>((_, reject) => {
5959
this.cancel = (reason: CancelReason) => {
6060
this.cancelReason = reason;
61-
this.abortController.abort();
62-
reject(new CancelledFailure(reason));
61+
const err = new CancelledFailure(reason);
62+
this.abortController.abort(err);
63+
reject(err);
6364
};
6465
});
6566
this.context = new Context(

0 commit comments

Comments
 (0)