[BUG] `timeouts.idle` is cleared after completed keep-alive responses

Aperta
#169 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Tranquilla
Stack tecnologico
javascript, node.js
Ambito
api, backend

Direzione di ricerca

Start with normalizeOptions(), Agent, and connect(), then trace how completed responses return sockets to Node's free socket pool. Verify the keep-alive path preserves the configured timeouts.idle value without replacing the existing connect() timeout behavior. Done means a completed keep-alive socket expires after the configured idle timeout, as described by the reproduction shape; PR #170 is referenced in the issue.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Is there an existing issue for this?
  • I have searched the existing issues
Current Behavior

Summary

@npmcli/agent applies timeouts.idle while a socket is created, but completed keep-alive sockets can lose that timeout when Node moves them into the free socket pool. As a result, timeouts.idle protects in-flight idle requests, but it does not reliably expire pooled sockets after a response has completed.

Impact

Consumers that configure timeouts.idle expecting it to bound the lifetime of idle keep-alive sockets can keep completed sockets open indefinitely, or until the remote peer/server closes them. This is especially visible when a caller sets timeouts.idle below an upstream keep-alive timeout to avoid reusing sockets that the upstream might close first.

Current behavior

  1. normalizeOptions() converts timeout into the custom timeouts.idle value and removes the native timeout option.
  2. Agent stores the custom timeout separately and passes the remaining normalized options to the underlying Node/agent-base agent.
  3. connect() applies timeouts.idle directly to the socket when it is created.
  4. After the response completes, Node keeps the socket alive and reapplies agent.options.timeout for the free socket pool.
  5. Since agent.options.timeout is absent, Node's free-socket timeout becomes 0, clearing the idle timeout.

Expected behavior

If timeouts.idle is configured, completed keep-alive sockets should continue to expire after that idle timeout while they are in the free socket pool.

Reproduction shape

  1. Create an agent with timeouts: { idle: 500 } and maxSockets: 1.
  2. Make a request to an HTTP server that responds successfully and keeps the connection alive.
  3. Consume the response body so the socket returns to the free socket pool.
  4. Wait longer than timeouts.idle while the server's keep-alive timeout remains longer than the agent timeout.

Before the fix, the free socket remains open because Node resets its timeout to 0. After the fix, the free socket is destroyed after the configured idle timeout.

Proposed fix

Override the agent's free-socket keep-alive handling and reapply timeouts.idle after Node decides to keep a socket alive. This preserves the existing custom timeout behavior for active sockets and avoids changing native agent.options.timeout for active proxied requests.

Notes

This should not replace the custom timeouts.idle handling in connect(): that path still provides the package-specific IdleTimeoutError behavior for sockets that go idle before a response completes. The additional keep-alive handling is needed specifically for completed response sockets managed by Node's free socket pool.

See PR: #170

Lingua principale
JavaScript
Stelle
9
Fork
15
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di npm/agent

Tutte le issue di npm/agent

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.