gofiber/fiber · error

context canceled while starting service

Error message

context canceled while starting service %s: %w

What it means

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.

Solutions

  1. Review ServicesStartupContextProvider — return a context with a deadline generous enough for the slowest service.
  2. Order services so lightweight ones start first and dependencies precede dependents.
  3. Make service.Start() honor ctx and fail fast, so cancellation propagates cleanly instead of blocking.
  4. If startup is routinely interrupted, investigate why shutdown is being requested before readiness.

Example fix

// before — startup context cancels on any signal immediately
app := fiber.New(fiber.Config{
    Services: []fiber.Service{slowCacheSvc, dbSvc},
    ServicesStartupContextProvider: func() context.Context {
        ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
        _ = stop
        return ctx
    },
})
// interrupt during slowCacheSvc.Start -> context canceled while starting service db

// after — generous deadline, ordered services
app := fiber.New(fiber.Config{
    Services: []fiber.Service{dbSvc, slowCacheSvc},
    ServicesStartupContextProvider: func() context.Context {
        ctx, _ := context.WithTimeout(context.Background(), 60*time.Second)
        return ctx
    },
})
Defensive patterns

Strategy: validation

Validate before calling

// Provide a startup context with a sane deadline.
app := fiber.New(fiber.Config{
    Services: svcs,
    ServicesStartupContextProvider: func() context.Context {
        ctx, _ := context.WithTimeout(context.Background(), startupBudget(svcs))
        return ctx
    },
})

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of gofiber/fiber@a105acad6c (2026-08-11). Data as JSON: /api/errors/0aa7db9159cf3af3. Report an issue: GitHub.

Appendix: source

Thrown at services.go:94

	return context.Background()
}

// startServices Handles the start process of services for the current application.
// Iterates over all configured services and tries to start them, returning an error if any error occurs.
func (app *App) startServices(ctx context.Context) error {
	if !app.hasConfiguredServices() {
		return nil
	}

	var errs []error
	for idx, srv := range app.configured.Services {
		if srv == nil {
			return fmt.Errorf("fiber: service at index %d is nil", idx)
		}
		if err := ctx.Err(); err != nil {
			// Context is canceled, return an error the soonest possible, so that
			// the user can see the context cancellation error and act on it.
			return fmt.Errorf("context canceled while starting service %s: %w", srv.String(), err)
		}

		err := srv.Start(ctx)
		if err == nil {
			// mark the service as started
			app.state.setService(srv)
			continue
		}

		if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
			return fmt.Errorf("service %s start: %w", srv.String(), err)
		}

		errs = append(errs, fmt.Errorf("service %s start: %w", srv.String(), err))
	}
	return errors.Join(errs...)
}

View on GitHub (pinned to a105acad6c)