Skip to content

Commit

Permalink
jsonrpc: Stop background rescans gracefully.
Browse files Browse the repository at this point in the history
Use the server waitgroup to ensure background rescans can return on
their own terms rather than being interrupted and potentially causing
panics.
  • Loading branch information
jholdstock committed Oct 12, 2024
1 parent 4adf892 commit 1707025
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions internal/rpc/jsonrpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -2032,9 +2032,14 @@ func (s *Server) importPrivKey(ctx context.Context, icmd any) (any, error) {
}

if rescan {
// TODO: This is not synchronized with process shutdown and
// will cause panics when the DB is closed mid-transaction.
go w.RescanFromHeight(context.Background(), n, scanFrom)
// Rescan in the background rather than blocking the rpc request. Use
// the server waitgroup to ensure the rescan can return cleanly rather
// than being killed mid database transaction.
s.wg.Add(1)
go func() {
defer s.wg.Done()
_ = w.RescanFromHeight(context.Background(), n, scanFrom)
}()
}

return nil, nil
Expand Down Expand Up @@ -2084,9 +2089,14 @@ func (s *Server) importPubKey(ctx context.Context, icmd any) (any, error) {
}

if rescan {
// TODO: This is not synchronized with process shutdown and
// will cause panics when the DB is closed mid-transaction.
go w.RescanFromHeight(context.Background(), n, scanFrom)
// Rescan in the background rather than blocking the rpc request. Use
// the server waitgroup to ensure the rescan can return cleanly rather
// than being killed mid database transaction.
s.wg.Add(1)
go func() {
defer s.wg.Done()
_ = w.RescanFromHeight(context.Background(), n, scanFrom)
}()
}

return nil, nil
Expand Down Expand Up @@ -2130,9 +2140,14 @@ func (s *Server) importScript(ctx context.Context, icmd any) (any, error) {
}

if rescan {
// TODO: This is not synchronized with process shutdown and
// will cause panics when the DB is closed mid-transaction.
go w.RescanFromHeight(context.Background(), n, scanFrom)
// Rescan in the background rather than blocking the rpc request. Use
// the server waitgroup to ensure the rescan can return cleanly rather
// than being killed mid database transaction.
s.wg.Add(1)
go func() {
defer s.wg.Done()
_ = w.RescanFromHeight(context.Background(), n, scanFrom)
}()
}

return nil, nil
Expand Down

0 comments on commit 1707025

Please sign in to comment.