slimtoolkit/slim · error

error stopping target app: %w

Error message

error stopping target app: %w

What it means

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.

Source

Thrown at pkg/app/sensor/app.go:313

	encoder := json.NewEncoder(&out)
	encoder.SetEscapeHTML(false)
	encoder.SetIndent(" ", " ")
	_ = encoder.Encode(info)
	fmt.Printf("%s\n", out.String())
}

// sensor control <stop-target-app|wait-for-event|change-log-level|...>
func runControlCommand(ctx context.Context) error {
	if len(os.Args) < 3 {
		return errors.New("missing command")
	}

	cmd := control.Command(os.Args[2])

	switch cmd {
	case control.StopTargetAppCommand:
		if err := control.ExecuteStopTargetAppCommand(ctx, *commandsFile); err != nil {
			return fmt.Errorf("error stopping target app: %w", err)
		}

	case control.WaitForEventCommand:
		if len(os.Args) < 4 {
			return errors.New("missing event name")
		}
		if err := control.ExecuteWaitEvenCommand(ctx, eventsFilePath(), event.Type(os.Args[3])); err != nil {
			return fmt.Errorf("error waiting for sensor event: %w", err)
		}

	default:
		return fmt.Errorf("unknown command: %s", cmd)
	}

	return nil
}

View on GitHub (pinned to 81940d17fa)

Solutions

  1. Inspect the wrapped inner error (%w) to find the real cause (file I/O vs process state).
  2. Ensure the commands file path passed to the sensor is valid and writable before running the stop command.
  3. Verify the target app process is still running and reachable by the sensor.
  4. If the target is already gone, treat the stop as a no-op or add tolerance for 'already stopped' conditions.

Example fix

// before
if err := control.ExecuteStopTargetAppCommand(ctx, *commandsFile); err != nil {
    return fmt.Errorf("error stopping target app: %w", err)
}
// after
if err := control.ExecuteStopTargetAppCommand(ctx, *commandsFile); err != nil {
    if errors.Is(err, os.ErrNotExist) { // target already gone
        return nil
    }
    return fmt.Errorf("error stopping target app: %w", err)
}
Defensive patterns

Strategy: try-catch

Validate before calling

if fi, err := os.Stat(*commandsFile); err != nil || fi.IsDir() {
    return fmt.Errorf("commands file %q is not writable/usable", *commandsFile)
}

Type guard

func isStopCommand(cmd control.Command) bool {
    return cmd == control.StopTargetAppCommand
}

Try / catch

err := sensorApp.Run(ctx)
if err != nil {
    var stopErr error
    if errors.As(err, &stopErr) && strings.Contains(err.Error(), "error stopping target app") {
        log.Warnf("target app stop failed (may already be dead): %v", err)
        return nil // treat already-stopped as success
    }
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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