Skip to content

feat(k8s): retry cleanup API calls with a per-request timeout - #71

Merged
Goalina merged 4 commits into
opensourceways:mainfrom
wyhhai:feat/cleanup-retry-timeout
Nov 19, 2026
Merged

Goalina merged 4 commits into
opensourceways:mainfrom
wyhhai:feat/cleanup-retry-timeout

Conversation

@wyhhai

@wyhhai wyhhai commented Nov 11, 2026 •

Copy link
Copy Markdown

Background

Cleanup (Stop containers → cleanupJob() → prunePods() / pruneSecrets()) used to issue one untimed, un-retried request per resource. When the node-local ClusterIP → managed apiserver path has a transient blip, kube-proxy/conntrack can pin the connection to a dead backend: the SYN is blackholed and the process hangs until the kernel exhausts SYN retries (~127s), then fails with connect ETIMEDOUT.

This is not theoretical:

  • Job vllm-project/vllm-ascend/actions/runs/42573365617/job/129015567064 — all seven business steps succeeded, only the post-job Stop containers failed after 129.9s with FetchError ... https://<apiserver>/api/v1/namespaces/<namespace>/pods ... connect ETIMEDOUT.
  • Two more jobs on the same day hit the same signature at listNamespacedSecret and deleteNamespacedPod (131s / 133s).
  • The auth preflight (isAuthPermissionsOK, which already retries since feat(k8s): retry transient k8s API failures in auth preflight; bump runner to 2.338.0 #64) passed right before the hanging call, so this is not RBAC and not a fully-down API server.

The important detail is that retrying alone is not enough: a conntrack entry keeps the DNAT mapping for the life of the connection, so a retry only helps if the previous attempt is abandoned and a new connection is opened. That requires a per-request timeout.

Changes

  • prunePods() / pruneSecrets() now run every list and delete call through retryK8sApiCall. Cleanup uses a larger budget than the default: 6 attempts, 15s per-request timeout, 3/6/12/15/15s backoff (~141s worst case), sized to ride through the ~127s kernel SYN-retry window.
  • Create paths are covered too. createJobPod() / createContainerStepPod() / createDockerSecret() / createSecretForEnvs() use the same policy. They run on the same ClusterIP path, and 3 of the 10 ETIMEDOUT events in the linked analysis happened in createJobPod during Prepare — before this change those were uncovered.
  • Creates are idempotent. A create that timed out client-side may already have been applied, so the retry legitimately observes 409 AlreadyExists. That is treated as success by reading the object back (readNamespacedPod / readNamespacedSecret), instead of failing the job.
  • A timeout abandons the stuck request rather than aborting the socket; the retry opens a fresh connection, which is what bypasses the conntrack entry pinned to a dead backend.
  • Deletes treat 404 as success. A request that timed out client-side may already have been applied server-side, so the retry legitimately observes NotFound. Failing the job on that 404 would recreate the exact symptom this PR removes.
  • Retry warnings now include the error code (code=ETIMEDOUT) instead of logging [object Object] for plain-object errors.
  • All touched functions accept an optional CoreV1Api client and retry options so the logic is unit-testable without a cluster. Existing call sites (prepare-job, cleanup-job) are unchanged.
  • deleteNamespacedPod keeps gracePeriodSeconds: 0, matching the previous deletePod() behavior; deleteNamespacedSecret keeps the previous parameters.
  • deleteSecret() was left behind by the inlining above and had no remaining callers, so it is removed.

Tests

npm install --prefix packages/hooklib && npm run build --prefix packages/hooklib
npm install --prefix packages/k8s
npx tsc --noEmit -p packages/k8s
npx jest --config packages/k8s/jest.config.js tests/prune-retry-test.ts tests/retry-utils-test.ts --runInBand
npx eslint packages/k8s/src/k8s/index.ts packages/k8s/tests/prune-retry-test.ts

packages/k8s/tests/prune-retry-test.ts covers 22 cases: transient list/delete/create retries, a hung request abandoned after the per-request timeout and retried, a 404 treated as already-deleted, a 404 arriving after a dropped connection still treated as success, a 409 on a retried create read back as success (including a read-back that has to be retried itself), a non-retryable and a non-AlreadyExists error still failing, and secrets list/delete/create retry.

Scope note

This PR only adds timeout + retry and the idempotency that comes with it. One thing is intentionally not covered: making cleanup best-effort (warn and exit 0). Changing the job's failure semantics is a policy decision and should be a separate PR — with the current budget a dead path can still fail the job after ~141s instead of today's ~127s kernel timeout.

Related

Resolve https://github.com/opensourceways/ascend-ci-deployment/issues/2656 — root cause analysis and cluster-side evidence (HK-001 CI Stop containers timeouts, same incident as #2638).

Cleanup (prunePods / pruneSecrets) issued a single untimed request per
resource. A transient ClusterIP -> apiserver blip could pin the TCP
connection to a dead backend via conntrack, hanging the connect until the
kernel exhausted SYN retries (~127s) and failing a job whose test steps
had all passed (vllm-ascend 11-09, twenty-one jobs).

Wrap every cleanup list/delete call in retryK8sApiCall so each attempt is
bounded by the default 15s timeout and reopens a fresh connection, and
accept an injectable client + retry options for unit testing.
@opensourceways-bot

Copy link
Copy Markdown

Welcome To opensourceways Community

Hey @wyhhai , thanks for your contribution to the community.

Bot Usage Manual

I'm the Bot here serving you. You can find the instructions on how to interact with me at Here . That means you can comment below every pull request or issue to trigger Bot Commands.

Contact Guide

If you have any questions, please contact the SIG: infratructure ,
and any of the maintainers: @GeorgeCao-hw, @TangJia025, @pkking, @zhongjun2 ,
and any of the committers: @GeorgeCao-hw, @Goalina, @Hourunze1997, @JavaPythonAIForBAT, @KadenZhang3321, @LiYanghang00, @ccijunk, @drizzlezyk, @pkking, @rosecoffe, @tfhddd, @yao-xiaobai, @zhongjun2, @zkhzkhz .

@opensourceways-bot

Copy link
Copy Markdown

CLA Signature Pass

wyhhai, thanks for your pull request. All authors of the commits have signed the CLA. 👍

@opensourceways-bot

Copy link
Copy Markdown

Linking Issue Notice

@wyhhai , the pull request must be linked to at least one issue.
If an issue has already been linked, but the needs-issue label remains, you can remove the label by commenting /check-issue .

@wyhhai

wyhhai commented Nov 12, 2026

Copy link
Copy Markdown
Author

/check-issue

@Longwt123

Copy link
Copy Markdown
Collaborator

谢谢,改动方向和描述一致:cleanup 里的 list/delete 都走 retryK8sApiCall(单请求 15s 超时、4 次尝试、3/6/12s 退避),可注入 client 也方便单测。我在本地实际跑过:

  • tsc --noEmit -p packages/k8s:通过
  • jest tests/prune-retry-test.ts tests/retry-utils-test.ts --runInBand:14 个用例全过
  • delete 参数与改动前一致(gracePeriodSeconds: 0、secret 删除参数相同)

下面几项建议按风险从高到低处理。

1. 重试 delete 可能因 404 反而失败(P0)

isRetryableK8sApiError() 把 404 视为不可重试,prune-retry-test.ts 也把这个行为固化了。但单请求超时的本质是「放弃一次结果未知的请求」:如果服务端其实已经删掉、只是响应在回程丢了,重试就会拿到 404,Promise.all 直接 reject —— 正好又回到「业务步骤全绿、Stop containers 红」的原始症状。

#2656 里记录的单向丢包特征(只有去程 SYN、没有 REJECT)让这个场景并非纯理论。

建议:delete 的包装里把 404 当作成功,例如

await retryK8sApiCall(...).catch(err => {
  if ((err as { code?: number }).code === 404) return
  throw err
})

或者给 retryK8sApiCall 加一个 ignoreStatusCodes 选项,并补一个「delete 已在服务端生效、响应丢失、重试收到 404」的用例。

2. Prepare 阶段的 createJobPod 没有覆盖(P1)

#2656 里 10 次 ETIMEDOUT 有 3 次发生在 Prepare 的 createJobPod。这些路径仍然是单发、无超时:createNamespacedPod(index.ts L181、L234)、createNamespacedSecret(L821、L848)。要么一起包上重试,要么在描述里明确写清本期只覆盖 cleanup。

3. 重试预算可能不够(P2)

最坏 4×15s + 3/6/12s ≈ 81s。而 #2656 记录的故障窗口从 14:02 持续到 16:18、跨节点反复出现,内核侧要约 127s 才报错。如果单次黑洞超过 81s,job 仍然会失败。可以的话把 attempts 提到 5–6,或者把「cleanup 降级为 best-effort」作为后续项 —— 但残余风险建议写进 PR/issue,而不是隐含。

4. 细节

  • 注释里 "A timeout aborts the stuck connect" 说得偏强:withTimeout 只是放弃 Promise,并没有 abort socket,每次重试都会把上一个请求留到内核超时。如果在意,可以把 abortSignal 透传下去。
  • 重试告警用 err instanceof Error ? err.message : String(err),非 Error 形状会打成 [object Object](新测试的输出里就能看到)。#2656 的验收标准 feat: add preferred pod affinity #2 要求尝试日志可读,建议把 code / cause.code 一起打出来。
  • deleteSecret() 改完后已无调用方(deletePod() 仍被 run-container-step 使用),属于死代码,可以删。
  • 小问题:正文里的 resolve https://.../issues/2656 建议改成 Refs。

- Treat 404 on delete as success: a request that timed out client-side may
  already have been applied server-side, so the retry legitimately observes
  NotFound and must not fail an otherwise green job.
- Raise the cleanup attempt budget to 6 (~141s) to cover the ~127s kernel
  SYN-retry window seen on the HK CI nodes.
- Log the error code instead of "[object Object]" for plain-object errors.
- Remove deleteSecret(), which lost its last caller when the delete path was
  inlined for retrying.
@opensourceways-bot

Copy link
Copy Markdown

CLA Signature Pass

wyhhai, thanks for your pull request. All authors of the commits have signed the CLA. 👍

@wyhhai

wyhhai commented Nov 12, 2026

Copy link
Copy Markdown
Author

感谢 review,四条都处理了(commit c508f44)。

1. delete 收到 404 反而失败(P0)—— 已修
新增 isK8sApiNotFoundError()(兼容 code / statusCode / body.code),prunePods / pruneSecrets 的 delete 包装在 404 时按成功处理。补了两个用例:

  • treats a 404 as already deleted instead of failing the cleanup
  • treats a 404 after a dropped connection as success(首次 ETIMEDOUT、重试 404)

原先固化了「404 = 失败」的那个用例已改写,并保留一个反向用例(400 仍然抛错)。

2. Prepare 阶段 createJobPod —— 确认未覆盖,但不在本 PR 里做
不是漏了,是幂等语义不同:create 重试如果第一次已成功、响应丢失,重试会拿到 409 AlreadyExists,同样不在可重试列表里,直接抛错就是同一个 bug 的翻版。正确做法是 409 时 readNamespacedPod 回读返回,比 cleanup 多约 15 行。已在 PR 正文的 Scope note 里写明:10 次 ETIMEDOUT 中 3 次发生在该路径,列为已知缺口,后续单独提 PR。

3. 重试预算 —— 已提到 6 次
prunePods / pruneSecrets 的默认值改为 { attempts: 6 },最坏约 141s,覆盖 ~127s 的 SYN 重试窗口。没有动 retryK8sApiCall 的全局默认值,避免影响 isAuthPermissionsOK 等其他调用方。

4. 细节 —— 全部采纳

  • 注释从 "aborts the stuck connect" 改为 "abandons the stuck request"(withTimeout 确实只是放弃 Promise,不 abort socket)
  • 重试告警改用 formatErrorForLog(),现在输出 code=ETIMEDOUT 而不是 [object Object](jest 输出里可以看到)
  • 删除已无调用方的 deleteSecret()
  • 正文 resolve 改为 Refs(补充一句:跨仓库链接用 closing keyword 本来也不会自动关闭,这次只是语义更准确)

本地验证:tsc --noEmit 通过;jest prune-retry-test.ts retry-utils-test.ts 16 个用例全过;eslint / prettier 干净。

@opensourceways-bot

Copy link
Copy Markdown

Linking Issue Notice

@wyhhai , the pull request must be linked to at least one issue.
If an issue has already been linked, but the needs-issue label remains, you can remove the label by commenting /check-issue .

@wyhhai

wyhhai commented Nov 12, 2026

Copy link
Copy Markdown
Author

/check-issue

Prepare-stage createJobPod was not covered by the cleanup retry work, yet 3 of
the 10 ETIMEDOUT events observed on the HK CI nodes happened there.

Creates cannot simply be retried: when the first attempt succeeds and only the
response is lost, the retry gets 409 AlreadyExists. Treat that as success by
reading the object back, which keeps the call idempotent.
@wyhhai

wyhhai commented Nov 12, 2026

Copy link
Copy Markdown
Author

补一下你提的第 2 点:Prepare 阶段的 create 也覆盖了(commit a0b96f4),不再是 TODO。

改动:

  • createJobPod、createContainerStepPod、createDockerSecret、createSecretForEnvs 全部走同一套重试(6 次、单请求 15s、3/6/12/15/15s 退避)。
  • 幂等处理:create 超时后重试拿到 409 AlreadyExists 时不再抛错,而是 readNamespacedPod / readNamespacedSecret 回读后返回——这正是之前把它列为缺口的原因,现在补上了。
  • 非 409(例如 422 校验失败)仍然原样抛出,不会被吞掉。

新增 5 个用例:

  • retries a transient failure when creating a job pod
  • reads the pod back when a retried create reports AlreadyExists
  • does not swallow a non-AlreadyExists create error
  • retries a transient failure when creating a secret
  • reads the secret back when a retried create reports AlreadyExists

本地验证:tsc --noEmit 通过,jest 21 个用例全过,eslint / prettier 干净。

剩下的唯一 Scope 项是「cleanup 改成 best-effort(warn + exit 0)」,因为它改变失败语义,仍建议单独 PR。

@opensourceways-bot

Copy link
Copy Markdown

CLA Signature Pass

wyhhai, thanks for your pull request. All authors of the commits have signed the CLA. 👍

@opensourceways-bot

Copy link
Copy Markdown

Linking Issue Notice

@wyhhai , the pull request must be linked to at least one issue.
If an issue has already been linked, but the needs-issue label remains, you can remove the label by commenting /check-issue .

@Longwt123

Copy link
Copy Markdown
Collaborator

LGTM,剩余两点小问题:

  1. 409 回读没有超时/重试:createPodIdempotently / createSecretIdempotently 里 409 之后的 readNamespacedPod / readNamespacedSecret 是裸调用。如果重试期间链路仍在黑洞,这一步又会退回到「无超时 + 内核约 127s 挂死」,等于把老问题留在 create 路径上。建议同样包一层 retryK8sApiCall(复用 BLACKHOLE_RETRY_OPTIONS),并补一个「409 → 回读超时 → 重试成功」的用例。

  2. 信息泄露:正文里的内部 apiserver 地址 https://10.247.0.1/api/v1/... 建议脱敏为 https://<apiserver>/api/v1/...。

其余改动本地复验过:tsc --noEmit -p packages/k8s 通过、jest tests/prune-retry-test.ts tests/retry-utils-test.ts 21 个用例全过、eslint 干净。

The 409 read-back was a bare, untimed call, which reintroduced the exact
hang this PR removes whenever the link is still blackholed. Route it through
retryK8sApiCall with the same policy as the create.
@wyhhai

wyhhai commented Nov 13, 2026

Copy link
Copy Markdown
Author

两点都改了(commit 7d471bf)。

1. 409 回读包上重试 —— 说得对,这是我漏掉的
createPodIdempotently / createSecretIdempotently 里 409 之后的 readNamespacedPod / readNamespacedSecret 原来是裸调用,链路仍在黑洞时会退回「无超时 + 127s 挂死」,等于把老问题搬到了 create 路径。现在同样走 retryK8sApiCall,复用 BLACKHOLE_RETRY_OPTIONS(6 次 / 15s / 3-6-12-15-15s)。

补了用例 retries the read-back when the link is still blackholed:create 超时 → 409 → 回读超时 → 回读重试成功,断言 readNamespacedPod 被调用 2 次且返回回读到的对象。

2. 正文脱敏 —— 已改
https://10.247.0.1/api/v1/namespaces/vllm-project/pods → https://<apiserver>/api/v1/namespaces/<namespace>/pods。


本地复验:tsc --noEmit -p packages/k8s 通过、jest tests/prune-retry-test.ts tests/retry-utils-test.ts 22 个用例全过、eslint / prettier 干净。

@opensourceways-bot

Copy link
Copy Markdown

CLA Signature Pass

wyhhai, thanks for your pull request. All authors of the commits have signed the CLA. 👍

@wyhhai

wyhhai commented Nov 13, 2026

Copy link
Copy Markdown
Author

/check-issue

@Longwt123

Copy link
Copy Markdown
Collaborator

/lgtm
/approve

@opensourceways-bot

Copy link
Copy Markdown

Label Command Feedback

  • Thanks for your review, your opinion is very important to us. The maintainers will consider your advice carefully.
  • The label(s) **** permission is outside.
  • Thanks for your review, your opinion is very important to us. The maintainers will consider your advice carefully.
  • The label(s) approved permission is outside.

@Goalina
Goalina merged commit 1aaa29d into opensourceways:main Nov 19, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants