{"record":{"id":"fb2a51a4895b2f2e","repo":"slimtoolkit/slim","slug":"sensor-shutdown-before-monitor-stop","errorCode":null,"errorMessage":"sensor shutdown before monitor stop","messagePattern":"sensor shutdown before monitor stop","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/app/sensor/controlled/controlled.go","lineNumber":20,"sourceCode":"\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"fmt\"\n\t\"time\"\n\n\tlog \"github.com/sirupsen/logrus\"\n\n\t\"github.com/slimtoolkit/slim/pkg/app/sensor/artifact\"\n\t\"github.com/slimtoolkit/slim/pkg/app/sensor/execution\"\n\t\"github.com/slimtoolkit/slim/pkg/app/sensor/monitor\"\n\t\"github.com/slimtoolkit/slim/pkg/ipc/command\"\n\t\"github.com/slimtoolkit/slim/pkg/ipc/event\"\n\t\"github.com/slimtoolkit/slim/pkg/mondel\"\n\t\"github.com/slimtoolkit/slim/pkg/util/errutil\"\n)\n\nvar ErrPrematureShutdown = errors.New(\"sensor shutdown before monitor stop\")\n\ntype Sensor struct {\n\tctx context.Context\n\texe execution.Interface\n\n\tnewMonitor monitor.NewCompositeMonitorFunc\n\tdel        mondel.Publisher\n\tartifactor artifact.Processor\n\n\tworkDir    string\n\tmountPoint string\n}\n\nfunc NewSensor(\n\tctx context.Context,\n\texe execution.Interface,\n\tnewMonitor monitor.NewCompositeMonitorFunc,\n\tdel mondel.Publisher,","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/slimtoolkit/slim/blob/81940d17fa112cc678e32209214bcb2355cb3004/pkg/app/sensor/controlled/controlled.go#L2-L38","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Stop the monitor first, then shut down the sensor (monitor.Stop before sensor shutdown)","Check whether the target app exited early and fix the app start command/entrypoint","In tests, follow the documented lifecycle: Start -> run -> Shutdown/Stop in order","If the sensor crashed, inspect sensor logs for the root cause before the lifecycle error"],"exampleFix":"// before\nsensor.Shutdown()\nmonitor.Stop()\n// after\nmonitor.Stop()\nsensor.Shutdown()","handlingStrategy":"try-catch","validationCode":"// Go: verify lifecycle state before shutting down\nif monitor.IsRunning() {\n    if err := monitor.Stop(); err != nil { return err }\n}\nif err := sensor.Shutdown(); err != nil {\n    if errors.Is(err, controlled.ErrPrematureShutdown) { /* handle */ }\n}","typeGuard":"func IsPrematureShutdown(err error) bool {\n    return errors.Is(err, controlled.ErrPrematureShutdown)\n}","tryCatchPattern":"if err := runWithMonitor(ctx, ...); err != nil {\n    if errors.Is(err, controlled.ErrPrematureShutdown) {\n        // sensor exited before monitor stop: log and re-run with correct order\n    } else {\n        return err\n    }\n}","preventionTips":["Always call monitor.Stop() before sensor shutdown","Handle immediate app exits (fix entrypoint) before lifecycle teardown","Mirror the ordering used in TestStartFollowedByShutdown in your integration code"],"tags":["lifecycle","sensor","monitoring","go"],"backgroundTag":"sensor-shutdown-order","analyzedSha":"81940d17fa112cc678e32209214bcb2355cb3004","analyzedAt":"2026-08-31T23:06:12.682Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}