slimtoolkit/slim · error

sensor shutdown before monitor stop

Error message

sensor shutdown before monitor stop

What it means

ErrPrematureShutdown is a sentinel error from the controlled sensor package signaling that the sensor process shut down before the monitor was stopped, violating the expected lifecycle order (start monitor -> run -> stop monitor -> shutdown). The runWithMonitor orchestration uses it to detect an out-of-order or crashed teardown so callers don't mistake an orderly run for one whose monitoring data is incomplete.

Source

Thrown at pkg/app/sensor/controlled/controlled.go:20

import (
	"context"
	"errors"
	"fmt"
	"time"

	log "github.com/sirupsen/logrus"

	"github.com/slimtoolkit/slim/pkg/app/sensor/artifact"
	"github.com/slimtoolkit/slim/pkg/app/sensor/execution"
	"github.com/slimtoolkit/slim/pkg/app/sensor/monitor"
	"github.com/slimtoolkit/slim/pkg/ipc/command"
	"github.com/slimtoolkit/slim/pkg/ipc/event"
	"github.com/slimtoolkit/slim/pkg/mondel"
	"github.com/slimtoolkit/slim/pkg/util/errutil"
)

var ErrPrematureShutdown = errors.New("sensor shutdown before monitor stop")

type Sensor struct {
	ctx context.Context
	exe execution.Interface

	newMonitor monitor.NewCompositeMonitorFunc
	del        mondel.Publisher
	artifactor artifact.Processor

	workDir    string
	mountPoint string
}

func NewSensor(
	ctx context.Context,
	exe execution.Interface,
	newMonitor monitor.NewCompositeMonitorFunc,
	del mondel.Publisher,

View on GitHub (pinned to 81940d17fa)

Solutions

  1. Stop the monitor first, then shut down the sensor (monitor.Stop before sensor shutdown)
  2. Check whether the target app exited early and fix the app start command/entrypoint
  3. In tests, follow the documented lifecycle: Start -> run -> Shutdown/Stop in order
  4. If the sensor crashed, inspect sensor logs for the root cause before the lifecycle error

Example fix

// before
sensor.Shutdown()
monitor.Stop()
// after
monitor.Stop()
sensor.Shutdown()
Defensive patterns

Strategy: try-catch

Validate before calling

// Go: verify lifecycle state before shutting down
if monitor.IsRunning() {
    if err := monitor.Stop(); err != nil { return err }
}
if err := sensor.Shutdown(); err != nil {
    if errors.Is(err, controlled.ErrPrematureShutdown) { /* handle */ }
}

Type guard

func IsPrematureShutdown(err error) bool {
    return errors.Is(err, controlled.ErrPrematureShutdown)
}

Try / catch

if err := runWithMonitor(ctx, ...); err != nil {
    if errors.Is(err, controlled.ErrPrematureShutdown) {
        // sensor exited before monitor stop: log and re-run with correct order
    } else {
        return err
    }
}

Prevention

When it happens

Trigger: Calling sensor shutdown/termination while the composite monitor is still running; the sensor's runWithMonitor flow observes the sensor exiting before monitor stop is invoked (e.g., the app exits and shutdown is triggered out of order).

Common situations: Container apps that exit almost immediately (bad entrypoint, config crash); calling shutdown before Stop on the monitor in custom integrations; race conditions in test code like TestStartFollowedByShutdown that stop things in the wrong order.

Related errors


AI-assisted analysis of slimtoolkit/slim@81940d17fa (2026-08-31). Data as JSON: /api/errors/fb2a51a4895b2f2e. Report an issue: GitHub.