From 39232c4dde2536d986134a1b9c36e98dc7f14662 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Tue, 24 Jun 2025 12:46:53 -0700 Subject: [PATCH] service: add a start timeout to avoid blocking forever on healthchecks Signed-off-by: Andrea Luzzardi --- environment/environment.go | 4 +++- environment/service.go | 9 ++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/environment/environment.go b/environment/environment.go index 47078e76..7a8b91c9 100644 --- a/environment/environment.go +++ b/environment/environment.go @@ -257,10 +257,12 @@ func (env *Environment) RunBackground(ctx context.Context, explanation, command, } // Start the service + startCtx, cancel := context.WithTimeout(ctx, serviceStartTimeout) + defer cancel() svc, err := serviceState.AsService(dagger.ContainerAsServiceOpts{ Args: args, UseEntrypoint: useEntrypoint, - }).Start(ctx) + }).Start(startCtx) if err != nil { var exitErr *dagger.ExecError if errors.As(err, &exitErr) { diff --git a/environment/service.go b/environment/service.go index b148900e..f05e682c 100644 --- a/environment/service.go +++ b/environment/service.go @@ -4,10 +4,15 @@ import ( "context" "errors" "fmt" + "time" "dagger.io/dagger" ) +var ( + serviceStartTimeout = 30 * time.Second +) + type Service struct { Config *ServiceConfig `json:"config"` Endpoints EndpointMappings `json:"endpoints"` @@ -59,10 +64,12 @@ func (env *Environment) startService(ctx context.Context, cfg *ServiceConfig) (* } // Start the service + startCtx, cancel := context.WithTimeout(ctx, serviceStartTimeout) + defer cancel() svc, err := container.AsService(dagger.ContainerAsServiceOpts{ Args: args, UseEntrypoint: true, - }).Start(ctx) + }).Start(startCtx) if err != nil { var exitErr *dagger.ExecError if errors.As(err, &exitErr) {