Repository navigation
Description
A request that awaits a promise created by another request fails with "Cannot perform I/O on behalf of a different request" once the function that created the promise has been optimized by Maglev or TurboFan. The same code works until then. So a Worker can run correctly for a long time, then start returning 500s with no change to its code or its traffic.
Error: Cannot perform I/O on behalf of a different request. I/O objects (such as streams, request/response bodies, and others) created in the context of one request handler cannot be accessed from a different request's handler.
Mechanism
patches/v8/0006-Implement-Promise-Context-Tagging.patch gives each new promise the current request's context tag so that a cross-request then can be handed back to the right request. The optimizing compilers skip that step:
- The runtime and builtin paths tag the promise (
PromiseContextInit,Factory::NewJSPromiseWithoutHook). - TurboFan's
JSCreateLowering::ReduceJSCreatePromisestoresjsgraph()->ZeroConstant()inkContextTagOffset. - Maglev's promise allocation in
maglev-reducer-inl.hstoresGetSmiConstant(0)inkContextTagOffset. Runtime_PromiseContextCheckreturns the promise as it is when its tag isSmi::zero(), which is the same treatment as a promise with no owner.
So a promise allocated in optimized code during a request behaves like one created at module scope. Its continuations run in whichever request settles it. I did not read the tag at runtime; the source lines and the results below are the evidence.
Minimal repro
/warm optimizes derive. /load then creates a promise with it, and three other requests await that promise and do their own I/O.
// Usage: node repro.mjs <path to miniflare/dist/src/index.js>
const { Miniflare, convertV4MiniflareOptions } = await import(process.argv[2]);
const worker = (moduleScope) => `
let shared;
let open;
${moduleScope ? "shared = new Promise((resolve) => { open = resolve; });" : ""}
function derive(base) {
return base.then((value) => value);
}
export default {
async fetch(request, env) {
const { pathname } = new URL(request.url);
if (pathname === "/warm") {
let last;
for (let i = 0; i < 200000; i++) last = derive(Promise.resolve(i));
await last;
return new Response("warm");
}
if (pathname === "/ready") return new Response(String(shared !== undefined));
if (pathname === "/load") {
${moduleScope ? "" : "shared = derive(new Promise((resolve) => { open = resolve; }));"}
await scheduler.wait(600);
open(1);
await scheduler.wait(50);
return new Response("load ok");
}
const keepAlive = scheduler.wait(30000);
await shared;
const reply = await env.UPSTREAM.fetch("http://upstream/");
void keepAlive;
return new Response(await reply.text());
},
};
`;
async function run(name) {
const mf = new Miniflare(convertV4MiniflareOptions({
compatibilityDate: "2026-07-07",
modules: true,
script: worker(name === "module scope"),
serviceBindings: { UPSTREAM: () => new Response("own I/O ok") },
}));
try {
if (name === "warm") await (await mf.dispatchFetch("http://w/warm")).text();
const load = mf.dispatchFetch("http://w/load");
const deadline = Date.now() + 5000;
while ((await (await mf.dispatchFetch("http://w/ready")).text()) !== "true") {
if (Date.now() > deadline) throw new Error("/load did not create the shared promise within 5 s");
}
const waiters = ["/a", "/b", "/c"].map(async (path) => {
const response = await mf.dispatchFetch(`http://w${path}`);
return `${response.status} ${(await response.text()).split("\n")[0].slice(0, 70)}`;
});
await (await load).text();
console.log(`${name}: ${JSON.stringify(await Promise.all(waiters))}`);
} finally {
await mf.dispose();
}
}
await run("cold");
await run("warm");
await run("module scope");Each cell is the status all three waiting requests answered:
| workerd | V8 flags | cold | warm | module scope |
|---|---|---|---|---|
| 1.20260926.1 | none | 200 | 500 | 500 |
| 1.20260926.1 | --no-turbofan --no-maglev |
200 | 200 | 500 |
| 1.20261104.1 | none | 200 | 500 | 500 |
| 1.20261104.1 | --no-turbofan --no-maglev |
200 | 200 | 500 |
Set the flags with MINIFLARE_WORKERD_V8_FLAGS. The "module scope" column is the control: it fails with or without the optimizers, so the flags are not switching off the check itself.
.then().catch(), Promise.all().then().catch() and an async function in place of derive fail the same way.
Where this shows up
This came from cloudflare/vinext#4560, where ensureAppRouteModulesLoaded memoizes a route's Promise.all(...).then(...).catch(...) and concurrent first requests to that route await it.
- A trace shows that function compiled by Maglev, then failing on the next first load that has waiters.
- With only that function excluded (
--maglev-filter=-<name> --turbo-filter=-<name>), the same requests answered 200. - A function like this is hot on its fast path and rarely runs the path that creates the promise, so the failure can appear long after the code path was last exercised.
A waiting request can avoid it by wrapping the shared promise in one it creates itself, new Promise((resolve, reject) => { shared.then(resolve, reject); }), as long as that wrapper's code is not itself optimized.
Activity
fix: tag isolate-shared promises with the request that creates them (#…
fix(mcp): authorise after the upload; one JWKS fetch at a time per is…
T-1253 A coalesced directory read answers each caller in its own requ…
fix(web): 利用状態の取得中の Promise をリクエスト間で共有しない (#275)