Skip to content

Commit 7ed57ef

Browse files
authored
statesync: more orderly dispatcher shutdown (tendermint#7601)
1 parent 2199e0a commit 7ed57ef

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

internal/statesync/dispatcher.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (d *Dispatcher) dispatch(ctx context.Context, peer types.NodeID, height int
107107
// Respond allows the underlying process which receives requests on the
108108
// requestCh to respond with the respective light block. A nil response is used to
109109
// represent that the receiver of the request does not have a light block at that height.
110-
func (d *Dispatcher) Respond(lb *tmproto.LightBlock, peer types.NodeID) error {
110+
func (d *Dispatcher) Respond(ctx context.Context, lb *tmproto.LightBlock, peer types.NodeID) error {
111111
d.mtx.Lock()
112112
defer d.mtx.Unlock()
113113

@@ -121,27 +121,37 @@ func (d *Dispatcher) Respond(lb *tmproto.LightBlock, peer types.NodeID) error {
121121
// If lb is nil we take that to mean that the peer didn't have the requested light
122122
// block and thus pass on the nil to the caller.
123123
if lb == nil {
124-
answerCh <- nil
125-
return nil
124+
select {
125+
case answerCh <- nil:
126+
return nil
127+
case <-ctx.Done():
128+
return ctx.Err()
129+
}
126130
}
127131

128132
block, err := types.LightBlockFromProto(lb)
129133
if err != nil {
130134
return err
131135
}
132136

133-
answerCh <- block
134-
return nil
137+
select {
138+
case <-ctx.Done():
139+
return ctx.Err()
140+
case answerCh <- block:
141+
return nil
142+
}
135143
}
136144

137145
// Close shuts down the dispatcher and cancels any pending calls awaiting responses.
138146
// Peers awaiting responses that have not arrived are delivered a nil block.
139147
func (d *Dispatcher) Close() {
140148
d.mtx.Lock()
141149
defer d.mtx.Unlock()
142-
for peer, call := range d.calls {
150+
for peer := range d.calls {
143151
delete(d.calls, peer)
144-
close(call)
152+
// don't close the channel here as it's closed in
153+
// other handlers, and would otherwise get garbage
154+
// collected.
145155
}
146156
}
147157

internal/statesync/dispatcher_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestDispatcherReturnsNoBlock(t *testing.T) {
8080

8181
go func() {
8282
<-chans.Out
83-
require.NoError(t, d.Respond(nil, peer))
83+
require.NoError(t, d.Respond(ctx, nil, peer))
8484
cancel()
8585
}()
8686

@@ -309,7 +309,7 @@ func handleRequests(ctx context.Context, t *testing.T, d *Dispatcher, ch chan p2
309309
peer := request.To
310310
resp := mockLBResp(ctx, t, peer, int64(height), time.Now())
311311
block, _ := resp.block.ToProto()
312-
require.NoError(t, d.Respond(block, resp.peer))
312+
require.NoError(t, d.Respond(ctx, block, resp.peer))
313313
case <-ctx.Done():
314314
return
315315
}

internal/statesync/reactor.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,10 @@ func (r *Reactor) handleLightBlockMessage(ctx context.Context, envelope *p2p.Env
740740
height = msg.LightBlock.SignedHeader.Header.Height
741741
}
742742
r.logger.Info("received light block response", "peer", envelope.From, "height", height)
743-
if err := r.dispatcher.Respond(msg.LightBlock, envelope.From); err != nil {
743+
if err := r.dispatcher.Respond(ctx, msg.LightBlock, envelope.From); err != nil {
744+
if errors.Is(err, context.Canceled) {
745+
return err
746+
}
744747
r.logger.Error("error processing light block response", "err", err, "height", height)
745748
}
746749

internal/statesync/reactor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func setup(
161161
}
162162
}
163163

164-
logger := log.NewTestingLogger(t)
164+
logger := log.NewNopLogger()
165165

166166
var err error
167167
rts.reactor, err = NewReactor(

0 commit comments

Comments
 (0)