Skip to content

Commit

Permalink
fix: ensure Close can be called multiple times safely
Browse files Browse the repository at this point in the history
  • Loading branch information
jvillafanez committed May 21, 2024
1 parent 14b2554 commit e6352b6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
5 changes: 3 additions & 2 deletions services/antivirus/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ EventLoop:
}

func (av Antivirus) Close() {
av.stopped.Store(true)
close(av.stopCh)
if av.stopped.CompareAndSwap(false, true) {
close(av.stopCh)
}
}

func (av Antivirus) processEvent(e events.Event, s events.Publisher) error {
Expand Down
5 changes: 3 additions & 2 deletions services/clientlog/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ EventLoop:
}

func (cl *ClientlogService) Close() {
cl.stopped.Store(true)
close(cl.stopCh)
if cl.stopped.CompareAndSwap(false, true) {
close(cl.stopCh)
}
}

func (cl *ClientlogService) processEvent(event events.Event) {
Expand Down
5 changes: 3 additions & 2 deletions services/notifications/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ EventLoop:
}

func (s eventsNotifier) Close() {
s.stopped.Store(true)
close(s.stopCh)
if s.stopped.CompareAndSwap(false, true) {
close(s.stopCh)
}
}

func (s eventsNotifier) render(ctx context.Context, template email.MessageTemplate,
Expand Down
5 changes: 3 additions & 2 deletions services/policies/pkg/service/event/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ EventLoop:
// from, so resources won't be freed and there will be memory leaks. For now,
// if the service is stopped, you should close the app soon after.
func (s Service) Close() {
s.stopped.Store(true)
close(s.stopCh)
if s.stopped.CompareAndSwap(false, true) {
close(s.stopCh)
}
}

func (s Service) processEvent(e events.Event) error {
Expand Down
5 changes: 3 additions & 2 deletions services/postprocessing/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ EventLoop:
// from, so resources won't be freed and there will be memory leaks. For now,
// if the service is stopped, you should close the app soon after.
func (pps *PostprocessingService) Close() {
pps.stopped.Store(true)
close(pps.stopCh)
if pps.stopped.CompareAndSwap(false, true) {
close(pps.stopCh)
}
}

func (pps *PostprocessingService) processEvent(e events.Event) error {
Expand Down

0 comments on commit e6352b6

Please sign in to comment.