dgraph-io/dgraph · critical

no ZeroHooks configured - ensure worker package is imported

Error message

no ZeroHooks configured - ensure worker package is imported or hooks.Enable() is called

What it means

GetHooks returns the configured ZeroHooks, which must be registered either by importing the worker package (which sets them via init) or by explicitly calling hooks.Enable()/SetDefaultZeroHooks. If neither global config nor a default is present, it panics rather than returning nil hooks.

Source

Thrown at hooks/config.go:120

	return globalConfig.Load()
}

// SetDefaultZeroHooks registers the fallback ZeroHooks used when embedded mode is
// disabled or Config.ZeroHooks is nil. The worker package calls this from its init.
func SetDefaultZeroHooks(h ZeroHooks) {
	defaultZeroHooks.Store(&defaultHooksHolder{hooks: h})
}

// GetHooks returns the active Zero hooks.
// If embedded mode is not enabled, it returns the default hooks implementation.
func GetHooks() ZeroHooks {
	if cfg := globalConfig.Load(); cfg != nil && cfg.ZeroHooks != nil {
		return cfg.ZeroHooks
	}
	if h := defaultZeroHooks.Load(); h != nil {
		return h.hooks
	}
	panic("no ZeroHooks configured - ensure worker package is imported or hooks.Enable() is called")
}

View on GitHub (pinned to 759e242be6)

Solutions

  1. Call hooks.Enable() (or hooks.SetDefaultZeroHooks(...)) during startup before any GetHooks call
  2. Import the worker package for its init side effect (blank import: _ "your/module/worker")
  3. In tests, set default hooks in TestMain or a setup fixture
  4. Ensure the global config with ZeroHooks is loaded before use

Example fix

// before
func main() {
    StartWorker() // panics: no ZeroHooks
}
// after
import _ "your/module/worker"
func main() {
    hooks.Enable()
    StartWorker()
}
Defensive patterns

Strategy: try-catch

Validate before calling

if cfg := hooks.GlobalConfig(); cfg == nil || cfg.ZeroHooks == nil {
  if !hooks.DefaultsSet() { hooks.Enable() }
}

Try / catch

defer func() {
  if r := recover(); r != nil {
    if strings.Contains(fmt.Sprint(r), "no ZeroHooks configured") {
      hooks.Enable()
      // retry operation
    } else { panic(r) }
  }
}()

Prevention

When it happens

Trigger: Calling GetHooks (directly or via workers like AssignNsIdsOverNetwork) in a binary/test that never imports the worker package and never calls hooks.Enable() or sets a default ZeroHooks.

Common situations: Unit tests exercising hook-consuming code without the worker import, trimming imports to break the init-side-effect registration chain, or new entry points wired before hook initialization.

Related errors


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/24e2ec59b3567ef9. Report an issue: GitHub.