Skip to content
Merged
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
29 changes: 21 additions & 8 deletions packages/cloudflare/src/flush.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ExecutionContext } from '@cloudflare/workers-types';
import type { Client } from '@sentry/core';
import { flush } from '@sentry/core';
import { debug, flush } from '@sentry/core';
import { DEBUG_BUILD } from './debug-build';

type FlushLock = {
readonly ready: Promise<void>;
Expand Down Expand Up @@ -118,17 +119,29 @@ function getOrCreateFlushLockRegistry(context: ExecutionContext): FlushLockRegis
* Flushes the client and then disposes of it to allow garbage collection.
* This should be called at the end of each request to prevent memory leaks.
*
* This function never rejects. On Workers, a rejected promise passed to
* `ctx.waitUntil` marks the whole invocation as `outcome: exception` even when
* the handler itself completed successfully. Since flush/dispose is internal
* SDK housekeeping, a failure here must not fail the user's invocation.
*
* @param client - The CloudflareClient instance to flush and dispose
* @param timeout - Timeout in milliseconds for the flush operation
* @returns A promise that resolves when flush and dispose are complete
*/
export async function flushAndDispose(client: Client | undefined, timeout = 2000): Promise<void> {
if (!client) {
await flush(timeout);

return;
try {
if (!client) {
await flush(timeout);
return;
}
await client.flush(timeout);
} catch (e) {
DEBUG_BUILD && debug.warn('Failed to flush client', e);
} finally {
try {
client?.dispose();
} catch (e) {
DEBUG_BUILD && debug.warn('Failed to dispose client', e);
}
}

await client.flush(timeout);
client.dispose();
}
30 changes: 30 additions & 0 deletions packages/cloudflare/test/flush.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,36 @@ describe('flushAndDispose', () => {
expect(flushSpy).toHaveBeenCalled();
flushSpy.mockRestore();
});

it('should not reject when client.flush rejects', async () => {
const mockClient = {
flush: vi.fn().mockRejectedValue(new Error('flush failed')),
dispose: vi.fn(),
} as unknown as Client;

await expect(flushAndDispose(mockClient)).resolves.toBeUndefined();
// dispose re-arms the client (recreates its transport) for the next isolate
// invocation, so it must still run even when flush failed.
expect(mockClient.dispose).toHaveBeenCalled();
});

it('should not reject when client.dispose throws', async () => {
const mockClient = {
flush: vi.fn().mockResolvedValue(true),
dispose: vi.fn().mockImplementation(() => {
throw new Error('dispose failed');
}),
} as unknown as Client;

await expect(flushAndDispose(mockClient)).resolves.toBeUndefined();
});

it('should not reject when the global flush rejects', async () => {
const flushSpy = vi.spyOn(sentryCore, 'flush').mockRejectedValue(new Error('flush failed'));

await expect(flushAndDispose(undefined)).resolves.toBeUndefined();
flushSpy.mockRestore();
});
});

describe('getOriginalWaitUntil', () => {
Expand Down
Loading