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
This commit is contained in:
Jing Chen
2024-08-27 15:45:56 -07:00
committed by gVisor bot
parent 99745eb79e
commit c8cb440a5f
+1 -3
View File
@@ -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()