Why it usually works: In a normal request, both the Real Socket and the Mock Socket are alive. The Mock Socket tries to "drive" (call readStart), but the Real Socket is already driving. Libuv handles are generally resilient to being told to "start reading" when they are already reading. It's messy, but not fatal.
sequenceDiagram
autonumber
participant Stream as Node Stream Logic
participant Mock as MockHttpSocket
participant Real as Real TLSSocket
participant Handle as Libuv C++ Handle
Note over Mock, Real: Setup: Mock._handle = Real._handle (Aliased)
Note over Real, Handle: Real Socket is alive and managing the handle
Real->>Handle: readStart() (Real drives the bus)
loop Data Flow
Handle-->>Real: Data incoming...
Real-->>Mock: push(data)
Stream->>Mock: _read(size) (Stream wants more?)
rect rgb(240, 255, 240)
Note right of Mock: The "Happy" Race
Mock->>Mock: Has _handle? YES.
Mock->>Handle: readStart()
Note left of Handle: Handle is ALREADY reading.<br/>Libuv ignores this redundancy.<br/>Status: OK.
end
end
Note over Stream, Handle: Connection ends gracefully for everyone.