Skip to content

Commit

Permalink
fix: do not allow lnclient access while it is shutting down (#605)
Browse files Browse the repository at this point in the history
* fix: do not allow lnclient access while it is shutting down

* fix: also lock the stop endpoint to avoid restart while lnclient is stopping

* chore: fix comment copy
  • Loading branch information
rolznz authored Sep 4, 2024
1 parent da27ed2 commit a4fb2fa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 8 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,12 @@ func (api *api) ChangeUnlockPassword(changeUnlockPasswordRequest *ChangeUnlockPa
}

func (api *api) Stop() error {
if !startMutex.TryLock() {
// do not allow to stop twice in case this is somehow called twice
return errors.New("app is busy")
}
defer startMutex.Unlock()

logger.Logger.Info("Running Stop command")
if api.svc.GetLNClient() == nil {
return errors.New("LNClient not started")
Expand Down Expand Up @@ -719,7 +725,7 @@ func (api *api) Start(startRequest *StartRequest) {
func (api *api) StartInternal(startRequest *StartRequest) (err error) {
if !startMutex.TryLock() {
// do not allow to start twice in case this is somehow called twice
return errors.New("app is already starting")
return errors.New("app is busy")
}
defer startMutex.Unlock()
return api.svc.StartApp(startRequest.UnlockPassword)
Expand All @@ -728,7 +734,7 @@ func (api *api) StartInternal(startRequest *StartRequest) (err error) {
func (api *api) Setup(ctx context.Context, setupRequest *SetupRequest) error {
if !startMutex.TryLock() {
// do not allow to start twice in case this is somehow called twice
return errors.New("app is already starting")
return errors.New("app is busy")
}
defer startMutex.Unlock()
info, err := api.GetInfo(ctx)
Expand Down
6 changes: 4 additions & 2 deletions service/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ func (svc *service) stopLNClient() {
if svc.lnClient == nil {
return
}
lnClient := svc.lnClient
svc.lnClient = nil

logger.Logger.Info("Shutting down LN client")
err := svc.lnClient.Shutdown()
err := lnClient.Shutdown()
if err != nil {
logger.Logger.WithError(err).Error("Failed to stop LN client")
svc.eventPublisher.Publish(&events.Event{
Expand All @@ -34,7 +37,6 @@ func (svc *service) stopLNClient() {
return
}
logger.Logger.Info("Publishing node shutdown event")
svc.lnClient = nil
svc.eventPublisher.Publish(&events.Event{
Event: "nwc_node_stopped",
})
Expand Down

0 comments on commit a4fb2fa

Please sign in to comment.