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

未关闭 适合新手
#2,580 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
领域
backend

调研方向

阅读 packages/http-client/src/index.ts,尤其是 HttpClient.requestRawWithCallback 和 issue 中展示的超时处理程序。使用提供的 repro.mjs,针对保持自身连接打开的 peer 重现挂起,然后检查 package 中现有的超时行为测试。当超时的请求释放其 socket,且进程在两种 peer 场景下都能退出时,即为完成。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
TypeScript
星标
5.9k
派生
1.8k
平均合并
15 分钟
30 天内合并 PR
1

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

actions/toolkit 的其他 Issue

查看 actions/toolkit 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。