kataras/iris · error

MVC: DependenciesReadOnly called from wrong state the API gi

Error message

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

What it means

DependenciesReadOnly returns the controller's resolved dependency list from the injector container, which does not exist until the controller is activated. Calling it before activation means the dependency-injection graph has not been built, so it panics to expose the illegal state instead of returning nil.

Source

Thrown at mvc/controller.go:331

	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
}

// checks if a method is already registered.
func (c *ControllerActivator) isReservedMethod(name string) bool {
	for methodName := range c.routes {
		if methodName == name {
			return true
		}
	}

View on GitHub (pinned to 7bedaf55a0)

Solutions

  1. Query DependenciesReadOnly only from an AfterActivation callback where the injector is guaranteed to exist.
  2. If you must know dependencies earlier, collect them from the hero.Container you registered rather than the activator.
  3. Report as a bug if the panic occurs inside AfterActivation despite correct usage.

Example fix

// before
deps := m.Activator.DependenciesReadOnly() // panics before activation
// after
m.AfterActivation(func(a *mvc.Activation) {
	deps := a.DependenciesReadOnly()
	for _, d := range deps { log.Println(d.DestType) }
})
Defensive patterns

Strategy: validation

Validate before calling

mvcApp.AfterActivation(func(a *mvc.Activation) {
	for _, d := range a.DependenciesReadOnly() {
		log.Printf("dep: %v", d.DestType)
	}
})

Try / catch

defer func() {
	if r := recover(); r != nil {
		log.Printf("DependenciesReadOnly before activation: %v", r)
	}
}()

Prevention

When it happens

Trigger: Calling activator.DependenciesReadOnly() before mvc activation completes — e.g. in Configure hooks, right after Handle(), or in a test before AfterActivation ran.

Common situations: Introspection/debug tooling that walks registered controllers at boot, dependency audit code, or documentation examples copied out of the AfterActivation context.

Related errors


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