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
5 changes: 5 additions & 0 deletions .changeset/catch-constructor-interception-rejection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cloudflare/containers': patch
---

Log outbound interception failures that happen while a running container is re-attached in the constructor, instead of leaving an unhandled promise rejection.
6 changes: 6 additions & 0 deletions src/lib/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,13 @@ export class Container<Env = Cloudflare.Env> extends DurableObject<Env> {
}

if (this.container.running) {
// Nothing awaits this promise, so observe a failure here to keep it from
// surfacing as an unhandledRejection. refreshOutboundInterception() re-applies
// and awaits its own copy, so callers on that path still see the error.
this.applyOutboundInterceptionPromise = this.applyOutboundInterception();
this.applyOutboundInterceptionPromise.catch(e => {
console.error(`Error applying outbound interception for container ${this.ctx.id}:`, e);
});
}
});

Expand Down
21 changes: 21 additions & 0 deletions src/tests/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,27 @@ describe('HTTPS interception runtime guard', () => {
});
});

describe('outbound interception on construction', () => {
test('logs a failure instead of leaving an unhandled rejection', async ({ mockCtx }) => {
class InterceptingContainer extends Container {}
InterceptingContainer.outbound = () => new Response('ok');
mockCtx.container.running = true;
mockCtx.container.interceptAllOutboundHttp.mockRejectedValue(new Error('intercept failed'));
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});

const container = new InterceptingContainer(mockCtx as never, {});
await mockCtx.blockConcurrencyWhile.mock.results[0].value;
await new Promise(resolve => setTimeout(resolve, 0));

expect(errorSpy).toHaveBeenCalledWith(
expect.stringContaining('outbound interception'),
expect.any(Error)
);
await expect(container.applyOutboundInterceptionPromise).rejects.toThrow('intercept failed');
errorSpy.mockRestore();
});
});

describe('getRandom', () => {
baseTest('should return a container stub', async () => {
const mockBinding = {
Expand Down