@actions/http-client: a timed-out request is half-closed, never destroyed, so the process can stay alive after all work is done

Aperta Adatta ai principianti
#2,580 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
node.js, typescript
Ambito
backend

Direzione di ricerca

Leggi packages/http-client/src/index.ts, in particolare HttpClient.requestRawWithCallback e il gestore del timeout mostrato nell’issue. Riproduci il blocco con il repro.mjs fornito verso un peer che mantiene aperta la propria connessione, poi esamina i test esistenti del package sul comportamento del timeout. Il lavoro è completato quando una richiesta scaduta rilascia il proprio socket e il processo termina in entrambi gli scenari del peer.

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

Descrizione

Describe the bug

When a request hits its socket timeout, HttpClient.requestRawWithCallback half-closes the socket and rejects, but never destroys the request or the socket:

// packages/http-client/src/index.ts
req.setTimeout(this._socketTimeout || 3 * 60000, () => {
  if (socket) {
    socket.end()
  }
  handleResult(new Error(`Request timeout: ${info.options.path}`))
})

socket.end() sends a FIN and then waits for the peer to close its side. If the peer never does (a stalled upstream, a load balancer that keeps the connection), the socket stays open and referenced, so Node's event loop never becomes empty. The caller gets its rejection, can retry successfully and finish all its work, and the process still does not exit.

This is @actions/http-client 4.0.1 and unchanged on main (0c3a9c3).

To Reproduce

repro.mjs, next to an install of @actions/[email protected]:

// node repro.mjs [closes]
//
// The peer accepts the request and never answers. By default it also keeps its side of the connection open after the
// client's FIN; with "closes" it ends its side when the client does.
import net from 'node:net'
import {HttpClient} from '@actions/http-client'

const peerClosesItsSide = process.argv[2] === 'closes'

const server = net.createServer({allowHalfOpen: !peerClosesItsSide}, socket => {
  socket.unref() // the peer's own socket must not be what keeps this process alive
  socket.on('data', () => {})
})
server.unref()

server.listen(0, '127.0.0.1', async () => {
  const {port} = server.address()
  const started = Date.now()
  process.on('exit', () => console.log(`process exited ${Date.now() - started} ms after start`))

  const client = new HttpClient('repro', [], {socketTimeout: 500, allowRetries: false})
  try {
    await client.get(`http://127.0.0.1:${port}/`)
  } catch (error) {
    console.log(`request rejected after ${Date.now() - started} ms: ${error.message}`)
  }
  console.log('all work is done; the process should exit now')
})
  1. npm install @actions/[email protected]

  2. node repro.mjs: the request is rejected after the timeout, the last line is printed, and the process never exits.

    request rejected after 504 ms: Request timeout: /
    all work is done; the process should exit now
    
  3. node repro.mjs closes: the same, but the peer closes its side, and the process exits at once.

    request rejected after 503 ms: Request timeout: /
    all work is done; the process should exit now
    process exited 504 ms after start
    

Expected behavior

After a timeout the request's socket is released, so a process whose work is done exits whatever the peer does.

Replacing socket.end() with socket.destroy() in that handler (or calling req.destroy()) does it. With that one-line change applied to lib/index.js of 4.0.1, node repro.mjs prints process exited 505 ms after start in both cases.

Additional context

How we met it: on self-hosted Linux x64 runners, a step running actions/download-artifact@v8 (9000827, which bundles @actions/http-client 4.0.1) had one request to the artifact service time out, retried, downloaded the artifact, logged its success, and then did not end until the job's time limit cancelled it 52 minutes later. Two jobs of one workflow run did this in the same minutes:

22:45:10.69Z Downloading single artifact
22:51:10.88Z Attempt 1 of 5 failed with error: Request timeout: /twirp/github.actions.results.api.v1.ArtifactService/ListArtifacts. Retrying request in 8000 ms...
22:51:19.24Z Preparing to download the following artifacts:
22:51:21.28Z Artifact download completed successfully.
22:51:21.28Z Total of 1 artifact(s) downloaded
22:51:21.28Z Download artifact has finished successfully
23:43:44.92Z ##[error]The operation was canceled.

We could not inspect the hung processes, so the half-closed socket is the explanation that fits the code and the reproduction above, not something we observed on the runners. download-artifact's run() returns after its success line and nothing calls process.exit, so any handle left open keeps the step alive; when the action is used inside a composite action, the step cannot carry timeout-minutes either.

Related: #2575 describes another socket left open in @actions/artifact, whose wait ends when this same timeout fires after three minutes. With the behaviour described here the timeout itself does not release the socket, so that bound only holds when the peer closes its side.

The reproduction was run with Node v26.11.0 on macOS.

Lingua principale
TypeScript
Stelle
5.9k
Fork
1.8k
Merge medio
15m
PR unite (30g)
1

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 actions/toolkit

Tutte le issue di actions/toolkit

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.