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

jsonrpc: Stop background (more) rescans gracefully #2434

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
jsonrpc: Stop background rescans (more) gracefully
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.
jholdstock committed Oct 14, 2024
commit 05105ec9baee7a2fcf794ad8386791ea2ba1a742
33 changes: 24 additions & 9 deletions internal/rpc/jsonrpc/methods.go
Original file line number Diff line number Diff line change
@@ -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
@@ -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
@@ -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