From 7933df96f30b86870abfed486363fdbb85e724f8 Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Sat, 21 Nov 2026 01:00:52 +0100 Subject: [PATCH] test: stabilize the UDP control-close test and the sealed hook test The UDP close test raced the asynchronous association close with a goroutine count heuristic; probe until forwarding stops instead. The sealed hook test called InitSecrets with a fresh temp dir on a database already sealed by the package test key; use the package key. gofmt trace.go. --- db/traffic_tamper_hooks_test.go | 3 --- traffic/rewrite/trace.go | 10 +++++----- traffic/socks5_udp_test.go | 25 ++++++++++++------------- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/db/traffic_tamper_hooks_test.go b/db/traffic_tamper_hooks_test.go index 03c116d..8a347ad 100644 --- a/db/traffic_tamper_hooks_test.go +++ b/db/traffic_tamper_hooks_test.go @@ -14,9 +14,6 @@ func TestTrafficTamperHookCRUDSealedAndVersions(t *testing.T) { SkipPGUnavailable(t, err) } t.Cleanup(func() { _ = d.Close() }) - if err := d.InitSecrets(t.TempDir()); err != nil { - t.Fatalf("InitSecrets: %v", err) - } const srcV1 = `package hook const apiSecret = "super-secret-hmac-key-v1" diff --git a/traffic/rewrite/trace.go b/traffic/rewrite/trace.go index a7ed0ed..68f77d3 100644 --- a/traffic/rewrite/trace.go +++ b/traffic/rewrite/trace.go @@ -44,11 +44,11 @@ const ( // TraceLine is one row of an exchange's rewrite trace, in order. An exchange is // rewritten when it has at least one applied line. type TraceLine struct { - Phase Phase - SourceKind string // "rule" (default when empty) | "hook" - RuleID int64 // 0 on a global or hook line - HookID int64 // 0 on a rule or global line - HookVersion int // 0 on a rule or global line + Phase Phase + SourceKind string // "rule" (default when empty) | "hook" + RuleID int64 // 0 on a global or hook line + HookID int64 // 0 on a rule or global line + HookVersion int // 0 on a rule or global line // RuleSnapshot is the rule or hook metadata as JSON, on its first line in the exchange only. RuleSnapshot string Action string diff --git a/traffic/socks5_udp_test.go b/traffic/socks5_udp_test.go index 57f9195..f0e934e 100644 --- a/traffic/socks5_udp_test.go +++ b/traffic/socks5_udp_test.go @@ -9,7 +9,6 @@ import ( "net" "os" "path/filepath" - "runtime" "strconv" "strings" "sync/atomic" @@ -321,7 +320,6 @@ func TestSOCKS5UDPAssociateRelayStoreAndControlConnClose(t *testing.T) { } defer tr.Close() - baseG := runtime.NumGoroutine() proxyAddr, err := tr.StartSOCKS5(SOCKS5Config{ Addr: "127.0.0.1:0", EnableUDP: true, @@ -422,23 +420,24 @@ func TestSOCKS5UDPAssociateRelayStoreAndControlConnClose(t *testing.T) { t.Fatalf("HTTP exchanges rows = (%d, %v), want 0", httpRows, err) } - // 4. Closing TCP control connection must tear down the UDP association promptly. + // 4. Closing the TCP control connection must tear down the UDP association. + // The close is processed asynchronously by the association goroutine, so a + // single immediate probe races it: probe until a datagram is no longer + // forwarded, then assert the relay is closed. _ = ctrl.Close() deadline := time.Now().Add(2 * time.Second) + closed := false for time.Now().Before(deadline) { - if runtime.NumGoroutine() <= baseG+6 { + before := received.Load() + _, _ = cliUDP.WriteToUDP(makeClientUDPPacketIPv4(echoAddr.IP, echoAddr.Port, 0x00, []byte("after-close")), relayAddr) + time.Sleep(50 * time.Millisecond) + if received.Load() == before { + closed = true break } - time.Sleep(25 * time.Millisecond) } - beforeAfterClose := received.Load() - _, _ = cliUDP.WriteToUDP(makeClientUDPPacketIPv4(echoAddr.IP, echoAddr.Port, 0x00, []byte("after-close")), relayAddr) - _ = cliUDP.SetReadDeadline(time.Now().Add(150 * time.Millisecond)) - if _, _, rerr := cliUDP.ReadFromUDP(buf); rerr == nil { - t.Fatal("expected no UDP reply after TCP control connection closed") - } - if received.Load() != beforeAfterClose { - t.Fatal("datagram was forwarded after TCP control connection closed") + if !closed { + t.Fatal("UDP association still forwards datagrams 2s after the TCP control connection closed") } }