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
2 changes: 1 addition & 1 deletion packages/cloudflare/src/flush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const flushLockRegistries = new WeakMap<ExecutionContext['waitUntil'], FlushLock
*
* By using the original waitUntil for flush operations, we bypass this issue.
*/
export function getOriginalWaitUntil(context: ExecutionContextCompat): ExecutionContext['waitUntil'] | undefined {
export function getOriginalWaitUntil(context: ExecutionContextCompat): ExecutionContext['waitUntil'] {
// eslint-disable-next-line @typescript-eslint/unbound-method
const currentWaitUntil = context.waitUntil;
const original = flushLockRegistries.get(currentWaitUntil)?.originalWaitUntil;
Expand Down
2 changes: 1 addition & 1 deletion packages/cloudflare/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function wrapRequestHandlerWithInit(
// to track pending tasks. If we use the instrumented version for flushAndDispose,
// it acquires the lock, then flushAndDispose tries to wait for the same lock,
// creating a deadlock.
const waitUntil = context ? getOriginalWaitUntil(context)?.bind(context) : undefined;
const waitUntil = context ? getOriginalWaitUntil(context).bind(context) : undefined;
const errorMechanismType = getRequestErrorMechanismType(context);

const client = initSdk({ ...options, ctx: context });
Expand Down
4 changes: 2 additions & 2 deletions packages/cloudflare/src/workflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type {
} from 'cloudflare:workers';
import { setAsyncLocalStorageAsyncContextStrategy } from '@sentry/server-utils/no-diagnostic-channels';
import type { CloudflareOptions } from './client';
import { flushAndDispose } from './flush';
import { flushAndDispose, getOriginalWaitUntil } from './flush';
import { instrumentEnv } from './instrumentations/worker/instrumentEnv';
import { addCloudResourceContext } from './scope-utils';
import { init } from './sdk';
Expand Down Expand Up @@ -218,7 +218,7 @@ export function instrumentWorkflowWithSentry<
setAsyncLocalStorageAsyncContextStrategy();

return withIsolationScope(async isolationScope => {
const waitUntil = context.waitUntil.bind(context);
const waitUntil = getOriginalWaitUntil(context).bind(context);
const client = init({ ...options, ctx: context, enableDedupe: false });
isolationScope.setClient(client);

Expand Down
6 changes: 3 additions & 3 deletions packages/cloudflare/test/flush.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe('getOriginalWaitUntil', () => {

expect(result).not.toBe(context.waitUntil);
expect(result).toBeDefined();
result!(Promise.resolve());
result(Promise.resolve());
expect(originalWaitUntil).toHaveBeenCalled();
});

Expand All @@ -183,7 +183,7 @@ describe('getOriginalWaitUntil', () => {
const result = getOriginalWaitUntil(context);

expect(result).not.toBe(context.waitUntil);
result!(Promise.resolve());
result(Promise.resolve());
expect(originalWaitUntil).toHaveBeenCalled();
});

Expand All @@ -207,7 +207,7 @@ describe('getOriginalWaitUntil', () => {
} as unknown as Client;

const originalWaitUntil = getOriginalWaitUntil(context);
originalWaitUntil!.call(context, flushAndDispose(mockClient));
originalWaitUntil.call(context, flushAndDispose(mockClient));

await vi.waitFor(() => Promise.all(waitUntilPromises));
expect(mockClient.flush).toHaveBeenCalled();
Expand Down
111 changes: 111 additions & 0 deletions packages/cloudflare/test/workflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,117 @@
await expect(drainWaitUntilLikeCloudflareVitestPool(waitUntilPromises)).resolves.toBeUndefined();
});

test('teardown does not deadlock when a workflow instance is reused across runs', async () => {
const waitUntilPromises: Promise<unknown>[] = [];
const context: ExecutionContext = {
waitUntil: vi.fn((promise: Promise<unknown>) => {
waitUntilPromises.push(promise);
}),
passThroughOnException: vi.fn(),
props: {},
};

let runCount = 0;
let releaseAppWork: () => void = () => undefined;

class ReusedWorkflow {
public constructor(private _ctx: ExecutionContext) {}

public async run(_event: Readonly<WorkflowEvent<Params>>, step: WorkflowStep): Promise<void> {
runCount += 1;
await step.do('reused step', async () => {
if (runCount === 2) {
this._ctx.waitUntil(
new Promise<void>(resolve => {
releaseAppWork = resolve;
}),
);
}
});
}
}

const TestWorkflowInstrumented = instrumentWorkflowWithSentry(getSentryOptions, ReusedWorkflow as any);
// Cloudflare reuses a Workflow instance across runs, so the context
// captured at construction is instrumented by the first run's init()
const workflow = new TestWorkflowInstrumented(context, {}) as ReusedWorkflow;
const event = { payload: {}, timestamp: new Date(), instanceId: INSTANCE_ID };

await workflow.run(event, mockStep);
await drainWaitUntilLikeCloudflareVitestPool(waitUntilPromises);

await workflow.run(event, mockStep);

releaseAppWork();

// Both the application work and the teardown promise must settle
await expect(drainWaitUntilLikeCloudflareVitestPool(waitUntilPromises)).resolves.toBeUndefined();
});

test('step errors are still captured when a workflow instance is reused across runs', async () => {
const waitUntilPromises: Promise<unknown>[] = [];
const context: ExecutionContext = {
waitUntil: vi.fn((promise: Promise<unknown>) => {
waitUntilPromises.push(promise);
}),
passThroughOnException: vi.fn(),
props: {},
};

let shouldThrow = false;

class ReusedErrorWorkflow {
public constructor(private _ctx: ExecutionContext) {}

public async run(_event: Readonly<WorkflowEvent<Params>>, step: WorkflowStep): Promise<void> {
await step.do('flaky step', async () => {
if (shouldThrow) {
shouldThrow = false;
throw new Error('second run error');
}
});
}
}

const TestWorkflowInstrumented = instrumentWorkflowWithSentry(getSentryOptions, ReusedErrorWorkflow as any);
const workflow = new TestWorkflowInstrumented(context, {}) as ReusedErrorWorkflow;
const event = { payload: {}, timestamp: new Date(), instanceId: INSTANCE_ID };

await workflow.run(event, mockStep);
await drainWaitUntilLikeCloudflareVitestPool(waitUntilPromises);

shouldThrow = true;
await workflow.run(event, mockStep);
await drainWaitUntilLikeCloudflareVitestPool(waitUntilPromises);

expect(mockTransport.send).toHaveBeenCalledWith([

Check failure on line 295 in packages/cloudflare/test/workflow.test.ts

View workflow job for this annotation

GitHub Actions / Node (22) Unit Tests

test/workflow.test.ts > workflows > step errors are still captured when a workflow instance is reused across runs

AssertionError: expected "spy" to be called with arguments: [ [ ObjectContaining{…}, …(1) ] ] Received: 1st spy call: [ [ - ObjectContaining { - "trace": ObjectContaining { + { + "event_id": "e4ecf4fb05a142328ea3270f364c6412", + "sdk": { + "name": "sentry.javascript.cloudflare", + "version": "10.67.0", + }, + "sent_at": "2026-08-04T13:14:30.931Z", + "trace": { + "environment": "production", + "org_id": undefined, + "public_key": "8", + "release": "1.0.0", + "sample_rand": "0.44116884107728693", + "sample_rate": "1", + "sampled": "true", "trace_id": "ae0ee06761b3485292195d62282270f0", "transaction": "flaky step", }, }, [ [ { - "type": "event", + "type": "transaction", + }, + { + "breadcrumbs": undefined, + "contexts": { + "cloud_resource": { + "cloud.provider": "cloudflare", + }, + "runtime": { + "name": "cloudflare", + }, + "trace": { + "data": { + "cloudflare.workflow.attempt": 1, + "code.function.name": "flaky step", + "sentry.op": "function", + "sentry.origin": "auto.faas.cloudflare.workflow", + "sentry.sample_rate": 1, + "sentry.source": "task", + "workflow.step.name": "flaky step", + }, + "links": undefined, + "op": "function", + "origin": "auto.faas.cloudflare.workflow", + "parent_span_id": undefined, + "span_id": "a1332da7ffde275a", + "status": "ok", + "trace_id": "ae0ee06761b3485292195d62282270f0", }, - ObjectContaining { - "exception": { - "values": [ - ObjectContaining { - "mechanism": { - "handled": true, - "type": "auto.faas.cloudflare.workflow", }, - "type": "Error", - "value": "second run error", + "environment": "production", + "event_id": "e4ecf4fb05a142328ea3270f364c6412", + "platform": "javascript", + "release": "1.0.0", + "request": undefined, + "sdk": { + "integrations": [ + "InboundFilters", + "FunctionToString", + "ConversationId", + "LinkedErrors", + "Fetch", + "HttpServer", + "RequestData", + "Console", + "VercelAI", + ], + "name": "sentry.javascript.cloudflare", + "packages": [ + { + "name": "npm:@sentry/cloudflare", + "version": "10.67.0", }, ], + "settings": undefined, + "version": "10.67.0", + }, + "spans": [], + "start_timestamp": 1785849270.9292367, + "timestamp": 1785849270.929498, + "transaction": "flaky step", + "transaction_info": { + "source": "task", }, + "type": "transaction", }, ], ], ], ] 2nd spy call: [ [ - ObjectContaining { - "trace": ObjectContaining { + { + "event_id": "a078bb6a89f040e8a41e42171ad6dd0a", + "sdk": { + "name": "sentry.javascript.cloudflare", + "version": "10.67.0", + }, + "sent_at": "2026-08-04T13:14:30.935Z", + "trace": { + "environment": "production", + "org_id": undefined, + "public_key": "8", + "release": "1.0.0", + "sample_rand": "0.44116884107728693", + "sample_rate": "1", + "sampled": "true", "trace_id": "ae0ee06761b3485292195d62282270f0", "transaction

Check failure on line 295 in packages/cloudflare/test/workflow.test.ts

View workflow job for this annotation

GitHub Actions / Node (24) Unit Tests

test/workflow.test.ts > workflows > step errors are still captured when a workflow instance is reused across runs

AssertionError: expected "spy" to be called with arguments: [ [ ObjectContaining{…}, …(1) ] ] Received: 1st spy call: [ [ - ObjectContaining { - "trace": ObjectContaining { + { + "event_id": "593030ebb534406ea5f7ea984bc904ff", + "sdk": { + "name": "sentry.javascript.cloudflare", + "version": "10.67.0", + }, + "sent_at": "2026-08-04T13:14:33.276Z", + "trace": { + "environment": "production", + "org_id": undefined, + "public_key": "8", + "release": "1.0.0", + "sample_rand": "0.44116884107728693", + "sample_rate": "1", + "sampled": "true", "trace_id": "ae0ee06761b3485292195d62282270f0", "transaction": "flaky step", }, }, [ [ { - "type": "event", + "type": "transaction", + }, + { + "breadcrumbs": undefined, + "contexts": { + "cloud_resource": { + "cloud.provider": "cloudflare", + }, + "runtime": { + "name": "cloudflare", + }, + "trace": { + "data": { + "cloudflare.workflow.attempt": 1, + "code.function.name": "flaky step", + "sentry.op": "function", + "sentry.origin": "auto.faas.cloudflare.workflow", + "sentry.sample_rate": 1, + "sentry.source": "task", + "workflow.step.name": "flaky step", + }, + "links": undefined, + "op": "function", + "origin": "auto.faas.cloudflare.workflow", + "parent_span_id": undefined, + "span_id": "af928a67f33c3e2b", + "status": "ok", + "trace_id": "ae0ee06761b3485292195d62282270f0", }, - ObjectContaining { - "exception": { - "values": [ - ObjectContaining { - "mechanism": { - "handled": true, - "type": "auto.faas.cloudflare.workflow", }, - "type": "Error", - "value": "second run error", + "environment": "production", + "event_id": "593030ebb534406ea5f7ea984bc904ff", + "platform": "javascript", + "release": "1.0.0", + "request": undefined, + "sdk": { + "integrations": [ + "InboundFilters", + "FunctionToString", + "ConversationId", + "LinkedErrors", + "Fetch", + "HttpServer", + "RequestData", + "Console", + "VercelAI", + ], + "name": "sentry.javascript.cloudflare", + "packages": [ + { + "name": "npm:@sentry/cloudflare", + "version": "10.67.0", }, ], + "settings": undefined, + "version": "10.67.0", + }, + "spans": [], + "start_timestamp": 1785849273.2763715, + "timestamp": 1785849273.2766001, + "transaction": "flaky step", + "transaction_info": { + "source": "task", }, + "type": "transaction", }, ], ], ], ] 2nd spy call: [ [ - ObjectContaining { - "trace": ObjectContaining { + { + "event_id": "d72b38edd0374e109cf88e2d2094190b", + "sdk": { + "name": "sentry.javascript.cloudflare", + "version": "10.67.0", + }, + "sent_at": "2026-08-04T13:14:33.295Z", + "trace": { + "environment": "production", + "org_id": undefined, + "public_key": "8", + "release": "1.0.0", + "sample_rand": "0.44116884107728693", + "sample_rate": "1", + "sampled": "true", "trace_id": "ae0ee06761b3485292195d62282270f0", "transactio

Check failure on line 295 in packages/cloudflare/test/workflow.test.ts

View workflow job for this annotation

GitHub Actions / Node (26) Unit Tests

test/workflow.test.ts > workflows > step errors are still captured when a workflow instance is reused across runs

AssertionError: expected "spy" to be called with arguments: [ [ ObjectContaining{…}, …(1) ] ] Received: 1st spy call: [ [ - ObjectContaining { - "trace": ObjectContaining { + { + "event_id": "9a8a74fc3de949cabb08708697fc1c66", + "sdk": { + "name": "sentry.javascript.cloudflare", + "version": "10.67.0", + }, + "sent_at": "2026-08-04T13:14:38.323Z", + "trace": { + "environment": "production", + "org_id": undefined, + "public_key": "8", + "release": "1.0.0", + "sample_rand": "0.44116884107728693", + "sample_rate": "1", + "sampled": "true", "trace_id": "ae0ee06761b3485292195d62282270f0", "transaction": "flaky step", }, }, [ [ { - "type": "event", + "type": "transaction", + }, + { + "breadcrumbs": undefined, + "contexts": { + "cloud_resource": { + "cloud.provider": "cloudflare", + }, + "runtime": { + "name": "cloudflare", + }, + "trace": { + "data": { + "cloudflare.workflow.attempt": 1, + "code.function.name": "flaky step", + "sentry.op": "function", + "sentry.origin": "auto.faas.cloudflare.workflow", + "sentry.sample_rate": 1, + "sentry.source": "task", + "workflow.step.name": "flaky step", + }, + "links": undefined, + "op": "function", + "origin": "auto.faas.cloudflare.workflow", + "parent_span_id": undefined, + "span_id": "a88699146b4f1638", + "status": "ok", + "trace_id": "ae0ee06761b3485292195d62282270f0", }, - ObjectContaining { - "exception": { - "values": [ - ObjectContaining { - "mechanism": { - "handled": true, - "type": "auto.faas.cloudflare.workflow", }, - "type": "Error", - "value": "second run error", + "environment": "production", + "event_id": "9a8a74fc3de949cabb08708697fc1c66", + "platform": "javascript", + "release": "1.0.0", + "request": undefined, + "sdk": { + "integrations": [ + "InboundFilters", + "FunctionToString", + "ConversationId", + "LinkedErrors", + "Fetch", + "HttpServer", + "RequestData", + "Console", + "VercelAI", + ], + "name": "sentry.javascript.cloudflare", + "packages": [ + { + "name": "npm:@sentry/cloudflare", + "version": "10.67.0", }, ], + "settings": undefined, + "version": "10.67.0", + }, + "spans": [], + "start_timestamp": 1785849278.316474, + "timestamp": 1785849278.316677, + "transaction": "flaky step", + "transaction_info": { + "source": "task", }, + "type": "transaction", }, ], ], ], ] 2nd spy call: [ [ - ObjectContaining { - "trace": ObjectContaining { + { + "event_id": "16d811b4c3054f6ba4afd6c08e836e08", + "sdk": { + "name": "sentry.javascript.cloudflare", + "version": "10.67.0", + }, + "sent_at": "2026-08-04T13:14:38.329Z", + "trace": { + "environment": "production", + "org_id": undefined, + "public_key": "8", + "release": "1.0.0", + "sample_rand": "0.44116884107728693", + "sample_rate": "1", + "sampled": "true", "trace_id": "ae0ee06761b3485292195d62282270f0", "transaction"

Check failure on line 295 in packages/cloudflare/test/workflow.test.ts

View workflow job for this annotation

GitHub Actions / Node (20.19) Unit Tests

test/workflow.test.ts > workflows > step errors are still captured when a workflow instance is reused across runs

AssertionError: expected "spy" to be called with arguments: [ [ ObjectContaining{…}, …(1) ] ] Received: 1st spy call: [ [ - ObjectContaining { - "trace": ObjectContaining { + { + "event_id": "35824a7b94064a9e910b034613a3bf60", + "sdk": { + "name": "sentry.javascript.cloudflare", + "version": "10.67.0", + }, + "sent_at": "2026-08-04T13:14:37.807Z", + "trace": { + "environment": "production", + "org_id": undefined, + "public_key": "8", + "release": "1.0.0", + "sample_rand": "0.44116884107728693", + "sample_rate": "1", + "sampled": "true", "trace_id": "ae0ee06761b3485292195d62282270f0", "transaction": "flaky step", }, }, [ [ { - "type": "event", + "type": "transaction", + }, + { + "breadcrumbs": undefined, + "contexts": { + "cloud_resource": { + "cloud.provider": "cloudflare", + }, + "runtime": { + "name": "cloudflare", + }, + "trace": { + "data": { + "cloudflare.workflow.attempt": 1, + "code.function.name": "flaky step", + "sentry.op": "function", + "sentry.origin": "auto.faas.cloudflare.workflow", + "sentry.sample_rate": 1, + "sentry.source": "task", + "workflow.step.name": "flaky step", + }, + "links": undefined, + "op": "function", + "origin": "auto.faas.cloudflare.workflow", + "parent_span_id": undefined, + "span_id": "9a2d6de82f302701", + "status": "ok", + "trace_id": "ae0ee06761b3485292195d62282270f0", }, - ObjectContaining { - "exception": { - "values": [ - ObjectContaining { - "mechanism": { - "handled": true, - "type": "auto.faas.cloudflare.workflow", }, - "type": "Error", - "value": "second run error", + "environment": "production", + "event_id": "35824a7b94064a9e910b034613a3bf60", + "platform": "javascript", + "release": "1.0.0", + "request": undefined, + "sdk": { + "integrations": [ + "InboundFilters", + "FunctionToString", + "ConversationId", + "LinkedErrors", + "Fetch", + "HttpServer", + "RequestData", + "Console", + "VercelAI", + ], + "name": "sentry.javascript.cloudflare", + "packages": [ + { + "name": "npm:@sentry/cloudflare", + "version": "10.67.0", }, ], + "settings": undefined, + "version": "10.67.0", + }, + "spans": [], + "start_timestamp": 1785849277.807144, + "timestamp": 1785849277.8073943, + "transaction": "flaky step", + "transaction_info": { + "source": "task", }, + "type": "transaction", }, ], ], ], ] 2nd spy call: [ [ - ObjectContaining { - "trace": ObjectContaining { + { + "event_id": "26228abbf32a4001abb3bbd83c2d0f20", + "sdk": { + "name": "sentry.javascript.cloudflare", + "version": "10.67.0", + }, + "sent_at": "2026-08-04T13:14:37.822Z", + "trace": { + "environment": "production", + "org_id": undefined, + "public_key": "8", + "release": "1.0.0", + "sample_rand": "0.44116884107728693", + "sample_rate": "1", + "sampled": "true", "trace_id": "ae0ee06761b3485292195d62282270f0", "transaction
expect.objectContaining({
trace: expect.objectContaining({
transaction: 'flaky step',
trace_id: TRACE_ID,
}),
}),
[
[
{
type: 'event',
},
expect.objectContaining({
exception: {
values: [
expect.objectContaining({
type: 'Error',
value: 'second run error',
mechanism: { type: 'auto.faas.cloudflare.workflow', handled: true },
}),
],
},
}),
],
],
]);
});

test('Wraps env with instrumentEnv', async () => {
class EnvTestWorkflow {
constructor(_ctx: ExecutionContext, _env: unknown) {}
Expand Down
Loading