{"record":{"id":"0c3d8715e30e9ef3","repo":"slimtoolkit/slim","slug":"sensor-received-unepxected-command-v","errorCode":null,"errorMessage":"sensor received unepxected command: %#+v","messagePattern":"sensor received unepxected command: %#\\+v","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/app/sensor/controlled/controlled.go","lineNumber":224,"sourceCode":"\t\t\tdefault:\n\t\t\t\tlog.Info(\"sensor: ignoring unknown or unexpected command => \", cmd)\n\t\t\t} // eof: type switch\n\n\t\tcase err := <-mon.Errors():\n\t\t\tlog.WithError(err).Warn(\"sensor: non-critical monitor error condition\")\n\t\t\ts.exe.PubEvent(event.Error, monitor.NonCriticalError(err).Error())\n\n\t\tcase <-ticker.C:\n\t\t\ts.exe.HookTargetAppRunning()\n\t\t\tlog.Debug(\".\")\n\t\t} // eof: select\n\t}\n\n\tif !stopCommandReceived {\n\t\t// Monitor can finish before the stop command is received.\n\t\t// In such case, we have to await the explicit stop.\n\t\tif cmd, ok := (<-s.exe.Commands()).(*command.StopMonitor); !ok {\n\t\t\treturn fmt.Errorf(\"sensor received unepxected command: %#+v\", cmd)\n\t\t}\n\t}\n\n\treturn s.processMonitoringResults(mon)\n}\n\nfunc (s *Sensor) processMonitoringResults(mon monitor.CompositeMonitor) error {\n\t// A bit of code duplication to avoid starting a goroutine\n\t// for error event handling - keeping the control flow\n\t// \"single-threaded\" keeps reasoning about the logic.\n\tfor _, err := range mon.DrainErrors() {\n\t\tlog.WithError(err).Warn(\"sensor: non-critical monitor error condition (drained)\")\n\t\ts.exe.PubEvent(event.Error, monitor.NonCriticalError(err).Error())\n\t}\n\n\tlog.Info(\"sensor: composite monitor is done, checking status...\")\n\n\treport, err := mon.Status()","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/slimtoolkit/slim/blob/81940d17fa112cc678e32209214bcb2355cb3004/pkg/app/sensor/controlled/controlled.go#L206-L242","documentation":"runWithMonitor waits for the monitor to finish, then — if no stop was seen — expects the next command on the commands channel to be *command.StopMonitor. If the received command is of any other type (or the channel yields a different command), the type assertion fails and this error is returned. Note the typo 'unepxected' is in the message itself.","triggerScenarios":"During a monitored run, a command other than StopMonitor (e.g. StartMonitor, abort, or a nil/unknown command) arrives on s.exe.Commands() after the monitor finished.","commonSituations":"Control-plane sending commands in unexpected order, state machine races, duplicate start commands issued while a monitor is running.","solutions":["Log the received command (%#+v in the error shows its type and fields) and fix the control side to always send StopMonitor","Make the state machine reject out-of-order commands before they reach the sensor","Widen the check to tolerate/ignore known-benign commands while awaiting StopMonitor"],"exampleFix":"// before\nif cmd, ok := (<-s.exe.Commands()).(*command.StopMonitor); !ok {\n\treturn fmt.Errorf(\"sensor received unepxected command: %#+v\", cmd)\n}\n// after\nswitch cmd := (<-s.exe.Commands()).(type) {\ncase *command.StopMonitor:\n\t// proceed\ncase nil:\n\treturn fmt.Errorf(\"sensor commands channel closed\")\ndefault:\n\treturn fmt.Errorf(\"sensor received unexpected command: %#+v\", cmd)\n}","handlingStrategy":"type-guard","validationCode":"// control side: only send StopMonitor while a monitor is active\n// assert state before sending:\n// if sensorState != monitoring { return errors.New(\"no active monitor\") }","typeGuard":"func isStopMonitor(cmd interface{}) (*command.StopMonitor, bool) {\n\tc, ok := cmd.(*command.StopMonitor)\n\treturn c, ok\n}","tryCatchPattern":"if err := runSensor(); err != nil {\n\tif strings.Contains(err.Error(), \"received unepxected command\") {\n\t\tlog.Printf(\"out-of-order control command; check sender state machine\")\n\t}\n}","preventionTips":["Serialize control commands through a state machine","Never send commands other than StopMonitor while monitoring","Fix the 'unepxected' typo to ease log grep"],"tags":["go","sensor","command-handling","type-assertion"],"backgroundTag":"unexpected-command-received","analyzedSha":"81940d17fa112cc678e32209214bcb2355cb3004","analyzedAt":"2026-08-31T23:06:12.682Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}