Skip to content

Commit b5c3b44

Browse files
authored
Merge pull request #127 from lattwood/lattwood/handle_shutdown_edgecase
Streams should check for Session shutdown when waiting for data & clean up timers
2 parents 8bd691f + 84b3fc6 commit b5c3b44

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

stream.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,17 @@ WAIT:
143143
timeout = timer.C
144144
}
145145
select {
146+
case <-s.session.shutdownCh:
146147
case <-s.recvNotifyCh:
147-
if timer != nil {
148-
timer.Stop()
149-
}
150-
goto START
151148
case <-timeout:
152149
return 0, ErrTimeout
153150
}
151+
if timer != nil {
152+
if !timer.Stop() {
153+
<-timeout
154+
}
155+
}
156+
goto START
154157
}
155158

156159
// Write is used to write to the stream
@@ -219,17 +222,25 @@ START:
219222

220223
WAIT:
221224
var timeout <-chan time.Time
225+
var timer *time.Timer
222226
writeDeadline := s.writeDeadline.Load().(time.Time)
223227
if !writeDeadline.IsZero() {
224228
delay := time.Until(writeDeadline)
225-
timeout = time.After(delay)
229+
timer = time.NewTimer(delay)
230+
timeout = timer.C
226231
}
227232
select {
233+
case <-s.session.shutdownCh:
228234
case <-s.sendNotifyCh:
229-
goto START
230235
case <-timeout:
231236
return 0, ErrTimeout
232237
}
238+
if timer != nil {
239+
if !timer.Stop() {
240+
<-timeout
241+
}
242+
}
243+
goto START
233244
}
234245

235246
// sendFlags determines any flags that are appropriate

0 commit comments

Comments
 (0)