Repository navigation
fix(gate-hub): drain a rejected client before closing so the gate still reads the certificate alert - #186
Merged
Conversation
…ll reads the certificate alert
TN019
force-pushed
the
fix/tls-rejection-close
branch
from
December 1, 2026 09:11
bdb5cf7 to
e35d3fb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Two
gated_tlscases failed now and then under the asan preset in CI, each passing on a re-run:test_ticket_download_hub_rejects_gate_certificatesawcurl=55instead ofcurl=56.test_expired_own_certificate_rejected_after_handshakesaw the gate logreason=timeout(about 10 ms afterrejected_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 toreason=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()ingate-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.pysetsSO_REUSEADDRon 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.mdandCHANGELOG.zh-CN.mdrecord the fix under Unreleased.How to verify
背景
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的“未发布”部分记录了这次修复。怎么验证