{"record":{"id":"a4f2cdd6c3d04023","repo":"pranshuparmar/witr","slug":"unexpected-ps-output-format-for-pid-d","errorCode":null,"errorMessage":"unexpected ps output format for pid %d","messagePattern":"unexpected ps output format for pid (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/proc/process_darwin.go","lineNumber":40,"sourceCode":"\n\t// Format: pid(0) ppid(1) uid(2) lstart(3-7) state(8) pcpu(9) rss(10) args(11+)\n\t// ucomm is excluded because it can contain spaces (e.g. \"Microsoft Teams\"),\n\t// which breaks strings.Fields parsing. The display name is derived from args instead.\n\tcmd := exec.Command(\"ps\", \"-p\", pidStr, \"-o\", \"pid=,ppid=,uid=,lstart=,state=,pcpu=,rss=,args=\")\n\tcmd.Env = buildEnvForPS()\n\tout, err := cmd.Output()\n\tif err != nil {\n\t\treturn model.Process{}, fmt.Errorf(\"process %d not found: %w\", pid, err)\n\t}\n\n\tline := strings.TrimSpace(string(out))\n\tif line == \"\" {\n\t\treturn model.Process{}, fmt.Errorf(\"process %d not found\", pid)\n\t}\n\n\tfields := strings.Fields(line)\n\tif len(fields) < 11 {\n\t\treturn model.Process{}, fmt.Errorf(\"unexpected ps output format for pid %d\", pid)\n\t}\n\n\tppid, _ := strconv.Atoi(fields[1])\n\tuid, _ := strconv.Atoi(fields[2])\n\n\tlstartStr := strings.Join(fields[3:8], \" \")\n\tstartedAt, _ := time.Parse(\"Mon Jan 2 15:04:05 2006\", lstartStr)\n\tif startedAt.IsZero() {\n\t\tstartedAt = time.Now().UTC()\n\t}\n\n\tstate := fields[8]\n\n\tcpuPct, _ := strconv.ParseFloat(fields[9], 64)\n\trssKB, _ := strconv.ParseFloat(fields[10], 64)\n\n\trawCmdline := \"\"\n\tif len(fields) > 11 {","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/pranshuparmar/witr/blob/dc4fa1da82d3e266fcbd928641b4f30b3077c64f/internal/proc/process_darwin.go#L22-L58","documentation":"ReadProcess on macOS parses ps output with a strict field layout (pid ppid uid lstart(5 fields) state pcpu rss args...). If the trimmed line has fewer than 11 fields, the output format does not match and parsing is aborted rather than producing garbage. This guards against incompatible or non-standard ps implementations.","triggerScenarios":"ReadProcess encountering ps output with < 11 whitespace-separated fields — a ps variant that ignores the -o format spec, locale/localized output, or truncated/aliased ps output.","commonSituations":"Environments with BusyBox/toybox ps or a shimmed ps in PATH; locale settings changing column formatting; a ps wrapper that reformats output.","solutions":["Verify `ps -p <pid> -o pid=,ppid=,uid=,lstart=,state=,pcpu=,rss=,args=` on that machine returns the expected columns.","Ensure the system ps is first in PATH (no BusyBox/wrapper shadowing /usr/bin/ps).","Run with a standard locale (e.g. LC_ALL=C) to rule out localized formatting.","Capture the raw ps output to diagnose the actual format and report it if the system ps looks standard."],"exampleFix":"// before\nLC_ALL=de_DE.UTF-8 witr <pid>\n// after\nLC_ALL=C witr <pid>","handlingStrategy":"validation","validationCode":"// sanity-check ps output format in your environment once at startup\nout, _ := exec.Command(\"ps\", \"-p\", \"1\", \"-o\", \"pid=,ppid=,uid=,lstart=,state=,pcpu=,rss=,args=\").Output()\nif out != nil && len(strings.Fields(strings.TrimSpace(string(out)))) < 11 {\n    // non-standard ps detected; don't rely on ReadProcess\n}","typeGuard":null,"tryCatchPattern":"proc, err := proc.ReadProcess(pid)\nif err != nil {\n    if strings.Contains(err.Error(), \"unexpected ps output format\") {\n        // environment problem: check ps variant/locale before retrying\n    }\n}","preventionTips":["Standardize locale (LC_ALL=C) for processes spawning ps","Verify /usr/bin/ps is not shadowed by BusyBox/toybox/wrappers","Smoke-test the ps -o format on target machines before deployment"],"tags":["macos","ps-output-parsing","environment"],"backgroundTag":"unexpected-ps-output-format","analyzedSha":"dc4fa1da82d3e266fcbd928641b4f30b3077c64f","analyzedAt":"2026-09-01T12:17:08.767Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}