-
-
Notifications
You must be signed in to change notification settings - Fork 973
Stop sleep monitor on user requested disconnect and restart on connect #4922
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -123,7 +123,7 @@ | |
|
|
||
| client.setupSignalHandler(client.ctx) | ||
|
|
||
| client.setDefaultFonts() | ||
| systray.Run(client.onTrayReady, client.onTrayExit) | ||
| } | ||
|
|
||
|
|
@@ -320,6 +320,9 @@ | |
| logFile string | ||
| wLoginURL fyne.Window | ||
|
|
||
| sleepService *sleep.Service | ||
| sleepLock sync.Mutex | ||
|
|
||
| connectCancel context.CancelFunc | ||
| } | ||
|
|
||
|
|
@@ -1165,6 +1168,14 @@ | |
|
|
||
| // startSleepListener initializes the sleep detection service and listens for sleep events | ||
| func (s *serviceClient) startSleepListener() { | ||
| s.sleepLock.Lock() | ||
| defer s.sleepLock.Unlock() | ||
|
|
||
| if s.sleepService != nil { | ||
| log.Debug("sleep detection service already initialized") | ||
| return | ||
| } | ||
|
|
||
| sleepService, err := sleep.New() | ||
| if err != nil { | ||
| log.Warnf("%v", err) | ||
|
|
@@ -1176,18 +1187,32 @@ | |
| return | ||
| } | ||
|
|
||
| s.sleepService = sleepService | ||
|
||
| log.Info("sleep detection service initialized") | ||
|
|
||
| // Cleanup on context cancellation | ||
| go func() { | ||
| <-s.ctx.Done() | ||
| log.Info("stopping sleep event listener") | ||
| if err := sleepService.Deregister(); err != nil { | ||
| log.Errorf("failed to deregister sleep detection: %v", err) | ||
| } | ||
| s.stopSleepListener() | ||
| }() | ||
| } | ||
|
|
||
| // stopSleepListener stops the sleep detection service | ||
| func (s *serviceClient) stopSleepListener() { | ||
| s.sleepLock.Lock() | ||
| defer s.sleepLock.Unlock() | ||
|
|
||
| if s.sleepService == nil { | ||
| return | ||
| } | ||
|
|
||
| log.Info("stopping sleep event listener") | ||
| if err := s.sleepService.Deregister(); err != nil { | ||
| log.Errorf("failed to deregister sleep detection: %v", err) | ||
| } | ||
| s.sleepService = nil | ||
| } | ||
|
|
||
| // handleSleepEvents sends a sleep notification to the daemon via gRPC | ||
| func (s *serviceClient) handleSleepEvents(event sleep.EventType) { | ||
| conn, err := s.getSrvClient(0) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The early return check for existing sleep service occurs after acquiring the lock but doesn't prevent a potential race condition. If
startSleepListener()is called multiple times concurrently, both calls could pass the nil check before either assigns tos.sleepService. Consider checking ifs.sleepService != nilagain after the sleep service is successfully created but before assignment, or document that this method should only be called from the event handler which is already serialized.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot open a new pull request to apply changes based on this feedback
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot open a new pull request to apply changes based on this feedback