Execution Context Canceled When Returning Response Object with Stubs from Service Worker

When calling a method on a Service Worker that returns a Response object containing stubs (remote objects), and then returning that Response object from the caller Worker, the execution context of the called Service Worker is canceled prematurely, even after attempting to dispose the Response object using the using declaration or explicit disposal. ServiceA Worker (wrangler.toml)
name = "service-a"
main = "src/index.ts"
logpush = true

[env.dev]
workers_dev = true
services = [{ binding = "SERVICE_B", service = "service-b" }]
name = "service-a"
main = "src/index.ts"
logpush = true

[env.dev]
workers_dev = true
services = [{ binding = "SERVICE_B", service = "service-b" }]
ServiceA Worker (src/index.ts)
export default {
async queue(batch, env) {

// Approach 1: Using 'using' declaration
using response = await env.SERVICE_B.fetchData();

// Approach 2: Explicit disposal
// response[Symbol.dispose]();
console.log('response', response)
}
}
export default {
async queue(batch, env) {

// Approach 1: Using 'using' declaration
using response = await env.SERVICE_B.fetchData();

// Approach 2: Explicit disposal
// response[Symbol.dispose]();
console.log('response', response)
}
}
ServiceB Worker (wrangler.toml)
name = "service-b"
main = "src/index.ts"
logpush = true
name = "service-b"
main = "src/index.ts"
logpush = true
ServiceB Worker (src/index.ts)
export default class extends WorkerEntrypoint<Env> {
async fetchData() {
const response = new Response('Data from Service B');
return response;
}
}
export default class extends WorkerEntrypoint<Env> {
async fetchData() {
const response = new Response('Data from Service B');
return response;
}
}
Expected Behavior: The Response object returned from fetchData in ServiceB should be properly disposed, and the execution context of ServiceB should remain active until all stubs have been disposed. Actual Behavior: The execution context of ServiceB is being canceled prematurely, even after attempting to dispose the Response object using the using declaration or explicit disposal in ServiceA. But in my case, the fetch call that happened in serviceB works fine, but in the logs of ServiceA and ServiceB it's showing as the event is being canceled, might be an issue with logpush, not sure.
No description
1 Reply
mr.d2315
mr.d23154mo ago
we should fully consume the Response before logging or utilizing it further.
using response = await env.SERVICE_B.fetchData();
console.log('response:', await response.text());
using response = await env.SERVICE_B.fetchData();
console.log('response:', await response.text());
so by awaiting the text() method, we ensure that the entire Response stream is consumed before attempting to log it. This prevents the stream from being closed prematurely due to the async nature of the queue processing.
Want results from more Discord servers?
Add your server