connection.writer wasn't checking closed state

This commit is contained in:
Matt Joiner 2018-02-02 13:36:18 +11:00
parent 7b037cd024
commit 8a43910e2d
2 changed files with 12 additions and 0 deletions

View File

@ -348,6 +348,7 @@ func (cn *connection) SetInterested(interested bool, msg func(pp.Message) bool)
// are okay.
type messageWriter func(pp.Message) bool
// Proxies the messageWriter's response.
func (cn *connection) request(r request, mw messageWriter) bool {
if cn.requests == nil {
cn.requests = make(map[request]struct{}, cn.nominalMaxRequests())
@ -359,6 +360,9 @@ func (cn *connection) request(r request, mw messageWriter) bool {
panic("requesting piece peer doesn't have")
}
cn.requests[r] = struct{}{}
if _, ok := cn.t.conns[cn]; !ok {
panic("requesting but not in active conns")
}
cn.t.pendingRequests[r]++
return mw(pp.Message{
Type: pp.Request,
@ -421,6 +425,9 @@ func (cn *connection) writer(keepAliveTimeout time.Duration) {
defer cn.Close()
defer keepAliveTimer.Stop()
for {
if cn.closed.IsSet() {
return
}
buf.Write(cn.postedBuffer.Bytes())
cn.postedBuffer.Reset()
if buf.Len() == 0 {

View File

@ -1171,6 +1171,11 @@ func (t *Torrent) SetInfoBytes(b []byte) (err error) {
// Returns true if connection is removed from torrent.Conns.
func (t *Torrent) deleteConnection(c *connection) (ret bool) {
if !c.closed.IsSet() {
panic("connection is not closed")
// There are behaviours prevented by the closed state that will fail
// if the connection has been deleted.
}
_, ret = t.conns[c]
delete(t.conns, c)
c.deleteAllRequests()