Skip to content

Intermittent EPIPE errors with high-concurrency Unix socket requests #760

@georgewaters

Description

@georgewaters

Description

I'm experiencing intermittent EPIPE errors when using MSW with testcontainers. The issue occurs when the interceptor is applied but no request handlers are attached (i.e., relying on passthrough behavior). This issue was first observed when writing tests using both @testcontainers/kafka and MSW.

The issue does not appear to happen when using only @testcontainers/kafka with native networking

Investigation

Through some investigation and trial-and-error, I've identified that the issue seems to happen specifically when:

  1. No request event listeners are attached to the interceptor
  2. High-concurrency requests are made to Unix sockets

When a listener is attached that calls controller.passthrough() synchronously, the issue does not occur.

My hypothesis is that when no listeners are attached, handleRequest() goes through the full async machinery (emitAsync()Promise.race()until()) before calling passthrough(). These async operations create microtask checkpoints that allow more requests to pile up, and when they all passthrough simultaneously, socket contention causes EPIPE errors.

Reproduction

I've created a minimal reproduction repository: https://github.com/georgewaters/testcontainers-kafka-msw-repro

The reproduction starts an MSW server with no handlers and server.listen({ onUnhandledRequest: 'bypass' }); and attempts to start a Kafka container. This makes many concurrent requests to Docker's Unix socket, which appear to intermittently fail with EPIPE errors.

Proposed fix

I've prepared a PR that adds an early return in handleRequest() when no listeners are attached which I'll submit separately

This bypasses the async machinery when it's not needed, which appears to resolve the timing issue in my testing.

I understand this is an intermittent issue that's difficult to reproduce reliably in automated tests, but I wanted to document my findings and proposed solution. Happy to discuss alternative approaches if this doesn't align.

Environment

Potentially related issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions