Cannot fetch service binding as it gets "canceled"
# workers-help
d
My worker wrangler TOML file has following settings:
Copy code
[[env.production.services]]
binding = "workerA"
service = "worker-a"

[[env.production.services]]
binding = "workerB"
service = "worker-b"
Both services are called like that:
Copy code
const workerAResponse = await env.workerA.fetch(
  new Request(new URL(`worker-a/${id}`, env.WORKER_A_DOMAIN)),
  {
    headers: request.clone().headers,
  }
);


if (!workerAResponse.ok) { return workerAResponse }

const { public_id } = await workerAResponse.json();

const workerBResponse = await env.workerB.fetch(
  new Request(new URL(`worker-b/${public_id}`, env.WORKER_B_DOMAIN)),
  {
    headers: request.clone().headers,
  }
);

if (!workerBResponse.ok) { return workerBResponse; }
Request to
worker-a
is successful, however a request to` worker-b` is canceled, as
wrangler tail
says:
GET <some-url> - Canceled @ 4/14/2023, 5:03:23 PM
What can possibly go wrong about it? And why worker-b response is canceled whereas worker-a (both are set up literally the same) works just fine. By the way, worker responds with
200
, but due to the
outcome: "canceled"
it is not taken into account ;/
k
Are you reading the response of Worker B?
In that example, you're not
Oh, nevermind, you're returning it
That shouldn't be an issue really - unless there's code after that where you can reach but don't read it
d
in case it fails (not "ok"), I return it. However if it returns 200 I do nothing with the response
k
You'll want to read the response if it's okay
You also don't need to clone the request to read the headers - headers aren't consumed by using them, only the request body is.
d
wow, didnt know I should always read what my worker returns me
as for the clone statement -- gotcha! thank you!
k
It's a quirk specific to Service Bindings and I don't know why - but it'll sit there waiting for you to consume the body and when you don't, that 'response' is cancelled as it wasn't consumed fully.
d
by the way, should i specify in workerB wrangler.toml that some other worker will do the fetch to this worker?
k
Not necessary, but if you mean that you don't want anything other than your Worker to be able to fetch it then set
workers_dev = false
and remove any `route`/`routes` keys so that it's only possible to reach it over Service Bindings
2 Views