mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-12 16:26:37 +00:00
Fix locking around close events
This commit is contained in:
parent
223bdbb9aa
commit
7948d046d1
@ -139,10 +139,11 @@ func (c *Connection) Index(idx []FileInfo) {
|
|||||||
|
|
||||||
// Request returns the bytes for the specified block after fetching them from the connected peer.
|
// Request returns the bytes for the specified block after fetching them from the connected peer.
|
||||||
func (c *Connection) Request(name string, offset uint64, size uint32, hash []byte) ([]byte, error) {
|
func (c *Connection) Request(name string, offset uint64, size uint32, hash []byte) ([]byte, error) {
|
||||||
if c.isClosed() {
|
c.Lock()
|
||||||
|
if c.closed {
|
||||||
|
c.Unlock()
|
||||||
return nil, ErrClosed
|
return nil, ErrClosed
|
||||||
}
|
}
|
||||||
c.Lock()
|
|
||||||
rc := make(chan asyncResult)
|
rc := make(chan asyncResult)
|
||||||
c.awaiting[c.nextId] = rc
|
c.awaiting[c.nextId] = rc
|
||||||
c.mwriter.writeHeader(header{0, c.nextId, messageTypeRequest})
|
c.mwriter.writeHeader(header{0, c.nextId, messageTypeRequest})
|
||||||
@ -169,11 +170,12 @@ func (c *Connection) Request(name string, offset uint64, size uint32, hash []byt
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Connection) Ping() (time.Duration, bool) {
|
func (c *Connection) Ping() (time.Duration, bool) {
|
||||||
if c.isClosed() {
|
c.Lock()
|
||||||
|
if c.closed {
|
||||||
|
c.Unlock()
|
||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
c.Lock()
|
rc := make(chan asyncResult, 1)
|
||||||
rc := make(chan asyncResult)
|
|
||||||
c.awaiting[c.nextId] = rc
|
c.awaiting[c.nextId] = rc
|
||||||
t0 := time.Now()
|
t0 := time.Now()
|
||||||
c.mwriter.writeHeader(header{0, c.nextId, messageTypePing})
|
c.mwriter.writeHeader(header{0, c.nextId, messageTypePing})
|
||||||
@ -269,17 +271,14 @@ func (c *Connection) readerLoop() {
|
|||||||
if c.mreader.err != nil {
|
if c.mreader.err != nil {
|
||||||
c.close()
|
c.close()
|
||||||
} else {
|
} else {
|
||||||
c.RLock()
|
c.Lock()
|
||||||
rc, ok := c.awaiting[hdr.msgID]
|
rc, ok := c.awaiting[hdr.msgID]
|
||||||
c.RUnlock()
|
delete(c.awaiting, hdr.msgID)
|
||||||
|
c.Unlock()
|
||||||
|
|
||||||
if ok {
|
if ok {
|
||||||
rc <- asyncResult{data, c.mreader.err}
|
rc <- asyncResult{data, c.mreader.err}
|
||||||
close(rc)
|
close(rc)
|
||||||
|
|
||||||
c.Lock()
|
|
||||||
delete(c.awaiting, hdr.msgID)
|
|
||||||
c.Unlock()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user