Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: chain: cancel long operations upon ctx cancelation #6144

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/submodule/chain/chaininfo_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ func (cia *chainInfoAPI) ChainGetPath(ctx context.Context, from types.TipSetKey,
return nil, fmt.Errorf("loading to tipset %s: %w", to, err)
}

revert, apply, err := chain.ReorgOps(cia.chain.ChainReader.GetTipSet, fts, tts)
revert, apply, err := chain.ReorgOps(ctx, cia.chain.ChainReader.GetTipSet, fts, tts)
if err != nil {
return nil, fmt.Errorf("error getting tipset branches: %w", err)
}
Expand Down
31 changes: 26 additions & 5 deletions pkg/chain/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,15 @@ func (store *Store) getCirculatingSupply(ctx context.Context, height abi.ChainEp
circ := big.Zero()
unCirc := big.Zero()
err := st.ForEach(func(a address.Address, actor *types.Actor) error {
// this can be a lengthy operation, we need to cancel early when
// the context is cancelled to avoid resource exhaustion
select {
case <-ctx.Done():
// this will cause ForEach to return
return ctx.Err()
default:
}

switch {
case actor.Balance.IsZero():
// Do nothing for zero-balance actors
Expand Down Expand Up @@ -1239,8 +1248,8 @@ func (store *Store) Stop() {

// ReorgOps used to reorganize the blockchain. Whenever a new tipset is approved,
// the new tipset compared with the local tipset to obtain which tipset need to be revert and which tipsets are applied
func (store *Store) ReorgOps(a, b *types.TipSet) ([]*types.TipSet, []*types.TipSet, error) {
return ReorgOps(store.GetTipSet, a, b)
func (store *Store) ReorgOps(ctx context.Context, a, b *types.TipSet) ([]*types.TipSet, []*types.TipSet, error) {
return ReorgOps(ctx, store.GetTipSet, a, b)
}

// ReorgOps takes two tipsets (which can be at different heights), and walks
Expand All @@ -1252,23 +1261,35 @@ func (store *Store) ReorgOps(a, b *types.TipSet) ([]*types.TipSet, []*types.TipS
//
// If an error happens along the way, we return the error with nil slices.
// todo should move this code into store.ReorgOps. anywhere use this function should invoke store.ReorgOps
func ReorgOps(lts func(context.Context, types.TipSetKey) (*types.TipSet, error), a, b *types.TipSet) ([]*types.TipSet, []*types.TipSet, error) {
func ReorgOps(ctx context.Context,
lts func(ctx context.Context, _ types.TipSetKey) (*types.TipSet, error),
a, b *types.TipSet,
) ([]*types.TipSet, []*types.TipSet, error) {
left := a
right := b

var leftChain, rightChain []*types.TipSet
for !left.Equals(right) {
// this can take a long time and lot of memory if the tipsets are far apart
// since it can be reached through remote calls, we need to
// cancel early when possible to prevent resource exhaustion.
select {
case <-ctx.Done():
return nil, nil, ctx.Err()
default:
}

if left.Height() > right.Height() {
leftChain = append(leftChain, left)
par, err := lts(context.TODO(), left.Parents())
par, err := lts(ctx, left.Parents())
if err != nil {
return nil, nil, err
}

left = par
} else {
rightChain = append(rightChain, right)
par, err := lts(context.TODO(), right.Parents())
par, err := lts(ctx, right.Parents())
if err != nil {
log.Infof("failed to fetch right.Parents: %s", err)
return nil, nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (fcs *fakeCS) ChainGetPath(ctx context.Context, from, to types.TipSetKey) (
}

// copied from the chainstore
revert, apply, err := chain.ReorgOps(func(ctx context.Context, tsk types.TipSetKey) (*types.TipSet, error) {
revert, apply, err := chain.ReorgOps(ctx, func(ctx context.Context, tsk types.TipSetKey) (*types.TipSet, error) {
return fcs.ChainGetTipSet(ctx, tsk)
}, fromTS, toTS)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/messagepool/messagepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -1523,7 +1523,7 @@ func (mp *MessagePool) runHeadChange(ctx context.Context, from *types.TipSet, to
}
}

revert, apply, err := chain.ReorgOps(mp.api.LoadTipSet, from, to)
revert, apply, err := chain.ReorgOps(ctx, mp.api.LoadTipSet, from, to)
if err != nil {
return fmt.Errorf("failed to compute reorg ops for mpool pending messages: %v", err)
}
Expand Down
Loading