{"record":{"id":"60a78f140297f054","repo":"slimtoolkit/slim","slug":"error-stopping-target-app-w","errorCode":null,"errorMessage":"error stopping target app: %w","messagePattern":"error stopping target app: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/app/sensor/app.go","lineNumber":313,"sourceCode":"\tencoder := json.NewEncoder(&out)\n\tencoder.SetEscapeHTML(false)\n\tencoder.SetIndent(\" \", \" \")\n\t_ = encoder.Encode(info)\n\tfmt.Printf(\"%s\\n\", out.String())\n}\n\n// sensor control <stop-target-app|wait-for-event|change-log-level|...>\nfunc runControlCommand(ctx context.Context) error {\n\tif len(os.Args) < 3 {\n\t\treturn errors.New(\"missing command\")\n\t}\n\n\tcmd := control.Command(os.Args[2])\n\n\tswitch cmd {\n\tcase control.StopTargetAppCommand:\n\t\tif err := control.ExecuteStopTargetAppCommand(ctx, *commandsFile); err != nil {\n\t\t\treturn fmt.Errorf(\"error stopping target app: %w\", err)\n\t\t}\n\n\tcase control.WaitForEventCommand:\n\t\tif len(os.Args) < 4 {\n\t\t\treturn errors.New(\"missing event name\")\n\t\t}\n\t\tif err := control.ExecuteWaitEvenCommand(ctx, eventsFilePath(), event.Type(os.Args[3])); err != nil {\n\t\t\treturn fmt.Errorf(\"error waiting for sensor event: %w\", err)\n\t\t}\n\n\tdefault:\n\t\treturn fmt.Errorf(\"unknown command: %s\", cmd)\n\t}\n\n\treturn nil\n}\n","sourceCodeStart":295,"sourceCodeEnd":330,"githubUrl":"https://github.com/slimtoolkit/slim/blob/81940d17fa112cc678e32209214bcb2355cb3004/pkg/app/sensor/app.go#L295-L330","documentation":"The sensor CLI's runControlCommand dispatches control commands parsed from os.Args. When the command is 'stop-target-app', it calls control.ExecuteStopTargetAppCommand, which signals the target application process to shut down (typically via a commands file). Any failure inside that execution (writing/sending the stop command, the target not acknowledging) is wrapped with this message and returned from Run, aborting the sensor's control flow.","triggerScenarios":"Running the sensor binary with the stop-target-app control command (os.Args[2] == control.StopTargetAppCommand) while ExecuteStopTargetAppCommand fails — e.g. the commands file cannot be written, the target app already exited, or IPC with the target times out.","commonSituations":"Target app crashed before the sensor could stop it; stale/missing --commands-file path; permissions problem on the commands file; orchestrator (Kubernetes) killed the target first so the stop handshake fails.","solutions":["Inspect the wrapped inner error (%w) to find the real cause (file I/O vs process state).","Ensure the commands file path passed to the sensor is valid and writable before running the stop command.","Verify the target app process is still running and reachable by the sensor.","If the target is already gone, treat the stop as a no-op or add tolerance for 'already stopped' conditions."],"exampleFix":"// before\nif err := control.ExecuteStopTargetAppCommand(ctx, *commandsFile); err != nil {\n    return fmt.Errorf(\"error stopping target app: %w\", err)\n}\n// after\nif err := control.ExecuteStopTargetAppCommand(ctx, *commandsFile); err != nil {\n    if errors.Is(err, os.ErrNotExist) { // target already gone\n        return nil\n    }\n    return fmt.Errorf(\"error stopping target app: %w\", err)\n}","handlingStrategy":"try-catch","validationCode":"if fi, err := os.Stat(*commandsFile); err != nil || fi.IsDir() {\n    return fmt.Errorf(\"commands file %q is not writable/usable\", *commandsFile)\n}","typeGuard":"func isStopCommand(cmd control.Command) bool {\n    return cmd == control.StopTargetAppCommand\n}","tryCatchPattern":"err := sensorApp.Run(ctx)\nif err != nil {\n    var stopErr error\n    if errors.As(err, &stopErr) && strings.Contains(err.Error(), \"error stopping target app\") {\n        log.Warnf(\"target app stop failed (may already be dead): %v\", err)\n        return nil // treat already-stopped as success\n    }\n    return err\n}","preventionTips":["Verify the commands file path exists and is writable before launching the sensor","Check target app liveness before issuing the stop command","Unwrap the %w error to log the true root cause"],"tags":["process-control","cli","sensor","lifecycle"],"backgroundTag":"process-stop-failed","analyzedSha":"81940d17fa112cc678e32209214bcb2355cb3004","analyzedAt":"2026-08-31T23:06:12.682Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}