Skip to content

Instantly share code, notes, and snippets.

@maxsei
Created July 9, 2026 17:19
Show Gist options
  • Select an option

  • Save maxsei/d21f93d2e1dd973fa2f431405b64e8e7 to your computer and use it in GitHub Desktop.

Select an option

Save maxsei/d21f93d2e1dd973fa2f431405b64e8e7 to your computer and use it in GitHub Desktop.
cloudflare worker cancellation and request completion

cf-disconnects

The idea of this experiment is to show that even if a client disonnects from a coudflare worker, the request will run to completion. To test that this is the case you can spam the sleep button in the UI which will cause the current in progress request to be cancelled by datastar. However, if you check the logs, you will see that sleeping will start and finish successfully.

{ reqId: 'dfacc03c-93e1-41eb-849b-999894e0628d', sleeping: 'start' }
{ reqId: 'dfacc03c-93e1-41eb-849b-999894e0628d', sleeping: 'end' }
{ reqId: 'ffecc601-3a04-4799-9812-58d30362c10c', sleeping: 'start' }
{ reqId: 'ffecc601-3a04-4799-9812-58d30362c10c', sleeping: 'end' }
{ reqId: '29183137-cd48-400c-b3f5-431e6bbf4df2', sleeping: 'start' }
{ reqId: '6e79972b-cc5d-49cc-b0fe-f471f76b3eaa', sleeping: 'start' }
{ reqId: '29183137-cd48-400c-b3f5-431e6bbf4df2', sleeping: 'end' }
{ reqId: '6e79972b-cc5d-49cc-b0fe-f471f76b3eaa', sleeping: 'end' }
import { Hono } from "hono";
import type { FC } from "hono/jsx";
const app = new Hono();
app.get("/", async (c) => {
return c.html(
<Layout>
<div class="flex flex-column">
<div>
<button data-on:click="@post('/sleep')" data-indicator="sleeping">
Sleep For
</button>
<input type="number" data-bind="sleepFor" value="5000" step="1" />
</div>
<div class="dn" data-class:dib="$sleeping">
Sleeping...
</div>
<SleptFor />
</div>
</Layout>,
);
});
app.post("/sleep", async (c) => {
const signals = await c.req.json();
const sleepFor = parseInt(signals.sleepFor);
const reqId = crypto.randomUUID();
console.info({ reqId, sleeping: "start" });
await new Promise<void>(async (accept) => {
setTimeout(() => accept(), sleepFor);
});
console.info({ reqId, sleeping: "end" });
return c.html(<SleptFor value={sleepFor} />);
});
const SleptFor: FC<{ value?: number }> = ({ value }) => (
<span id="sleptFor" data-show="!$sleeping">
{value ? `Slept for ${value}ms` : ""}
</span>
);
const Layout: FC<{ children: any }> = ({ children }) => {
return (
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>App</title>
<link
rel="stylesheet"
href="https://unpkg.com/tachyons@4.12.0/css/tachyons.min.css"
/>
<script
type="module"
src="https://cdn.jsdelivr.net/gh/starfederation/datastar@v1.0.2/bundles/datastar.js"
></script>
</head>
<body>{children}</body>
</html>
);
};
export default app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment