Skip to content

[Bug]: Disposing of an APIRequestContext in concurrent tasks can lead to errorΒ #37837

Closed

Description

Version

1.55.0

Steps to reproduce

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.

Environment

System:
    OS: macOS 15.7.1
    CPU: (14) arm64 Apple M3 Max
    Memory: 1.03 GB / 36.00 GB
  Binaries:
    Node: 22.9.0 - ~/.nvm/versions/node/v22.9.0/bin/node
    npm: 10.8.3 - ~/.nvm/versions/node/v22.9.0/bin/npm
  Languages:
    Bash: 3.2.57 - /bin/bash

Activity

  1. yury-s commented on Oct 13, 2025

    @yury-s
    Member

    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?

  2. egerber95 commented on Oct 15, 2025

    @egerber95
    Author

    Here is the full stack trace with PWDEBUGIMPL=1

    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.

    Image
  3. yury-s commented on Oct 16, 2025

    @yury-s
    Member

    Thanks, I can reproduce the error in --ui mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions