Skip to content

add sigint and sigterm support for logs when follow flag is used #13132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions pkg/compose/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package compose
import (
"context"
"io"
"os"
"os/signal"
"syscall"

"github.com/containerd/errdefs"
"github.com/docker/docker/api/types/container"
Expand Down Expand Up @@ -71,6 +74,11 @@ func (s *composeService) Logs(
}

if options.Follow {
signalChan := make(chan os.Signal, 2)
defer close(signalChan)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(signalChan)

printer := newLogPrinter(consumer)

monitor := newMonitor(s.apiClient(), projectName)
Expand Down Expand Up @@ -106,6 +114,10 @@ func (s *composeService) Logs(
eg.Go(func() error {
return monitor.Start(ctx)
})
Comment on lines 114 to 116
Copy link
Contributor

@alessio-perugini alessio-perugini Aug 7, 2025

Choose a reason for hiding this comment

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

Suggested change
eg.Go(func() error {
return monitor.Start(ctx)
})
eg.Go(func() error {
return monitor.Start(ctx)
})

Sorry if I jump in, but I got bitten by the recent change done by #13062.
With the fix you propose in this PR I'm afraid we'll have a goroutine leak. I think the monitor.Start will keep running.

Considering only this codebase's use case for the Logs function, the leak won't be a big deal because you always quit the process. However, this will become a problem for other users who use this method (Logs) and do not want to terminate the process.

I think it would be great if we could pass to the monitor.Start/newMonitor something to make it go back the old behaviour (quitting the monitor upon context cancellation)

for {
<-signalChan
return nil
}
}

return eg.Wait()
Expand Down