fix: resolve deadlock in Restart method
The Restart() method was acquiring a lock then calling Stop() which tried to acquire the same lock, causing a deadlock. Fixed by creating stopLocked() method that assumes lock is already held, and having Restart() call stopLocked() directly.
This commit is contained in:
parent
998c49e52d
commit
1b75778658
1 changed files with 7 additions and 2 deletions
|
|
@ -190,7 +190,12 @@ func (s *Service) Start() error {
|
||||||
func (s *Service) Stop() error {
|
func (s *Service) Stop() error {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
return s.stopLocked()
|
||||||
|
}
|
||||||
|
|
||||||
|
// stopLocked stops the running gateway daemon without acquiring the lock.
|
||||||
|
// Must be called with the lock already held.
|
||||||
|
func (s *Service) stopLocked() error {
|
||||||
logger.InfoC("daemon", "Stopping gateway daemon")
|
logger.InfoC("daemon", "Stopping gateway daemon")
|
||||||
|
|
||||||
pid := s.pidFile.Read()
|
pid := s.pidFile.Read()
|
||||||
|
|
@ -254,9 +259,9 @@ func (s *Service) Restart() error {
|
||||||
|
|
||||||
logger.InfoC("daemon", "Restarting gateway daemon")
|
logger.InfoC("daemon", "Restarting gateway daemon")
|
||||||
|
|
||||||
// Stop if running
|
// Stop if running (call stopLocked to avoid deadlock)
|
||||||
if s.pidFile.IsProcessRunning() {
|
if s.pidFile.IsProcessRunning() {
|
||||||
if err := s.Stop(); err != nil {
|
if err := s.stopLocked(); err != nil {
|
||||||
return fmt.Errorf("failed to stop daemon: %w", err)
|
return fmt.Errorf("failed to stop daemon: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue