only delete streams once we are done reading AND writing

fixes #98
This commit is contained in:
Lucas Clemente
2016-05-15 15:03:02 +02:00
parent d78b2f4d57
commit d4de18c472
3 changed files with 48 additions and 2 deletions

View File

@@ -29,7 +29,10 @@ type stream struct {
err error
mutex sync.Mutex
// eof is set if we are finished reading
eof int32 // really a bool
// closed is set when we are finished writing
closed int32 // really a bool
frameQueue streamFrameSorter
newFrameOrErrCond sync.Cond
@@ -195,6 +198,7 @@ func (s *stream) Write(p []byte) (int, error) {
// Close implements io.Closer
func (s *stream) Close() error {
atomic.StoreInt32(&s.closed, 1)
return s.session.QueueStreamFrame(&frames.StreamFrame{
StreamID: s.streamID,
Offset: s.writeOffset,
@@ -222,6 +226,7 @@ func (s *stream) maybeTriggerWindowUpdate() {
// RegisterError is called by session to indicate that an error occurred and the
// stream should be closed.
func (s *stream) RegisterError(err error) {
atomic.StoreInt32(&s.closed, 1)
s.mutex.Lock()
defer s.mutex.Unlock()
if s.err != nil { // s.err must not be changed!
@@ -236,6 +241,14 @@ func (s *stream) finishedReading() bool {
return atomic.LoadInt32(&s.eof) != 0
}
func (s *stream) finishedWriting() bool {
return atomic.LoadInt32(&s.closed) != 0
}
func (s *stream) finished() bool {
return s.finishedReading() && s.finishedWriting()
}
func (s *stream) StreamID() protocol.StreamID {
return s.streamID
}