Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions pkg/event_handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,23 @@ func Start(ctx context.Context, stop context.CancelFunc, cfg *conf.GlobalConfig,
Notifier: notifier,
}
go func() {
for event := range watcher.ResultChan() {
err2 := handler.Handle(ctx, &event)
if err2 != nil {
log.Printf("[event handler] failed to Handle workflow event %s", err2) // ERROR

for {
select {
case <-ctx.Done():
log.Print("[event handler] context canceled, exiting")
watcher.Stop()
return
case event, ok := <-watcher.ResultChan():
if !ok {
log.Print("[event handler] result channel closed")
stop()
return
}
if err2 := handler.Handle(ctx, &event); err2 != nil {
log.Printf("[event handler] failed to handle workflow event: %v", err2)
}
}
}
log.Print("[event handler] stopped work, closing watcher")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoshaDo can explain how the race condition could happen ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If stop() is called, it will cancel the context (ctx). If the context is already canceled before watcher.Stop() completes, there could be race conditions or unexpected behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok there are no race condition, but it is a good practice to stop the process if the watcher Chanel closed.

watcher.Stop()
stop()
}()
}
Loading