golang/go · error

bad exit code: (in )

Error message

bad exit code: %v (in %q)

What it means

Returned by exitCodeFilter.Finish (main.go:286) when the regex matched and captured an exit-code group, but strconv.Atoi failed to parse it as an integer. The captured text was present but not numeric, indicating malformed output from the device.

Solutions

  1. Examine the quoted buffer in the error — it shows exactly what was captured.
  2. Reduce output interleaving: run with -p 1 (no parallel tests) to see if the marker stabilizes.
  3. Ensure no test prints bytes that mimic the exit-code marker format.
  4. If using a custom runner, make sure it emits the canonical 'exit status <int>' line.
Defensive patterns

Strategy: try-catch

Try / catch

if _, err := run.onDevice(...); err != nil {
    if strings.Contains(err.Error(), "bad exit code") {
        // output interleaving; retry with -p 1
        return rerunSerial()
    }
    return err
}

Prevention

When it happens

Trigger: The exit-code regex matched text that is not a pure integer — e.g., the regex captured trailing characters, or the device/process emitted a non-numeric value where the exit code was expected. Generally points to a corrupted or partially-overwritten marker line.

Common situations: Output interleaving/concurrency garbled the exit marker line; a custom test harness prints text that matches the regex shape but with non-numeric content; terminal control sequences embedded in the captured group.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/2ea612e99c50c60e. Report an issue: GitHub.

Appendix: source

Thrown at misc/go_android_exec/main.go:286

	b := f.buf.Bytes()
	defer f.buf.Reset()
	match := f.exitRe.FindSubmatch(b)
	if len(match) < 2 || match[1] == nil {
		// Not a full match. Flush.
		if _, err := f.w.Write(b); err != nil {
			return 0, err
		}
		return 0, fmt.Errorf("no exit code (in %q)", string(b))
	}

	// Parse the exit code.
	code, err := strconv.Atoi(string(match[1]))
	if err != nil {
		// Something is malformed. Flush.
		if _, err := f.w.Write(b); err != nil {
			return 0, err
		}
		return 0, fmt.Errorf("bad exit code: %v (in %q)", err, string(b))
	}
	return code, nil
}

// pkgPath determines the package import path of the current working directory,
// and indicates whether it is
// and returns the path to the package source relative to $GOROOT (or $GOPATH).
func pkgPath() (importPath string, isStd bool, modPath, modDir string, err error) {
	errorf := func(format string, args ...any) (string, bool, string, string, error) {
		return "", false, "", "", fmt.Errorf(format, args...)
	}
	goTool, err := goTool()
	if err != nil {
		return errorf("%w", err)
	}
	cmd := exec.Command(goTool, "list", "-e", "-f", "{{.ImportPath}}:{{.Standard}}{{with .Module}}:{{.Path}}:{{.Dir}}{{end}}", ".")
	out, err := cmd.Output()
	if err != nil {

View on GitHub (pinned to b6b368adc5)