{"record":{"id":"352b143b2f2d2439","repo":"wavetermdev/waveterm","slug":"procinfo-too-few-fields-in-stat-for-pid-d","errorCode":null,"errorMessage":"procinfo: too few fields in stat for pid %d","messagePattern":"procinfo: too few fields in stat for pid (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/util/procinfo/procinfo_linux.go","lineNumber":85,"sourceCode":"\tif lp < 0 || rp < 0 || rp <= lp {\n\t\treturn nil, fmt.Errorf(\"procinfo: malformed stat for pid %d\", pid)\n\t}\n\n\tpidStr := strings.TrimSpace(s[:lp])\n\tcomm := s[lp+1 : rp]\n\trest := strings.Fields(s[rp+1:])\n\n\t// rest[0] = field 3 (state), rest[1] = field 4 (ppid), ...\n\t// Fields after comm are numbered starting at 3, so rest[i] = field (i+3).\n\t// We need:\n\t//   rest[0]  = field  3  state\n\t//   rest[1]  = field  4  ppid\n\t//   rest[11] = field 14  utime\n\t//   rest[12] = field 15  stime\n\t//   rest[17] = field 20  num_threads\n\t//   rest[21] = field 24  rss (pages)\n\tif len(rest) < 22 {\n\t\treturn nil, fmt.Errorf(\"procinfo: too few fields in stat for pid %d\", pid)\n\t}\n\n\tparsedPid, err := strconv.ParseInt(pidStr, 10, 32)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"procinfo: parse pid: %w\", err)\n\t}\n\n\tstatusChar := rest[0]\n\tstatus, ok := LinuxStatStatus[statusChar]\n\tif !ok {\n\t\tstatus = \"unknown\"\n\t}\n\n\tinfo := &ProcInfo{\n\t\tPid:        int32(parsedPid),\n\t\tCommand:    comm,\n\t\tStatus:     status,\n\t\tCpuUser:    -1,","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/wavetermdev/waveterm/blob/a4447c1563b2df285ab89e76c82f91e1a1a49c1e/pkg/util/procinfo/procinfo_linux.go#L67-L103","documentation":"After stripping the comm field, /proc/<pid>/stat must contain at least 22 whitespace-separated fields for the parser to extract ppid, utime, stime, num_threads, and rss (it needs rest[0..21]). Fewer fields means the stat record was truncated or structurally different from what the kernel provides.","triggerScenarios":"A truncated read of a stat file during process exit; reading a fake/fixture stat file with too few columns; an exotic kernel whose stat format omits trailing fields.","commonSituations":"Custom kernels or stripped-down embedded Linux builds with divergent procfs; unit-test fixtures missing fields; corrupted procfs output from virtualization.","solutions":["Retry the read once — transient truncation during process teardown is possible","Verify /proc/<pid>/stat has ~52 fields (cat /proc/<pid>/stat | wc -w)","If on a custom/embedded kernel, confirm its procfs stat layout matches mainline field ordering","Fix test fixtures to include at least 24 stat fields"],"exampleFix":"// before\nif len(rest) < 22 {\n    return nil, fmt.Errorf(\"procinfo: too few fields in stat for pid %d\", pid)\n}\n// after: parse what is available instead of failing\nif len(rest) < 22 {\n    return info, nil // partial info; fields default to -1\n}","handlingStrategy":"retry","validationCode":"// verify field count before dependent processing\ndata, _ := os.ReadFile(fmt.Sprintf(\"/proc/%d/stat\", pid))\nif strings.Count(string(data), \" \") < 24 {\n    // truncated/insufficient stat; avoid processing\n}","typeGuard":null,"tryCatchPattern":"_, err := procinfo.GetProcInfo(ctx, nil, pid)\nif err != nil && strings.Contains(err.Error(), \"too few fields\") {\n    // retry after a short delay; persistent failure means kernel/fixture problem\n}","preventionTips":["Use mainline Linux kernels or verify procfs layout on custom kernels","Include >= 24 fields in test stat fixtures","Retry transient truncations instead of failing the poll cycle","Sanity-check 'wc -w /proc/<pid>/stat' (~52) in deployment diagnostics"],"tags":["linux","procfs","parsing","stat-format"],"backgroundTag":"proc-stat-malformed","analyzedSha":"a4447c1563b2df285ab89e76c82f91e1a1a49c1e","analyzedAt":"2026-09-01T15:26:23.972Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}