Skip to content

Commit

Permalink
Fix number of bugs in datachannel.WriteTo
Browse files Browse the repository at this point in the history
1. WriteTo should not return EOF (see golang/go#44411).
2. In case of EOF, WriteTo should write the final payload before returning
3. In case of non EOF error, WriteTo should return n, not nw.
  • Loading branch information
YairHalberstadt authored Feb 8, 2023
1 parent b558522 commit e358fc3
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions datachannel/data_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,14 @@ func (c *SsmDataChannel) WriteTo(w io.Writer) (n int64, err error) {

if nr > 0 {
payload, err = c.HandleMsg(buf[:nr])
var isEOF bool
if err != nil {
log.Printf("WriteTo HandleMsg error: %v", err)
return int64(nw), err
if errors.Is(err, io.EOF) {
isEOF = true
} else {
log.Printf("WriteTo HandleMsg error: %v", err)
return n, err
}
}

if len(payload) > 0 {
Expand All @@ -145,6 +150,10 @@ func (c *SsmDataChannel) WriteTo(w io.Writer) (n int64, err error) {
return n, err
}
}

if isEOF {
return n, nil
}
}
}
}
Expand Down

0 comments on commit e358fc3

Please sign in to comment.