@@ -216,6 +216,24 @@ func drainChan(c chan pipeline.Event) {
216216 }
217217}
218218
219+ func unregisterWatcher (ctx context.Context , cConfig * csconfig.Config ) (bool , error ) {
220+ if cConfig .API == nil || cConfig .API .Client == nil || ! cConfig .API .Client .UnregisterOnExit {
221+ return false , nil
222+ }
223+
224+ lapiClient , err := apiclient .GetLAPIClient ()
225+ if err != nil {
226+ return false , err
227+ }
228+
229+ _ , err = lapiClient .Auth .UnregisterWatcher (ctx )
230+ if err != nil {
231+ return false , err
232+ }
233+
234+ return true , nil
235+ }
236+
219237func HandleSignals (ctx context.Context , cConfig * csconfig.Config ) error {
220238 var (
221239 newConfig * csconfig.Config
@@ -240,7 +258,6 @@ func HandleSignals(ctx context.Context, cConfig *csconfig.Config) error {
240258 go func () {
241259 defer trace .CatchPanic ("crowdsec/HandleSignals" )
242260
243- Loop:
244261 for {
245262 s := <- signalChan
246263 switch s {
@@ -250,14 +267,12 @@ func HandleSignals(ctx context.Context, cConfig *csconfig.Config) error {
250267
251268 if err = shutdown (s , cConfig ); err != nil {
252269 exitChan <- fmt .Errorf ("failed shutdown: %w" , err )
253-
254- break Loop
270+ return
255271 }
256272
257273 if newConfig , err = reloadHandler (ctx , s ); err != nil {
258274 exitChan <- fmt .Errorf ("reload handler failure: %w" , err )
259-
260- break Loop
275+ return
261276 }
262277
263278 if newConfig != nil {
@@ -269,8 +284,7 @@ func HandleSignals(ctx context.Context, cConfig *csconfig.Config) error {
269284
270285 if err = shutdown (s , cConfig ); err != nil {
271286 exitChan <- fmt .Errorf ("failed shutdown: %w" , err )
272-
273- break Loop
287+ return
274288 }
275289
276290 exitChan <- nil
@@ -283,20 +297,11 @@ func HandleSignals(ctx context.Context, cConfig *csconfig.Config) error {
283297 log .Warning ("Crowdsec service shutting down" )
284298 }
285299
286- if cConfig .API != nil && cConfig .API .Client != nil && cConfig .API .Client .UnregisterOnExit {
287- log .Warning ("Unregistering watcher" )
288-
289- lapiClient , err := apiclient .GetLAPIClient ()
290- if err != nil {
291- return err
292- }
293-
294- _ , err = lapiClient .Auth .UnregisterWatcher (ctx )
295- if err != nil {
296- return fmt .Errorf ("failed to unregister watcher: %w" , err )
300+ if ok , werr := unregisterWatcher (ctx , cConfig ); werr != nil {
301+ log .WithError (werr ).Warning ("unregistering watcher" )
302+ if ok {
303+ log .Warning ("Watcher unregistered" )
297304 }
298-
299- log .Warning ("Watcher unregistered" )
300305 }
301306
302307 return err
0 commit comments