Skip to content

Commit

Permalink
lib/protocol: Return from ClusterConfig when closed (syncthing#5752)
Browse files Browse the repository at this point in the history
  • Loading branch information
imsodin authored and calmh committed May 29, 2019
1 parent e2a647a commit 6664e01
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/protocol/protocol.go
Expand Up @@ -332,8 +332,11 @@ func (c *rawConnection) Request(folder string, name string, offset int64, size i
// ClusterConfig sends the cluster configuration message to the peer.
// It must be called just once (as per BEP), otherwise it will panic.
func (c *rawConnection) ClusterConfig(config ClusterConfig) {
c.clusterConfigBox <- &config
close(c.clusterConfigBox)
select {
case c.clusterConfigBox <- &config:
close(c.clusterConfigBox)
case <-c.closed:
}
}

func (c *rawConnection) Closed() bool {
Expand Down
23 changes: 23 additions & 0 deletions lib/protocol/protocol_test.go
Expand Up @@ -757,3 +757,26 @@ func TestSha256OfEmptyBlock(t *testing.T) {
}
}
}

// TestClusterConfigAfterClose checks that ClusterConfig does not deadlock when
// ClusterConfig is called on a closed connection.
func TestClusterConfigAfterClose(t *testing.T) {
m := newTestModel()

c := NewConnection(c0ID, &testutils.BlockingRW{}, &testutils.BlockingRW{}, m, "name", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
c.Start()

c.internalClose(errManual)

done := make(chan struct{})
go func() {
c.ClusterConfig(ClusterConfig{})
close(done)
}()

select {
case <-done:
case <-time.After(time.Second):
t.Fatal("timed out before Cluster Config returned")
}
}

0 comments on commit 6664e01

Please sign in to comment.