@@ -17,7 +17,7 @@ limitations under the License.
1717package main
1818
1919import (
20- "errors "
20+ "context "
2121 "fmt"
2222 "sync"
2323 "time"
@@ -102,26 +102,20 @@ type npdService struct {
102102}
103103
104104func (s * npdService ) Execute (args []string , r <- chan svc.ChangeRequest , changes chan <- svc.Status ) (bool , uint32 ) {
105- appTermCh := make (chan error , 1 )
106- svcLoopTermCh := make (chan error , 1 )
107- defer func () {
108- close (appTermCh )
109- close (svcLoopTermCh )
110- }()
111-
112105 changes <- svc.Status {State : svc .StartPending }
113106 changes <- svc.Status {State : svc .Running , Accepts : svcCommandsAccepted }
114107 var appWG sync.WaitGroup
115108 var svcWG sync.WaitGroup
116109
117110 options := s .options
111+ ctx , cancelFunc := context .WithCancel (context .Background ())
118112
119113 // NPD application goroutine.
120114 appWG .Add (1 )
121115 go func () {
122116 defer appWG .Done ()
123117
124- if err := npdMain (options , appTermCh ); err != nil {
118+ if err := npdMain (ctx , options ); err != nil {
125119 elog .Warning (windowsEventLogID , err .Error ())
126120 }
127121
@@ -132,16 +126,36 @@ func (s *npdService) Execute(args []string, r <-chan svc.ChangeRequest, changes
132126 svcWG .Add (1 )
133127 go func () {
134128 defer svcWG .Done ()
135-
136- serviceLoop (r , changes , appTermCh , svcLoopTermCh )
129+ for {
130+ select {
131+ case <- ctx .Done ():
132+ return
133+ case c := <- r :
134+ switch c .Cmd {
135+ case svc .Interrogate :
136+ changes <- c .CurrentStatus
137+ // Testing deadlock from https://code.google.com/p/winsvc/issues/detail?id=4
138+ time .Sleep (100 * time .Millisecond )
139+ changes <- c .CurrentStatus
140+ case svc .Stop , svc .Shutdown :
141+ elog .Info (windowsEventLogID , fmt .Sprintf ("Stopping %s service, %v" , svcName , c .Context ))
142+ cancelFunc ()
143+ case svc .Pause :
144+ elog .Info (windowsEventLogID , "ignoring pause command from Windows service control, not supported" )
145+ changes <- svc.Status {State : svc .Paused , Accepts : svcCommandsAccepted }
146+ case svc .Continue :
147+ elog .Info (windowsEventLogID , "ignoring continue command from Windows service control, not supported" )
148+ changes <- svc.Status {State : svc .Running , Accepts : svcCommandsAccepted }
149+ default :
150+ elog .Error (windowsEventLogID , fmt .Sprintf ("unexpected control request #%d" , c ))
151+ }
152+ }
153+ }
137154 }()
138155
139156 // Wait for the application go routine to die.
140157 appWG .Wait ()
141158
142- // Ensure that the service control loop is killed.
143- svcLoopTermCh <- nil
144-
145159 // Wait for the service control loop to terminate.
146160 // Otherwise it's possible that the channel closures cause the application to panic.
147161 svcWG .Wait ()
@@ -151,31 +165,3 @@ func (s *npdService) Execute(args []string, r <-chan svc.ChangeRequest, changes
151165
152166 return false , uint32 (0 )
153167}
154-
155- func serviceLoop (r <- chan svc.ChangeRequest , changes chan <- svc.Status , appTermCh chan error , svcLoopTermCh chan error ) {
156- for {
157- select {
158- case <- svcLoopTermCh :
159- return
160- case c := <- r :
161- switch c .Cmd {
162- case svc .Interrogate :
163- changes <- c .CurrentStatus
164- // Testing deadlock from https://code.google.com/p/winsvc/issues/detail?id=4
165- time .Sleep (100 * time .Millisecond )
166- changes <- c .CurrentStatus
167- case svc .Stop , svc .Shutdown :
168- elog .Info (windowsEventLogID , fmt .Sprintf ("Stopping %s service, %v" , svcName , c .Context ))
169- appTermCh <- errors .New ("stopping service" )
170- case svc .Pause :
171- elog .Info (windowsEventLogID , "ignoring pause command from Windows service control, not supported" )
172- changes <- svc.Status {State : svc .Paused , Accepts : svcCommandsAccepted }
173- case svc .Continue :
174- elog .Info (windowsEventLogID , "ignoring continue command from Windows service control, not supported" )
175- changes <- svc.Status {State : svc .Running , Accepts : svcCommandsAccepted }
176- default :
177- elog .Error (windowsEventLogID , fmt .Sprintf ("unexpected control request #%d" , c ))
178- }
179- }
180- }
181- }
0 commit comments