Occasional "Unhandled 'error' event" exception no matter what I do

I get this on my simple node.js build. I failed to find any pattern other than it seems to pass when I delete yarn.lock and yarn install again. But sometimes it just passes. This is infuriating, to be honest. And naturally, yarn.lock doesn’t change every time. Pinned node version, overrode packages install command to clean cache first – up to no avail. No idea what could possibly be the difference between a build that fails and the one that passes. This can happen without any code changes!

The local build passes of course and the CI one passes too sometimes. I know the problem is notorious, but nothing I found helped me so far.

-  Building for production...
Starting type checking service...
Using 1 worker with 2048MB memory limit
events.js:292
      throw er; // Unhandled 'error' event
      ^

Error: This socket has been ended by the other party
    at Socket.writeAfterFIN [as write] (net.js:454:14)
    at PoolWorker.writeEnd (/home/circleci/project/node_modules/thread-loader/dist/WorkerPool.js:129:20)
    at PoolWorker.dispose (/home/circleci/project/node_modules/thread-loader/dist/WorkerPool.js:273:12)
    at WorkerPool.disposeWorkers (/home/circleci/project/node_modules/thread-loader/dist/WorkerPool.js:370:16)
    at WorkerPool.terminate (/home/circleci/project/node_modules/thread-loader/dist/WorkerPool.js:305:10)
    at WorkerPool.disposeWorkers (/home/circleci/project/node_modules/thread-loader/dist/WorkerPool.js:364:12)
    at Timeout._onTimeout (/home/circleci/project/node_modules/thread-loader/dist/WorkerPool.js:358:44)
    at listOnTimeout (internal/timers.js:549:17)
    at processTimers (internal/timers.js:492:7)
Emitted 'error' event on Socket instance at:
    at emitErrorNT (net.js:1340:8)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  code: 'EPIPE'
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Exited with code exit status 1

The build file is straightforward:

Orb:

orbs:
  node: circleci/node@4.1.0

Setup job:

  node:
    docker:
      - image: "cimg/base:stable"
    steps:
      - node/install:
          install-yarn: true
          node-version: v15.1.0

Not sure whether cleaning the cache has any effect:

  build:
    executor:
      name: node/default
    steps:
      - checkout
      - node/install-packages:
          pkg-manager: yarn
          override-ci-command: yarn cache clean --force && yarn install
      - run: yarn build
      - run: yarn build-pack

Also, in the workflow the build job depends on the setup job:

      - build:
          requires:
            - node

No changes whatsoever, just a “rerun from start”. And it doesn’t work this way every time. Sometimes it still fails. And sometimes it builds on the first go. A race condition somewhere?

Have you considered potential memory issues?

Although, based on the specific text of that message, I’m guessing this is TypeScript related: