Skip to content

Commit

Permalink
jsonrpc: Stop background rescans (more) 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 forcefully terminated early.

This is still not an ideal situation because in the case where the
server/wallet is being shut down, RescanFromHeight will return with an
"rpc connection closed" error (or similar) rather than returning because
its provided context was cancelled. However, that is a notable
improvement over the existing situation which could result in an
unhandled panic.
  • Loading branch information
jholdstock committed Oct 14, 2024
1 parent 4adf892 commit 1e85686
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 1e85686

Please sign in to comment.