From c8cb440a5f77b2256810e7488cd629f826d5ec29 Mon Sep 17 00:00:00 2001 From: Jing Chen Date: Tue, 27 Aug 2024 15:42:24 -0700 Subject: [PATCH] Update how runsc's port-forward command is terminated. It currently expects all 3 events to happen to exit the command, the container is killed, local port forward is compeleted, a termination signal is caught. One can only terminate the command expecting both termination signal (e.g. ctrl + c) and the container being killed. Instead, the ideal behavior is that the command exits on either of previous 2 events. The local port forward goroutine is blocked until the context's cancel function is called. Thus, we just have the waitgroup to wait for the completion of the local port forward task. PiperOrigin-RevId: 668168948 --- runsc/cmd/portforward.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/runsc/cmd/portforward.go b/runsc/cmd/portforward.go index 0c1c14644..1626c7bfb 100644 --- a/runsc/cmd/portforward.go +++ b/runsc/cmd/portforward.go @@ -128,7 +128,7 @@ func (p *PortForward) Execute(ctx context.Context, f *flag.FlagSet, args ...any) // Start port forwarding with the local port. var wg sync.WaitGroup ctx, cancel := context.WithCancel(ctx) - wg.Add(3) + wg.Add(1) go func(localPort, portNum int) { defer cancel() defer wg.Done() @@ -141,7 +141,6 @@ func (p *PortForward) Execute(ctx context.Context, f *flag.FlagSet, args ...any) // Exit port forwarding if the container exits. go func() { - defer wg.Done() // Cancel port forwarding after Wait returns regardless of return // value as err may indicate sandbox has terminated already. _, _ = c.Wait() @@ -151,7 +150,6 @@ func (p *PortForward) Execute(ctx context.Context, f *flag.FlagSet, args ...any) // Wait for ^C from the user. go func() { - defer wg.Done() sig := waitSignal() fmt.Printf("Got %v, Exiting...\n", sig) cancel()