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/fix-runtime-rollout-onstop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cloudflare/containers': patch
---

Fix `onStop` lifecycle handling for runtime-signalled container rollouts.
Binary file added docs/issue-253-proof.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions docs/issue-253-proof.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 4 additions & 8 deletions src/lib/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import { DurableObject, WorkerEntrypoint } from 'cloudflare:workers';
const NO_CONTAINER_INSTANCE_ERROR =
'there is no container instance that can be provided to this durable object';
const RATE_LIMITED_ERROR = 'you are requesting too many containers per second';
const RUNTIME_SIGNALLED_ERROR = 'runtime signalled the container to exit:';
const RUNTIME_SIGNALLED_ERROR = 'runtime signalled the container to exit';
const RUNTIME_SIGNALLED_EXIT_CODE = /runtime signalled the container to exit.*:\s*(-?\d+)\s*$/i;
const UNEXPECTED_EXIT_ERROR = 'container exited with unexpected exit code:';
const NOT_LISTENING_ERROR = 'the container is not listening';
const CONTAINER_STATE_KEY = '__CF_CONTAINER_STATE';
Expand Down Expand Up @@ -156,13 +157,8 @@ function getExitCodeFromError(error: unknown): number | null {
}

if (isRuntimeSignalledError(error)) {
return +error.message
.toLowerCase()
.slice(
error.message.toLowerCase().indexOf(RUNTIME_SIGNALLED_ERROR) +
RUNTIME_SIGNALLED_ERROR.length +
1
);
const match = error.message.match(RUNTIME_SIGNALLED_EXIT_CODE);
return match ? Number(match[1]) : null;
}

if (isContainerExitNonZeroError(error)) {
Expand Down
92 changes: 92 additions & 0 deletions src/tests/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,98 @@ describe('Container', () => {
});
});

test('monitor should persist the exit code for the existing runtime exit message', async ({
mockCtx,
container,
}) => {
let rejectMonitor: (error: Error) => void = () => undefined;
using onErrorSpy = vi.spyOn(container, 'onError').mockResolvedValue(undefined);
mockCtx.container.monitor.mockReturnValue(
new Promise((_resolve, reject) => {
rejectMonitor = reject;
})
);

await container.start(undefined, { portToCheck: 8080, retries: 1, waitInterval: 1 });
mockCtx.container.running = false;
rejectMonitor(new Error('Runtime signalled the container to exit: 0'));

await vi.waitFor(() => {
expect(mockCtx.storage.put).toHaveBeenCalledWith(
'__CF_CONTAINER_STATE',
expect.objectContaining({ status: 'stopped_with_code', exitCode: 0 })
);
});
expect(onErrorSpy).not.toHaveBeenCalled();
});

test('monitor should not classify unrelated runtime errors as signalled exits', async ({
mockCtx,
container,
}) => {
let rejectMonitor: (error: Error) => void = () => undefined;
using onErrorSpy = vi.spyOn(container, 'onError').mockResolvedValue(undefined);
mockCtx.container.monitor.mockReturnValue(
new Promise((_resolve, reject) => {
rejectMonitor = reject;
})
);

await container.start(undefined, { portToCheck: 8080, retries: 1, waitInterval: 1 });
mockCtx.container.running = false;
rejectMonitor(new Error('container supervisor failed'));

await vi.waitFor(() => {
expect(mockCtx.storage.put).toHaveBeenCalledWith(
'__CF_CONTAINER_STATE',
expect.objectContaining({ status: 'stopped' })
);
});
expect(onErrorSpy).toHaveBeenCalledWith(
expect.objectContaining({ message: 'container supervisor failed' })
);
expect(mockCtx.storage.put).not.toHaveBeenCalledWith(
'__CF_CONTAINER_STATE',
expect.objectContaining({ status: 'stopped_with_code' })
);
});

test('rollout exit should replay onStop exactly once during recovery', async ({
mockCtx,
container,
}) => {
let rejectMonitor: (error: Error) => void = () => undefined;
using onStopSpy = vi.spyOn(container, 'onStop');
mockCtx.container.monitor.mockReturnValue(
new Promise((_resolve, reject) => {
rejectMonitor = reject;
})
);

await container.start(undefined, { portToCheck: 8080, retries: 1, waitInterval: 1 });
mockCtx.container.running = false;
rejectMonitor(
new Error('Runtime signalled the container to exit due to a new version rollout: 0')
);

await vi.waitFor(() => {
expect(mockCtx.storage.put).toHaveBeenCalledWith(
'__CF_CONTAINER_STATE',
expect.objectContaining({ status: 'stopped_with_code', exitCode: 0 })
);
});

await (
container as unknown as { syncPendingStoppedEvents(): Promise<void> }
).syncPendingStoppedEvents();
await (
container as unknown as { syncPendingStoppedEvents(): Promise<void> }
).syncPendingStoppedEvents();

expect(onStopSpy).toHaveBeenCalledTimes(1);
expect(onStopSpy).toHaveBeenCalledWith({ exitCode: 0, reason: 'exit' });
});

test('replaced monitor should not stop a newer container instance', async ({
mockCtx,
container,
Expand Down