Skip to content

fix(gate-hub): drain a rejected client before closing so the gate still reads the certificate alert - #186

Merged
TN019 merged 1 commit into
mainfrom
fix/tls-rejection-close
Dec 1, 2026
Merged

TN019 merged 1 commit into
mainfrom
fix/tls-rejection-close

Conversation

@TN019

@TN019 TN019 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Why

Two gated_tls cases failed now and then under the asan preset in CI, each passing on a re-run:

  • test_ticket_download_hub_rejects_gate_certificate saw curl=55 instead of curl=56.
  • test_expired_own_certificate_rejected_after_handshake saw the gate log reason=timeout (about 10 ms after rejected_by_peer, or instead of it).

Both come from the same hub behaviour. With TLS 1.3 the hub verifies the gate certificate after reading the gate's Certificate message, sends its alert and fails the handshake while the gate's CertificateVerify and Finished are still unread. close() on a socket with unread input sends a reset instead of FIN. If the reset reaches the gate before it writes its first request, the write fails, the alert is never read, and the gate falls back to reason=timeout (libcurl reports a send error, 55). This is the misdiagnosis ADR 0019 set out to remove, and operators would look at the network instead of the certificate.

Locally, 90 runs of the two cases under asan failed 6 times before this change.

What changed

  • hub_tls_linger_rejected() in gate-hub/tls.c: after a failed handshake the hub shuts down its sending side (FIN after the alert) and discards input until the client closes, for at most 500 ms. Both listeners call it before closing; a handshake that ran out of time closes at once as before, and the status listener never lingers past its connection deadline.
  • tests/test_ticket_fetch_progress.py sets SO_REUSEADDR on its fixed 8444 listener, as the other fixed-port test listeners already do. The hub now closes rejected connections first, so earlier tests leave 8444 connections in TIME_WAIT.
  • CHANGELOG.md and CHANGELOG.zh-CN.md record the fix under Unreleased.

How to verify

  • CI on this pull request.
  • After the change, 280 runs of the two cases under asan, partly under heavy load, had no failure. The test assertions are unchanged.

背景

CI 的 asan 预设下,gated_tls 有两个用例偶尔失败,重跑都能通过:

  • test_ticket_download_hub_rejects_gate_certificate 得到的是 curl=55,而不是 curl=56。
  • test_expired_own_certificate_rejected_after_handshake 中闸机记录了 reason=timeout(在 rejected_by_peer 之后约 10 毫秒,或者干脆取代了它)。

两者出自 hub 的同一个行为。在 TLS 1.3 下,hub 读到闸机的 Certificate 消息就开始校验证书,发出告警并让握手失败,这时闸机的 CertificateVerify 和 Finished 还没被读。对仍有未读数据的套接字调用 close(),内核发出的是 RST 而不是 FIN。如果 RST 在闸机写第一个请求之前到达,写就会失败,告警根本没被读到,闸机只好记成 reason=timeout(libcurl 报发送错误 55)。这正是 ADR 0019 想消除的误诊:运维人员会去查网络,而不是查证书。

修改前,本机 asan 下这两个用例跑 90 次失败 6 次。

改动

  • gate-hub/tls.c 新增 hub_tls_linger_rejected():握手失败后,hub 先关闭发送方向(告警之后发 FIN),再丢弃客户端发来的数据,直到对方关闭,最多等 500 毫秒。两个监听口在关闭前都调用它;握手超时的连接仍立即关闭,状态监听口也不会超出连接期限。
  • tests/test_ticket_fetch_progress.py 给固定在 8444 的监听设置 SO_REUSEADDR,与其他固定端口的测试监听一致。hub 现在主动关闭被拒绝的连接,前面的测试会在 8444 上留下 TIME_WAIT。
  • CHANGELOG.md 和 CHANGELOG.zh-CN.md 的“未发布”部分记录了这次修复。

怎么验证

  • 本 PR 的 CI。
  • 修改后,本机 asan 下这两个用例跑了 280 次(部分在高负载下),没有失败。测试断言没有改动。

@TN019
TN019 force-pushed the fix/tls-rejection-close branch from bdb5cf7 to e35d3fb Compare December 1, 2026 09:11
@TN019
TN019 merged commit e35d3fb into main Dec 1, 2026
5 checks passed
@TN019
TN019 deleted the fix/tls-rejection-close branch December 1, 2026 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant