dgraph-io/badger · error

ErrNilCallback

ErrNilCallback

Error message

Callback cannot be nil

What it means

ErrNilCallback is a sentinel guard error returned by DB.Subscribe when the caller supplies a nil callback function. Since subscription events are delivered by invoking cb for each KVList, a nil cb would panic inside the streaming loop, so the function validates it up front and refuses to start the subscription. The input at fault is the cb parameter; matches are irrelevant to this check.

Source

Thrown at errors.go:97

	ErrZeroBandwidth = stderrors.New("Bandwidth must be greater than zero")

	// ErrWindowsNotSupported is returned when opt.ReadOnly is used on Windows
	ErrWindowsNotSupported = stderrors.New("Read-only mode is not supported on Windows")

	// ErrPlan9NotSupported is returned when opt.ReadOnly is used on Plan 9
	ErrPlan9NotSupported = stderrors.New("Read-only mode is not supported on Plan 9")

	// ErrTruncateNeeded is returned when the value log gets corrupt, and requires truncation of
	// corrupt data to allow Badger to run properly.
	ErrTruncateNeeded = stderrors.New(
		"Log truncate required to run DB. This might result in data loss")

	// ErrBlockedWrites is returned if the user called DropAll. During the process of dropping all
	// data from Badger, we stop accepting new writes, by returning this error.
	ErrBlockedWrites = stderrors.New("Writes are blocked, possibly due to DropAll or Close")

	// ErrNilCallback is returned when subscriber's callback is nil.
	ErrNilCallback = stderrors.New("Callback cannot be nil")

	// ErrEncryptionKeyMismatch is returned when the storage key is not
	// matched with the key previously given.
	ErrEncryptionKeyMismatch = stderrors.New("Encryption key mismatch")

	// ErrInvalidDataKeyID is returned if the datakey id is invalid.
	ErrInvalidDataKeyID = stderrors.New("Invalid datakey id")

	// ErrInvalidEncryptionKey is returned if length of encryption keys is invalid.
	ErrInvalidEncryptionKey = stderrors.New("Encryption key's length should be" +
		"either 16, 24, or 32 bytes")
	// ErrGCInMemoryMode is returned when db.RunValueLogGC is called in in-memory mode.
	ErrGCInMemoryMode = stderrors.New("Cannot run value log GC when DB is opened in InMemory mode")

	// ErrGCInReadOnlyMode is returned when db.RunValueLogGC is called in read-only mode.
	ErrGCInReadOnlyMode = stderrors.New("Cannot run value log GC when DB is opened in ReadOnly mode")

	// ErrDBClosed is returned when a get operation is performed after closing the DB.

View on GitHub (pinned to 2a001d466f)

Solutions

  1. Pass a non-nil func(kv *KVList) error to Subscribe
  2. If subscription is optional, guard the call: only invoke Subscribe when the callback is set
  3. Return/log a config error at startup when the callback dependency is missing

Example fix

// before
db.Subscribe(ctx, nil, matches)
// after
if cb != nil {
    err := db.Subscribe(ctx, cb, matches)
}
Defensive patterns

Strategy: validation

Validate before calling

if cb == nil { return errors.New("subscription callback must be provided") }

Try / catch

if err := db.Subscribe(ctx, cb, matches); err != nil {
    if errors.Is(err, badger.ErrNilCallback) {
        return fmt.Errorf("subscribe misconfigured: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling db.Subscribe(ctx, nil, matches) — passing an untyped nil or a nil function variable as the cb argument.

Common situations: Building subscription registration from config/dependency injection where the callback wasn't wired; passing nil conditionally when a feature flag disables subscription handling.

Related errors


AI-assisted analysis of dgraph-io/badger@2a001d466f (2026-09-05). Data as JSON: /api/errors/ad25e423d7704f17. Report an issue: GitHub.