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. // are okay.
type messageWriter func(pp.Message) bool type messageWriter func(pp.Message) bool
// Proxies the messageWriter's response.
func (cn *connection) request(r request, mw messageWriter) bool { func (cn *connection) request(r request, mw messageWriter) bool {
if cn.requests == nil { if cn.requests == nil {
cn.requests = make(map[request]struct{}, cn.nominalMaxRequests()) 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") panic("requesting piece peer doesn't have")
} }
cn.requests[r] = struct{}{} cn.requests[r] = struct{}{}
if _, ok := cn.t.conns[cn]; !ok {
panic("requesting but not in active conns")
}
cn.t.pendingRequests[r]++ cn.t.pendingRequests[r]++
return mw(pp.Message{ return mw(pp.Message{
Type: pp.Request, Type: pp.Request,
@ -421,6 +425,9 @@ func (cn *connection) writer(keepAliveTimeout time.Duration) {
defer cn.Close() defer cn.Close()
defer keepAliveTimer.Stop() defer keepAliveTimer.Stop()
for { for {
if cn.closed.IsSet() {
return
}
buf.Write(cn.postedBuffer.Bytes()) buf.Write(cn.postedBuffer.Bytes())
cn.postedBuffer.Reset() cn.postedBuffer.Reset()
if buf.Len() == 0 { 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. // Returns true if connection is removed from torrent.Conns.
func (t *Torrent) deleteConnection(c *connection) (ret bool) { 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] _, ret = t.conns[c]
delete(t.conns, c) delete(t.conns, c)
c.deleteAllRequests() c.deleteAllRequests()