{"id":"0aa7db9159cf3af3","repo":"gofiber/fiber","slug":"context-canceled-while-starting-service-s-w","errorCode":null,"errorMessage":"context canceled while starting service %s: %w","messagePattern":"context canceled while starting service (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"services.go","lineNumber":94,"sourceCode":"\treturn context.Background()\n}\n\n// startServices Handles the start process of services for the current application.\n// Iterates over all configured services and tries to start them, returning an error if any error occurs.\nfunc (app *App) startServices(ctx context.Context) error {\n\tif !app.hasConfiguredServices() {\n\t\treturn nil\n\t}\n\n\tvar errs []error\n\tfor idx, srv := range app.configured.Services {\n\t\tif srv == nil {\n\t\t\treturn fmt.Errorf(\"fiber: service at index %d is nil\", idx)\n\t\t}\n\t\tif err := ctx.Err(); err != nil {\n\t\t\t// Context is canceled, return an error the soonest possible, so that\n\t\t\t// the user can see the context cancellation error and act on it.\n\t\t\treturn fmt.Errorf(\"context canceled while starting service %s: %w\", srv.String(), err)\n\t\t}\n\n\t\terr := srv.Start(ctx)\n\t\tif err == nil {\n\t\t\t// mark the service as started\n\t\t\tapp.state.setService(srv)\n\t\t\tcontinue\n\t\t}\n\n\t\tif errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {\n\t\t\treturn fmt.Errorf(\"service %s start: %w\", srv.String(), err)\n\t\t}\n\n\t\terrs = append(errs, fmt.Errorf(\"service %s start: %w\", srv.String(), err))\n\t}\n\treturn errors.Join(errs...)\n}\n","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/services.go#L76-L112","documentation":"Returned by startServices (services.go:94) when the startup context is already canceled/deadline-exceeded just before a service's Start is about to be called. Fiber wraps ctx.Err() so the caller sees which service was pending and the exact context error (Canceled or DeadlineExceeded).","triggerScenarios":"Setting Config.ServicesStartupContextProvider to return a context tied to a shutting-down parent, or canceling the startup context (e.g. via SIGINT during boot) before all services have started. The per-iteration ctx.Err() check at services.go:91 catches it.","commonSituations":"Graceful shutdown signaling during slow boot (e.g. Kubernetes liveness probe failing and the orchestrator sending SIGTERM), a startup deadline that is too short for DB connection warmup, or a parent context canceled by an earlier failed dependency.","solutions":["Lengthen or remove the startup deadline in ServicesStartupContextProvider so services can finish initializing.","Defer cancel() of your startup context until after app.startServices completes.","Make Service.Start return faster (lazy-connect, background reconnect) so it fits within the deadline.","On shutdown, ensure the startup ctx is not canceled until Listen has fully started services."],"exampleFix":"// before: deadline too short\nStartupCtx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)\ncfg.ServicesStartupContextProvider = func() context.Context { return StartupCtx }\n\n// after: adequate deadline + only cancel on real shutdown\nStartupCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)\ncfg.ServicesStartupContextProvider = func() context.Context { return StartupCtx }\ndefer cancel()","handlingStrategy":"validation","validationCode":"startupCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)\ndefer cancel()\ncfg.ServicesStartupContextProvider = func() context.Context { return startupCtx }","typeGuard":null,"tryCatchPattern":"if err := app.startServices(startupCtx); err != nil {\n    if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {\n        // startup aborted — retry with a longer deadline or fail gracefully\n    }\n}","preventionTips":["Size the startup deadline to the slowest service connect time plus margin.","Do not cancel the startup context from the same path that calls Listen.","Separate startup context lifecycle from shutdown context."],"tags":["services","context","startup","cancellation","deadline"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}