@@ -320,6 +320,9 @@ type serviceClient struct {
320320 logFile string
321321 wLoginURL fyne.Window
322322
323+ sleepService * sleep.Service
324+ sleepLock sync.Mutex
325+
323326 connectCancel context.CancelFunc
324327}
325328
@@ -1165,6 +1168,14 @@ func (s *serviceClient) getSrvClient(timeout time.Duration) (proto.DaemonService
11651168
11661169// startSleepListener initializes the sleep detection service and listens for sleep events
11671170func (s * serviceClient ) startSleepListener () {
1171+ s .sleepLock .Lock ()
1172+ defer s .sleepLock .Unlock ()
1173+
1174+ if s .sleepService != nil {
1175+ log .Debug ("sleep detection service already initialized" )
1176+ return
1177+ }
1178+
11681179 sleepService , err := sleep .New ()
11691180 if err != nil {
11701181 log .Warnf ("%v" , err )
@@ -1176,18 +1187,32 @@ func (s *serviceClient) startSleepListener() {
11761187 return
11771188 }
11781189
1190+ s .sleepService = sleepService
11791191 log .Info ("sleep detection service initialized" )
11801192
11811193 // Cleanup on context cancellation
11821194 go func () {
11831195 <- s .ctx .Done ()
1184- log .Info ("stopping sleep event listener" )
1185- if err := sleepService .Deregister (); err != nil {
1186- log .Errorf ("failed to deregister sleep detection: %v" , err )
1187- }
1196+ s .stopSleepListener ()
11881197 }()
11891198}
11901199
1200+ // stopSleepListener stops the sleep detection service
1201+ func (s * serviceClient ) stopSleepListener () {
1202+ s .sleepLock .Lock ()
1203+ defer s .sleepLock .Unlock ()
1204+
1205+ if s .sleepService == nil {
1206+ return
1207+ }
1208+
1209+ log .Info ("stopping sleep event listener" )
1210+ if err := s .sleepService .Deregister (); err != nil {
1211+ log .Errorf ("failed to deregister sleep detection: %v" , err )
1212+ }
1213+ s .sleepService = nil
1214+ }
1215+
11911216// handleSleepEvents sends a sleep notification to the daemon via gRPC
11921217func (s * serviceClient ) handleSleepEvents (event sleep.EventType ) {
11931218 conn , err := s .getSrvClient (0 )
0 commit comments