-
-
Notifications
You must be signed in to change notification settings - Fork 151
Description
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:
- No request event listeners are attached to the interceptor
- 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
- Node.js 22.x
[email protected]@mswjs/[email protected](via MSW)@testcontainers/[email protected]- Ubuntu 22.04
- Docker running locally
Potentially related issues
- MSW seems not to work with kafka testcontainers msw#1600 - Previous testcontainers compatibility issue (although seems to be a different root cause)
nock@14breakstestcontainersnock/nock#2839 - Similar symptoms in nock, although not identical- fix: properly handle Unix socket paths in interceptors #722 - Previous Unix socket fix