Skip to content

Commit f28e1c4

Browse files
authored
Add timeout to container stop operations (#121)
1 parent a3ed555 commit f28e1c4

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

internal/container/stop.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,36 @@ package container
33
import (
44
"context"
55
"fmt"
6+
"time"
67

78
"github.com/localstack/lstk/internal/config"
89
"github.com/localstack/lstk/internal/output"
910
"github.com/localstack/lstk/internal/runtime"
1011
)
1112

1213
func Stop(ctx context.Context, rt runtime.Runtime, sink output.Sink, containers []config.ContainerConfig) error {
14+
const stopTimeout = 30 * time.Second
15+
1316
for _, c := range containers {
1417
name := c.Name()
15-
running, err := rt.IsRunning(ctx, name)
18+
19+
checkCtx, checkCancel := context.WithTimeout(ctx, 5*time.Second)
20+
running, err := rt.IsRunning(checkCtx, name)
21+
checkCancel()
1622
if err != nil {
1723
return fmt.Errorf("checking %s running: %w", name, err)
1824
}
1925
if !running {
2026
return fmt.Errorf("LocalStack is not running")
2127
}
2228
output.EmitSpinnerStart(sink, "Stopping LocalStack...")
23-
if err := rt.Stop(ctx, name); err != nil {
29+
stopCtx, stopCancel := context.WithTimeout(ctx, stopTimeout)
30+
if err := rt.Stop(stopCtx, name); err != nil {
31+
stopCancel()
2432
output.EmitSpinnerStop(sink)
2533
return fmt.Errorf("failed to stop LocalStack: %w", err)
2634
}
35+
stopCancel()
2736
output.EmitSpinnerStop(sink)
2837
output.EmitSuccess(sink, "LocalStack stopped")
2938
}

0 commit comments

Comments
 (0)