Skip to content

Commit c3b0ff0

Browse files
committed
Problem: NetConn wrapper kept canceling context
Solution: Accept the zero time as a special case when setting deadlines. Somehow, this function gets called without a time and thus would previously Reset the timer to no time at all, thus expediting the call to cancel the context.
1 parent 3e63f82 commit c3b0ff0

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

netconn.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,23 @@ func (c *netConn) SetDeadline(t time.Time) error {
134134
}
135135

136136
func (c *netConn) SetWriteDeadline(t time.Time) error {
137-
c.writeTimer.Reset(t.Sub(time.Now()))
137+
if t.IsZero() {
138+
if !c.writeTimer.Stop() {
139+
<-c.writeTimer.C
140+
}
141+
} else {
142+
c.writeTimer.Reset(t.Sub(time.Now()))
143+
}
138144
return nil
139145
}
140146

141147
func (c *netConn) SetReadDeadline(t time.Time) error {
142-
c.readTimer.Reset(t.Sub(time.Now()))
148+
if t.IsZero() {
149+
if !c.readTimer.Stop() {
150+
<-c.readTimer.C
151+
}
152+
} else {
153+
c.readTimer.Reset(t.Sub(time.Now()))
154+
}
143155
return nil
144156
}

websocket_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ func TestHandshake(t *testing.T) {
130130
nc := websocket.NetConn(c)
131131
defer nc.Close()
132132

133-
nc.SetWriteDeadline(time.Now().Add(time.Second * 15))
133+
nc.SetWriteDeadline(0)
134+
time.Sleep(1)
135+
nc.SetWriteDeadline(time.Now().Add(time.Second * 14))
134136

135137
for i := 0; i < 3; i++ {
136138
_, err = nc.Write([]byte("hello"))
@@ -153,7 +155,9 @@ func TestHandshake(t *testing.T) {
153155
nc := websocket.NetConn(c)
154156
defer nc.Close()
155157

156-
nc.SetReadDeadline(time.Now().Add(time.Second * 15))
158+
nc.SetReadDeadline(0)
159+
time.Sleep(1)
160+
nc.SetReadDeadline(time.Now().Add(time.Second * 14))
157161

158162
read := func() error {
159163
p := make([]byte, len("hello"))

0 commit comments

Comments
 (0)