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

Shutdown #3234

Merged
merged 2 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

# 4.47.1
- Linux: fix support for Wayland
- Linux: release device upon app close, enabling other apps to connect to the BitBox after the BitBoxApp closes

# 4.47.0
- Bundle BitBox02 firmware version v9.22.0
Expand Down
9 changes: 7 additions & 2 deletions backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -898,12 +898,17 @@ func (backend *Backend) Environment() Environment {

// Close shuts down the backend. After this, no other method should be called.
func (backend *Backend) Close() error {
backend.ratesUpdater.Stop()
// Call this without `accountsAndKeystoreLock` as it eventually calls `DeregisterKeystore()`,
// which acquires the same lock.
if backend.usbManager != nil {
backend.usbManager.Close()
}

defer backend.accountsAndKeystoreLock.Lock()()

errors := []string{}

backend.ratesUpdater.Stop()

backend.uninitAccounts(true)

for _, coin := range backend.coins {
Expand Down
3 changes: 2 additions & 1 deletion backend/bridgecommon/bridgecommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,9 @@ func Shutdown() {

log := logging.Get().WithGroup("server")
if globalShutdown != nil {
log.Info("Shutdown about to be called")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: is this still needed, or is it a debug leftover?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it's a helpful line anyway in case Shutdown gets stuck for whatever reason

globalShutdown()
log.Info("Shutdown called")
log.Info("Shutdown finished")
} else {
log.Info("Shutdown called, but backend not running")
}
Expand Down
22 changes: 22 additions & 0 deletions backend/devices/usb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type Manager struct {
onUnregister func(string)

updateCh chan struct{}
quitCh chan struct{}

socksProxy socksproxy.SocksProxy

Expand Down Expand Up @@ -118,6 +119,7 @@ func NewManager(
onRegister: onRegister,
onUnregister: onUnregister,
updateCh: make(chan struct{}),
quitCh: make(chan struct{}),
socksProxy: socksProxy,

log: logging.Get().WithGroup("manager"),
Expand Down Expand Up @@ -272,6 +274,14 @@ func (manager *Manager) Update() {
func (manager *Manager) listen() {
for {
select {
case <-manager.quitCh:
return
default:
}

select {
case <-manager.quitCh:
return
case <-manager.updateCh:
case <-time.After(time.Second):
}
Expand Down Expand Up @@ -336,3 +346,15 @@ func (manager *Manager) Start() {
go manager.listen()
manager.Update()
}

// Close closes and unregisters all devices.
func (manager *Manager) Close() {
manager.log.Info("Closing devices manager")
close(manager.quitCh)
for deviceID, device := range manager.devices {
manager.log.WithField("device-id", deviceID).Info("manager: closing device")
device.Close()
delete(manager.devices, deviceID)
manager.onUnregister(deviceID)
}
}
2 changes: 1 addition & 1 deletion frontends/qt/libserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extern void handleURI(cchar_t* uri);
extern void serve(cppHeapFree cppHeapFreeFn, pushNotificationsCallback pushNotificationsFn, responseCallback responseFn, notifyUserCallback notifyUserFn, cchar_t* preferredLocale, getSaveFilenameCallback getSaveFilenameFn);
extern void systemOpen(cchar_t* url);
extern void goLog(cchar_t* msg);

extern void backendShutdown();
#ifdef __cplusplus
}
#endif
1 change: 1 addition & 0 deletions frontends/qt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ int main(int argc, char *argv[])
delete view;
view = nullptr;
webClassMutex.unlock();
backendShutdown();
});

#if defined(_WIN32)
Expand Down
5 changes: 5 additions & 0 deletions frontends/qt/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ func goLog(msg *C.cchar_t) {
logging.Get().WithGroup("qt-frontend").Info(goMsg)
}

//export backendShutdown
func backendShutdown() {
bridgecommon.Shutdown()
}

func authResult(ok bool) {
bridgecommon.AuthResult(ok)
}
Expand Down
Loading