Skip to content

Commit da6fac4

Browse files
committed
TUN-8297: Improve write timeout logging on safe_stream.go
## Summary: In order to properly monitor what is happening with the new write timeouts that we introduced in TUN-8244 we need proper logging. Right now we were logging write timeouts when the safe stream was being closed which didn't make sense because it was miss leading, so this commit prevents that by adding a flag that allows us to know whether we are closing the stream or not.
1 parent 47ad323 commit da6fac4

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

quic/safe_stream.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"net"
66
"sync"
7+
"sync/atomic"
78
"time"
89

910
"github.com/quic-go/quic-go"
@@ -19,6 +20,7 @@ type SafeStreamCloser struct {
1920
stream quic.Stream
2021
writeTimeout time.Duration
2122
log *zerolog.Logger
23+
closing atomic.Bool
2224
}
2325

2426
func NewSafeStreamCloser(stream quic.Stream, writeTimeout time.Duration, log *zerolog.Logger) *SafeStreamCloser {
@@ -44,27 +46,35 @@ func (s *SafeStreamCloser) Write(p []byte) (n int, err error) {
4446
}
4547
nBytes, err := s.stream.Write(p)
4648
if err != nil {
47-
s.handleTimeout(err)
49+
s.handleWriteError(err)
4850
}
4951

5052
return nBytes, err
5153
}
5254

5355
// Handles the timeout error in case it happened, by canceling the stream write.
54-
func (s *SafeStreamCloser) handleTimeout(err error) {
56+
func (s *SafeStreamCloser) handleWriteError(err error) {
57+
// If we are closing the stream we just ignore any write error.
58+
if s.closing.Load() {
59+
return
60+
}
5561
var netErr net.Error
5662
if errors.As(err, &netErr) {
5763
if netErr.Timeout() {
58-
// We don't need to log if what cause the timeout was `no network activity`.
64+
// We don't need to log if what cause the timeout was no network activity.
5965
if !errors.Is(netErr, &idleTimeoutError) {
6066
s.log.Error().Err(netErr).Msg("Closing quic stream due to timeout while writing")
6167
}
68+
// We need to explicitly cancel the write so that it frees all buffers.
6269
s.stream.CancelWrite(0)
6370
}
6471
}
6572
}
6673

6774
func (s *SafeStreamCloser) Close() error {
75+
// Set this stream to a closing state.
76+
s.closing.Store(true)
77+
6878
// Make sure a possible writer does not block the lock forever. We need it, so we can close the writer
6979
// side of the stream safely.
7080
_ = s.stream.SetWriteDeadline(time.Now())

0 commit comments

Comments
 (0)