Skip to content

Commit 28d78d4

Browse files
authored
*: forbid the use of time.After (#6985)
1 parent 408139a commit 28d78d4

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

internal/transport/http2_server.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,11 @@ func NewServerTransport(conn net.Conn, config *ServerConfig) (_ ServerTransport,
334334
// closed, would lead to a TCP RST instead of FIN, and the client
335335
// encountering errors. For more info:
336336
// https://github.com/grpc/grpc-go/issues/5358
337+
timer := time.NewTimer(time.Second)
338+
defer timer.Stop()
337339
select {
338340
case <-t.readerDone:
339-
case <-time.After(time.Second):
341+
case <-timer.C:
340342
}
341343
t.conn.Close()
342344
}

vet.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ git grep 'func [A-Z]' -- "*_test.go" | not grep -v 'func Test\|Benchmark\|Exampl
8383
# - Do not import x/net/context.
8484
not git grep -l 'x/net/context' -- "*.go"
8585

86+
# - Do not use time.After except in tests. It has the potential to leak the
87+
# timer since there is no way to stop it early.
88+
git grep -l 'time.After(' -- "*.go" | not grep -v '_test.go\|test_utils\|testutils'
89+
8690
# - Do not import math/rand for real library code. Use internal/grpcrand for
8791
# thread safety.
8892
git grep -l '"math/rand"' -- "*.go" 2>&1 | not grep -v '^examples\|^interop/stress\|grpcrand\|^benchmark\|wrr_test'

0 commit comments

Comments
 (0)