Handle incoming request cancellation in Workers with Request.signal
Cloudflare Workers now support listening to request cancellations using the Request.signal property with an abort event listener. This enables cleanup tasks and logging when clients disconnect, and requires enabling the enable_request_signal compatibility flag.
In Cloudflare Workers, you can now attach an event listener to Request objects, using the signal property ↗. This allows you to perform tasks when the request to your Worker is canceled by the client. To use this feature, you must set the enable_request_signal compatibility flag.
You can use a listener to perform cleanup tasks or write to logs before your Worker's invocation ends. For example, if you run the Worker below, and then abort the request from the client, a log will be written:
index.jsjsexport default {
async fetch(request, env, ctx) {
// This sets up an event listener that will be called if the client disconnects from your
// worker.
request.signal.addEventListener("abort", () => {
console.log("The request was aborted!");
});
const { readable, writable } = new IdentityTransformStream();
sendPing(writable);
return new Response(readable, {
headers: { "Content-Type": "text/plain" },
});
},
};
async function sendPing(writable) {
const writer = writable.getWriter();
const enc = new TextEncoder();
for (;;) {
// Send 'ping' every second to keep the connection alive
await writer.write(enc.encode("ping\r\n"));
await scheduler.wait(1000);
}
}index.tstsexport default {
async fetch(request, env, ctx): Promise<Response> {
// This sets up an event listener that will be called if the client disconnects from your
// worker.
request.signal.addEventListener('abort', () => {
console.log('The request was aborted!');
});
const { readable, writable } = new IdentityTransformStream();
sendPing(writable);
return new Response(readable, { headers: { 'Content-Type': 'text/plain' } });
},
} satisfies ExportedHandler<Env>;
async function sendPing(writable: WritableStream): Promise<void> {
const writer = writable.getWriter();
const enc = new TextEncoder();
for (;;) {
// Send 'ping' every second to keep the connection alive
await writer.write(enc.encode('ping\r\n'));
await scheduler.wait(1000);
}
}
For more information see the Request documentation.
Source: original entry ↗
More from Cloudflare
Follow Cloudflare to get its new changes in your feed and email digest.
Cloudflare One Client for macOS 2026.8.2100.0
GA release for macOS Cloudflare One Client with improved split tunnel handling that no longer briefly blocks traffic during reconnects, support for non-RFC 1918 local IPv4 networks, faster connects with lower memory use, and numerous reliability fixes across DNS, reauthentication, and client stability.
Cloudflare One Client for Windows 2026.8.2100.0
This GA release improves split tunnel reliability, adds support for non-RFC 1918 local networks, optimizes connection performance with faster reconnections and lower memory usage, and includes numerous bug fixes for DNS, registration, and network handling. The client now features a service recovery mechanism that automatically restarts on system unlock and better handles large hosts files without blocking traffic.
Cloudflare One Client for Linux 2026.8.2100.0
New GA release for Linux with improved split tunnel handling that no longer briefly blocks traffic during reconnects, support for non-RFC 1918 local IPv4 networks, faster tunnel reconnections, and lower memory usage. Includes numerous stability and reliability fixes for DNS, reconnection behavior, and crash issues.