{"record":{"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/a105acad6c1e4576a77f01e02973f67e962bb58d/services.go#L76-L112","documentation":"Returned by startServices() when ctx.Err() is non-nil at the top of an iteration — the startup context has been canceled or expired before this service's turn. Fiber returns immediately so the user sees the cancellation cause rather than attempting a doomed Start.","triggerScenarios":"Config.ServicesStartupContextProvider returns a context that is canceled (e.g., tied to an OS signal) or has a deadline that expires during startup, and a later service in the slice hasn't been started yet when the cancellation lands.","commonSituations":"SIGINT/SIGTERM arrives mid-startup while long-running services (DB pool warmup, cache priming) are still starting; a startup deadline configured too tight for the service set; graceful shutdown triggered before all services initialized.","solutions":["Review ServicesStartupContextProvider — return a context with a deadline generous enough for the slowest service.","Order services so lightweight ones start first and dependencies precede dependents.","Make service.Start() honor ctx and fail fast, so cancellation propagates cleanly instead of blocking.","If startup is routinely interrupted, investigate why shutdown is being requested before readiness."],"exampleFix":"// before — startup context cancels on any signal immediately\napp := fiber.New(fiber.Config{\n    Services: []fiber.Service{slowCacheSvc, dbSvc},\n    ServicesStartupContextProvider: func() context.Context {\n        ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)\n        _ = stop\n        return ctx\n    },\n})\n// interrupt during slowCacheSvc.Start -> context canceled while starting service db\n\n// after — generous deadline, ordered services\napp := fiber.New(fiber.Config{\n    Services: []fiber.Service{dbSvc, slowCacheSvc},\n    ServicesStartupContextProvider: func() context.Context {\n        ctx, _ := context.WithTimeout(context.Background(), 60*time.Second)\n        return ctx\n    },\n})","handlingStrategy":"validation","validationCode":"// Provide a startup context with a sane deadline.\napp := fiber.New(fiber.Config{\n    Services: svcs,\n    ServicesStartupContextProvider: func() context.Context {\n        ctx, _ := context.WithTimeout(context.Background(), startupBudget(svcs))\n        return ctx\n    },\n})","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Size the startup deadline to the slowest service's connect time plus margin.","Order services dependencies-first so cancellation has a clear culprit.","Make Start() honor ctx and fail fast."],"tags":["services","startup","context","cancellation","shutdown"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}