Skip to content

Commit 91b7aa4

Browse files
committed
fix: handle protocol shutdown more cleanly
1 parent acb24fa commit 91b7aa4

File tree

7 files changed

+47
-25
lines changed

7 files changed

+47
-25
lines changed

protocol/blockfetch/client.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
7373
go func() {
7474
<-c.Protocol.DoneChan()
7575
close(c.blockChan)
76+
close(c.startBatchResultChan)
7677
}()
7778
return c
7879
}
@@ -95,7 +96,10 @@ func (c *Client) GetBlockRange(start common.Point, end common.Point) error {
9596
c.busyMutex.Unlock()
9697
return err
9798
}
98-
err := <-c.startBatchResultChan
99+
err, ok := <-c.startBatchResultChan
100+
if !ok {
101+
return protocol.ProtocolShuttingDownError
102+
}
99103
if err != nil {
100104
c.busyMutex.Unlock()
101105
return err
@@ -112,7 +116,10 @@ func (c *Client) GetBlock(point common.Point) (ledger.Block, error) {
112116
c.busyMutex.Unlock()
113117
return nil, err
114118
}
115-
err := <-c.startBatchResultChan
119+
err, ok := <-c.startBatchResultChan
120+
if !ok {
121+
return nil, protocol.ProtocolShuttingDownError
122+
}
116123
if err != nil {
117124
c.busyMutex.Unlock()
118125
return nil, err

protocol/chainsync/client.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,15 @@ func (c *Client) GetCurrentTip() (*Tip, error) {
146146
if err := c.SendMessage(msg); err != nil {
147147
return nil, err
148148
}
149-
tip := <-c.currentTipChan
149+
tip, ok := <-c.currentTipChan
150+
if !ok {
151+
return nil, protocol.ProtocolShuttingDownError
152+
}
150153
// Clear out intersect result channel to prevent blocking
151-
<-c.intersectResultChan
154+
_, ok = <-c.intersectResultChan
155+
if !ok {
156+
return nil, protocol.ProtocolShuttingDownError
157+
}
152158
c.wantCurrentTip = false
153159
return &tip, nil
154160
}
@@ -171,6 +177,8 @@ func (c *Client) GetAvailableBlockRange(
171177
gotIntersectResult := false
172178
for {
173179
select {
180+
case <-c.DoneChan():
181+
return start, end, protocol.ProtocolShuttingDownError
174182
case tip := <-c.currentTipChan:
175183
end = tip.Point
176184
c.wantCurrentTip = false
@@ -200,6 +208,8 @@ func (c *Client) GetAvailableBlockRange(
200208
}
201209
for {
202210
select {
211+
case <-c.DoneChan():
212+
return start, end, protocol.ProtocolShuttingDownError
203213
case tip := <-c.currentTipChan:
204214
end = tip.Point
205215
c.wantCurrentTip = false
@@ -237,7 +247,9 @@ func (c *Client) Sync(intersectPoints []common.Point) error {
237247
if err := c.SendMessage(msg); err != nil {
238248
return err
239249
}
240-
if err := <-c.intersectResultChan; err != nil {
250+
if err, ok := <-c.intersectResultChan; !ok {
251+
return protocol.ProtocolShuttingDownError
252+
} else if err != nil {
241253
return err
242254
}
243255
// Pipeline the initial block requests to speed things up a bit

protocol/keepalive/client.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,9 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
6060
// Start goroutine to cleanup resources on protocol shutdown
6161
go func() {
6262
<-c.Protocol.DoneChan()
63+
// Stop any existing timer
6364
if c.timer != nil {
64-
// Stop timer and drain channel
65-
if ok := c.timer.Stop(); !ok {
66-
// Read item from channel, if available
67-
select {
68-
case <-c.timer.C:
69-
default:
70-
}
71-
}
65+
c.timer.Stop()
7266
}
7367
}()
7468
return c
@@ -93,13 +87,7 @@ func (c *Client) sendKeepAlive() {
9387
func (c *Client) startTimer() {
9488
// Stop any existing timer
9589
if c.timer != nil {
96-
if ok := c.timer.Stop(); !ok {
97-
// Read item from channel, if available
98-
select {
99-
case <-c.timer.C:
100-
default:
101-
}
102-
}
90+
c.timer.Stop()
10391
}
10492
// Create new timer
10593
c.timer = time.AfterFunc(c.config.Period, c.sendKeepAlive)

protocol/localstatequery/client.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ func (c *Client) acquire(point *common.Point) error {
154154
if err := c.SendMessage(msg); err != nil {
155155
return err
156156
}
157-
err := <-c.acquireResultChan
157+
err, ok := <-c.acquireResultChan
158+
if !ok {
159+
return protocol.ProtocolShuttingDownError
160+
}
158161
return err
159162
}
160163

@@ -178,7 +181,10 @@ func (c *Client) runQuery(query interface{}, result interface{}) error {
178181
if err := c.SendMessage(msg); err != nil {
179182
return err
180183
}
181-
resultCbor := <-c.queryResultChan
184+
resultCbor, ok := <-c.queryResultChan
185+
if !ok {
186+
return protocol.ProtocolShuttingDownError
187+
}
182188
if _, err := cbor.Decode(resultCbor, result); err != nil {
183189
return err
184190
}

protocol/localtxmonitor/client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ func (c *Client) acquire() error {
110110
return err
111111
}
112112
// Wait for reply
113-
<-c.acquireResultChan
113+
_, ok := <-c.acquireResultChan
114+
if !ok {
115+
return protocol.ProtocolShuttingDownError
116+
}
114117
return nil
115118
}
116119

protocol/localtxsubmission/client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ func (c *Client) SubmitTx(eraId uint16, tx []byte) error {
9494
if err := c.SendMessage(msg); err != nil {
9595
return err
9696
}
97-
err := <-c.submitResultChan
97+
err, ok := <-c.submitResultChan
98+
if !ok {
99+
return protocol.ProtocolShuttingDownError
100+
}
98101
return err
99102
}
100103

protocol/peersharing/client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ func (c *Client) GetPeers(amount uint8) ([]interface{}, error) {
6565
if err := c.SendMessage(msg); err != nil {
6666
return nil, err
6767
}
68-
peers := <-c.sharePeersChan
68+
peers, ok := <-c.sharePeersChan
69+
if !ok {
70+
return nil, protocol.ProtocolShuttingDownError
71+
}
6972
return peers, nil
7073
}
7174

0 commit comments

Comments
 (0)