kataras/iris · error

MVC: Singleton called from wrong state the API gives access

Error message

MVC: Singleton called from wrong state the API gives access to it only `AfterActivation`, report this as bug

What it means

ControllerActivator.Singleton reads the injector's Singleton flag, but the injector is only created after the controller is activated (its dependencies resolved). Calling Singleton before activation means the framework is being used out of order, so it panics and asks you to report it as a bug if reached through the documented AfterActivation flow.

Source

Thrown at mvc/controller.go:323

}

// Use registers a middleware for this Controller.
// It appends one or more handlers to the `BeginHandlers`.
// It's like the `Party.Use` but specifically
// for the routes that this controller will register to the targeted `Party`.
func (c *ControllerActivator) Use(handlers ...context.Handler) *ControllerActivator {
	c.BeginHandlers = append(c.BeginHandlers, handlers...)
	return c
}

// Singleton returns new if all incoming clients' requests
// have the same controller instance.
// This is done automatically by iris to reduce the creation
// of a new controller on each request, if the controller doesn't contain
// any unexported fields and all fields are services-like, static.
func (c *ControllerActivator) Singleton() bool {
	if c.injector == nil {
		panic("MVC: Singleton called from wrong state the API gives access to it only `AfterActivation`, report this as bug")
	}
	return c.injector.Singleton
}

// DependenciesReadOnly returns a list of dependencies, including the controller's one.
func (c *ControllerActivator) DependenciesReadOnly() []*hero.Dependency {
	if c.injector == nil {
		panic("MVC: DependenciesReadOnly called from wrong state the API gives access to it only `AfterActivation`, report this as bug")
	}

	return c.injector.Container.Dependencies
}

// Dependencies returns a value which can manage the controller's dependencies.
func (c *ControllerActivator) Dependencies() *hero.Container {
	return c.app.container // although the controller's one are: c.injector.Container
}

View on GitHub (pinned to 7bedaf55a0)

Solutions

  1. Move the Singleton() call into an AfterActivation callback, e.g. mvcApp.AfterActivation(func(a *mvc.Activation) { _ = a.Singleton() }).
  2. If you need the flag earlier, resolve dependencies first by calling activator.Activate(nil) or rely on Activate/Handlers flow before inspecting state.
  3. If it panics even inside AfterActivation, report it to the iris repo as the message instructs.

Example fix

// before
m := mvc.New(app)
m.Handle(new(UsersController))
isSingleton := m.Activator.Singleton() // panics: injector nil
// after
m := mvc.New(app)
m.Handle(new(UsersController))
m.AfterActivation(func(a *mvc.Activation) {
	_ = a.Singleton() // safe here
})
Defensive patterns

Strategy: validation

Validate before calling

// Only read singleton state inside AfterActivation:
mvcApp.AfterActivation(func(a *mvc.Activation) {
	_ = a.Singleton()
})

Type guard

// Go cannot nil-check private fields; guard by lifecycle:
// safe only when invoked from AfterActivation callbacks.

Try / catch

defer func() {
	if r := recover(); r != nil {
		log.Printf("MVC singleton state error: %v", r)
	}
}()

Prevention

When it happens

Trigger: Calling activator.Singleton() right after mvc.New(app).Handle (or inside a controller constructor) before AfterActivation has run, e.g. during Configure-time reflection on a controller that has no activation yet.

Common situations: Custom bootstrap code inspecting controller metadata before app.Listen/AfterActivation, tests that grab the activator and query it immediately, or plugin code hooking into MVC setup too early.

Related errors


AI-assisted analysis of kataras/iris@7bedaf55a0 (2026-08-30). Data as JSON: /api/errors/396c7bd166eaa269. Report an issue: GitHub.