Skip to content

HTTP

Worker A that declares a Service binding to Worker B can forward a Request object to Worker B, by calling the fetch() method that is exposed on the binding object.

For example, consider the following Worker that implements a fetch() handler:

{
	"$schema": "./node_modules/wrangler/config-schema.json",
	"name": "worker_b",
	"main": "./src/workerB.js",
	"upload_source_maps": true
}
"$schema" = "./node_modules/wrangler/config-schema.json"
name = "worker_b"
main = "./src/workerB.js"
upload_source_maps = true
export default {
  async fetch(request, env, ctx) {
    return new Response("Hello World!");
  }
}

The following Worker declares a binding to the Worker above:

{
	"$schema": "./node_modules/wrangler/config-schema.json",
	"name": "worker_a",
	"main": "./src/workerA.js",
	"upload_source_maps": true,
	"services": [
		{
			"binding": "WORKER_B",
			"service": "worker_b"
		}
	]
}
"$schema" = "./node_modules/wrangler/config-schema.json"
name = "worker_a"
main = "./src/workerA.js"
upload_source_maps = true

[[services]]
binding = "WORKER_B"
service = "worker_b"

And then can forward a request to it:

export default {
	async fetch(request, env) {
		return await env.WORKER_B.fetch(request);
	},
};

Handle canceled requests

Cancellation can pass from a client through one Worker to another. This applies to both HTTP and RPC service bindings. Refer to Request cancellation for an explanation.

Both Workers can report Client Disconnected for the same request. This can also happen if the calling Worker aborts the call or cancels the response body. The status alone does not tell you which Worker canceled first or why.

Read or forward the response body

await env.WORKER_B.fetch(request) waits for response headers. The body may still be arriving when it returns.

Handle the body based on what your code needs:

  • Send the response to your client: Return the Response, as in the forwarding example.
  • Read the response in your Worker: Await the body reader, such as await response.json() for a JSON response.
  • Discard the response body: Run await response.body?.cancel() to release resources.

A recorded HTTP 200 does not mean the whole body arrived. Check whether the body finished as well as the HTTP status.

Choose when work should stop

Follow Choose when work should stop for guidance on request work, short background tasks, and jobs that must finish.

Stop work when the client disconnects

To cancel an operation when the client disconnects, pass request.signal to an API that accepts an AbortSignal. Check the enable_request_signal compatibility setting first. If you forward the incoming Request, also check request_signal_passthrough.

For work that should continue after a disconnect, do not use that disconnect's abort signal. waitUntil() will not restart work canceled by a signal. Even without an abort signal, the runtime can still stop work when the request ends.

Find out why a request was canceled

Check both Workers for the same request:

  1. Log the same request ID in both Workers. If you create your own ID, pass it in a request header when calling the other Worker. Also log when the call starts, when response headers arrive, and when the body finishes or fails.
  2. Start real-time logs for both Workers before testing. For a Pages caller, open Pages Functions logs too. Note which deployments or versions you are testing.
  3. Send a request and let it finish. Then cancel a second request before it finishes, for example by closing the browser tab. Save the JSON logs from both tests.
  4. Match the requests by ID and UTC time. Compare outcome, HTTP status, and exceptions. Check your code for timeouts and AbortController calls. Check whether slow work in the called Worker makes the client give up.

Matching request IDs or cancellation counts do not tell you which Worker canceled first. If a log entry is missing, check logging settings, filters, sampling, and retention before assuming the request never arrived.

Was this helpful?