don’t return data for writing from a stream if it was closed with an error

This commit is contained in:
Marten Seemann 2017-01-03 15:31:32 +07:00
parent bf0caf3c03
commit 9e09198df6
No known key found for this signature in database
GPG key ID: 3603F40B121FCDEA
2 changed files with 21 additions and 2 deletions

View file

@ -158,13 +158,20 @@ func (s *stream) Write(p []byte) (int, error) {
func (s *stream) lenOfDataForWriting() protocol.ByteCount { func (s *stream) lenOfDataForWriting() protocol.ByteCount {
s.mutex.Lock() s.mutex.Lock()
l := protocol.ByteCount(len(s.dataForWriting)) var l protocol.ByteCount
if s.err == nil {
l = protocol.ByteCount(len(s.dataForWriting))
}
s.mutex.Unlock() s.mutex.Unlock()
return l return l
} }
func (s *stream) getDataForWriting(maxBytes protocol.ByteCount) []byte { func (s *stream) getDataForWriting(maxBytes protocol.ByteCount) []byte {
s.mutex.Lock() s.mutex.Lock()
if s.err != nil {
s.mutex.Unlock()
return nil
}
if s.dataForWriting == nil { if s.dataForWriting == nil {
s.mutex.Unlock() s.mutex.Unlock()
return nil return nil
@ -207,7 +214,6 @@ func (s *stream) sentFin() {
func (s *stream) AddStreamFrame(frame *frames.StreamFrame) error { func (s *stream) AddStreamFrame(frame *frames.StreamFrame) error {
maxOffset := frame.Offset + frame.DataLen() maxOffset := frame.Offset + frame.DataLen()
err := s.flowControlManager.UpdateHighestReceived(s.streamID, maxOffset) err := s.flowControlManager.UpdateHighestReceived(s.streamID, maxOffset)
if err != nil { if err != nil {
return err return err
} }

View file

@ -548,6 +548,19 @@ var _ = Describe("Stream", func() {
Expect(n).To(BeZero()) Expect(n).To(BeZero())
Expect(err).To(MatchError(testErr)) Expect(err).To(MatchError(testErr))
}) })
It("doesn't get data for writing if an error occurred", func() {
go func() {
_, err := str.Write([]byte("foobar"))
Expect(err).To(MatchError(testErr))
}()
Eventually(func() []byte { return str.dataForWriting }).ShouldNot(BeNil())
Expect(str.lenOfDataForWriting()).ToNot(BeZero())
str.RegisterError(testErr)
data := str.getDataForWriting(6)
Expect(data).To(BeNil())
Expect(str.lenOfDataForWriting()).To(BeZero())
})
}) })
Context("when CloseRemote is called", func() { Context("when CloseRemote is called", func() {