effect icon indicating copy to clipboard operation
effect copied to clipboard

NodeHttpIncomingMessage.remoteAddress throws after client disconnect clears socket

Open 000alen opened this issue 3 months ago • 0 comments

What happened

With @effect/[email protected], an HTTP request fiber can read HttpServerRequest.remoteAddress after a Node client disconnect has cleared the request socket. NodeHttpIncomingMessage.remoteAddress then throws a defect instead of returning Option.none():

TypeError: Cannot read properties of null (reading 'remoteAddress')
    at NodeHttpIncomingMessage.js:58
    at HttpMiddleware.js:170

The late read can occur from middleware/finalization after an abort, even when application handling is otherwise interruption-safe.

Current code

get remoteAddress() {
  return this.remoteAddressOverride ?? Option.fromNullishOr(this.source.socket.remoteAddress)
}

Node types declare IncomingMessage.socket as non-null, but Node can clear it during disconnect cleanup before a late getter read.

Suggested change

get remoteAddress() {
  return this.remoteAddressOverride ?? Option.fromNullishOr(this.source.socket?.remoteAddress)
}

This would preserve the getter's Option<string> contract when the live socket is no longer present. Callers that require the accepted peer for the full request lifetime should still snapshot it at accept time and pass remoteAddressOverride.

Reproduction shape

  1. Start a NodeHttpServer.makeHandler request whose Effect remains alive briefly after the response/request closes.
  2. Send a partial request and destroy the client socket.
  3. After Node has cleared the incoming request's socket, read HttpServerRequest.remoteAddress from an uninterruptible finalizer or late middleware step.
  4. Observe the TypeError above rather than Option.none().

Environment: Node 22, [email protected], @effect/[email protected].

000alen avatar Aug 24 '26 16:08 000alen