Skip to content

[Bug?]: Race condition when modifying workspace directory and calling yarn pack in parallel causes yarn to crashΒ #7222

Description

@GeorgeTaveras1231

Self-service

  • I'd be willing to implement a fix

Describe the bug

Scenario

Two parallel jobs are running.
Job 1 - Creates temporary files that are deleted after the process ends
Job 2 - Runs yarn pack or calls genPackList from @yarnpkg/plugin-pack

Explanation

genPackList calls walk - a function that traverses the folder structure to determine which files are part of the pack. In this process, walk calls cwdFs.lstatPromise(here) to determine wether the path belongs to a directory or a normal file. If its a directory, it calls cwdFs.readdirPromise(here) to get a list of all paths, which are added to the DFS traversal stack; each discovered path goes through the same process.

The race condition manifests within a traversal loop, after readdir. In the scenario where Job 1 created a file, in a directory being processed by the walk function, the file will be added to the DFS traversal stack. However, by the time that this path is popped, before lstat is called, it may have already been deleted by Job 1

Result

This produces a failure with the following error

> yarn pack
...
Error: ENOENT: no such file or directory, lstat '$PATH_TO_TEMPORARY_FILE'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'lstat',
  path: '$PATH_TO_TEMPORARY_FILE'
}

To reproduce

  1. Create a workspace
  2. Create a script that adds files -- wait 1 second -- then deletes the file
  3. Create another script that calls the script on step 2, as well as yarn pack, executing them in parallel
  4. Call the script created in step 3

The results may be mixed and may require tweaking the 1 second variable. When reproduced correctly, you should see the lstat error mentioned above.

Environment

System:
    OS: macOS 26.4.1
    CPU: (12) arm64 Apple M3 Pro
  Binaries:
    Node: 22.20.0 - /Users/georgetaveras/.asdf/installs/nodejs/22.20.0/bin/node
    Yarn: 4.11.0 - /opt/homebrew/bin/yarn
    npm: 10.9.3 - /Users/georgetaveras/.asdf/plugins/nodejs/shims/npm

Additional context

Proposal

Catch errors from lstat, ignore if the file no longer exists.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions