You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Disposing of an APIRequestContext in concurrent tasks can lead to errorΒ #37837
Run the below test script, most times it will throw an error on the first run but might have to try a couple times.
import { request, test } from '@playwright/test';
test.describe('APIRequestContext concurrency', async () => {
test('should be able to create and dispose APIRequestContext inside Promise.all', async () => {
await Promise.all(
Array.from({ length: 50 }).map(async (_, i) => {
let delay = Math.floor(Math.random() * 501);
await new Promise((res) => setTimeout(res, delay));
const apiContext = await request.newContext();
delay = Math.floor(Math.random() * 501);
await new Promise((res) => setTimeout(res, delay));
try {
await apiContext.dispose();
} catch (error) {
throw new Error('Error disposing apiContext: ' + error);
}
}),
);
});
});
Expected behavior
There should be no error when disposing of api request contexts in separate concurent tasks, given each one is also creating its own request context.
Actual behavior
An error can happen when disposing the api context
Error: Error disposing apiContext: Error: apiRequestContext._wrapApiCall: file data stream has unexpected number of bytes
Additional context
I believe this is a timing issue caused by disposing of an api context while another is being created concurrently in a different task. If there are no timeouts set, the script passes consistently which is why I think it's a timing issue. In this case I used timeouts of random length to simulate complex production code where each task in a Promise.all may not run at exactly the same speed, and set the script to 50 iterations to make reproducing the issue more likely.
I cannot reproduce it locally. Tried 1000 iterations and the test passes just fine. Can you run with PWDEBUGIMPL=1 and see if there is a more detailed stack trace?
Error: Error disposing apiContext: Error: apiRequestContext._wrapApiCall: file data stream has unexpected number of bytes
15 | await apiContext.dispose();
16 | } catch (error) {
> 17 | throw new Error('Error disposing apiContext: ' + error);
| ^
18 | }
19 | }),
20 | );
at /Users/eric.gerber/Documents/Repos/{production-repo}/playwright/tests/concurrency.spec.ts:17:17
at async Promise.all (index 36)
at /Users/eric.gerber/Documents/Repos/{production-repo}/playwright/tests/concurrency.spec.ts:5:5
at /Users/eric.gerber/Documents/Repos/{production-repo}/node_modules/playwright/lib/worker/workerMain.js:309:9
at /Users/eric.gerber/Documents/Repos/{production-repo}/node_modules/playwright/lib/worker/testInfo.js:326:11
at TimeoutManager.withRunnable (/Users/eric.gerber/Documents/Repos/{production-repo}/node_modules/playwright/lib/worker/timeoutManager.js:67:14)
at TestInfoImpl._runWithTimeout (/Users/eric.gerber/Documents/Repos/{production-repo}/node_modules/playwright/lib/worker/testInfo.js:324:7)
at /Users/eric.gerber/Documents/Repos/{production-repo}/node_modules/playwright/lib/worker/workerMain.js:307:7
at WorkerMain._runTest (/Users/eric.gerber/Documents/Repos/{production-repo}/node_modules/playwright/lib/worker/workerMain.js:282:5)
at WorkerMain.runTestGroup (/Users/eric.gerber/Documents/Repos/{production-repo}/node_modules/playwright/lib/worker/workerMain.js:194:11)
at process.<anonymous> (/Users/eric.gerber/Documents/Repos/{production-repo}/node_modules/playwright/lib/common/process.js:65:22)
Error: End of central directory record signature not found. Either not a zip file, or file is truncated.
at /Users/eric.gerber/Documents/Repos/{production-repo}/node_modules/playwright-core/lib/zipBundleImpl.js:1:24397
at /Users/eric.gerber/Documents/Repos/{production-repo}/node_modules/playwright-core/lib/zipBundleImpl.js:1:33604
at /Users/eric.gerber/Documents/Repos/{production-repo}/node_modules/playwright-core/lib/zipBundleImpl.js:1:17264
at FSReqCallback.wrapper [as oncomplete] (node:fs:671:5)
at FSReqCallback.callbackTrampoline (node:internal/async_hooks:130:17)
I have been doing some more testing around this, and the method in which you execute the test seems to matter. I created a small repo to isolate this issue https://github.com/egerber95/playwright-concurrency-bug
In both my production repo and this test repo there is a very high failure rate when executing the test from the UI mode, and no failure when executing from the terminal. In my production repo there is a decent failure rate when using the VSCode extension (how I ran the test when I got the above stack trace) to run the test, but it seems to pass 100% of the time using that method in my test repo. So not sure what could be different there.
I did have a coworker also run the test to make sure I didn't have some local environement issue, and they reported the same behavior. Very high error rate when the test is run from UI mode, no errors when run from terminal.
Activity
yury-s commentedon Oct 13, 2025
I cannot reproduce it locally. Tried 1000 iterations and the test passes just fine. Can you run with PWDEBUGIMPL=1 and see if there is a more detailed stack trace?
egerber95 commentedon Oct 15, 2025
Here is the full stack trace with PWDEBUGIMPL=1
I have been doing some more testing around this, and the method in which you execute the test seems to matter. I created a small repo to isolate this issue https://github.com/egerber95/playwright-concurrency-bug
In both my production repo and this test repo there is a very high failure rate when executing the test from the UI mode, and no failure when executing from the terminal. In my production repo there is a decent failure rate when using the VSCode extension (how I ran the test when I got the above stack trace) to run the test, but it seems to pass 100% of the time using that method in my test repo. So not sure what could be different there.
I did have a coworker also run the test to make sure I didn't have some local environement issue, and they reported the same behavior. Very high error rate when the test is run from UI mode, no errors when run from terminal.
yury-s commentedon Oct 16, 2025
Thanks, I can reproduce the error in --ui mode.
fix: do not zip live stack traces in ui mode
fix: do not zip live stack traces in ui mode