{"record":{"id":"d2fca6b0c6ba3b87","repo":"larksuite/cli","slug":"busdiscover-malformed-pid-in-s-w","errorCode":null,"errorMessage":"busdiscover: malformed pid in %s: %w","messagePattern":"busdiscover: malformed pid in (.+?): %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/event/adapter/localbus/busdiscover/pidfile.go","lineNumber":74,"sourceCode":"\t\t_ = lock.Unlock()\n\t\treturn nil, fmt.Errorf(\"busdiscover: rename pid file: %w\", err)\n\t}\n\treturn &Handle{lock: lock}, nil\n}\n\nfunc readPIDFile(eventsDir string) (int, time.Time, error) {\n\tpidPath := filepath.Join(eventsDir, pidFileName)\n\tdata, err := vfs.ReadFile(pidPath)\n\tif err != nil {\n\t\treturn 0, time.Time{}, err\n\t}\n\tlines := strings.SplitN(strings.TrimSpace(string(data)), \"\\n\", 2)\n\tif len(lines) < 2 {\n\t\treturn 0, time.Time{}, fmt.Errorf(\"busdiscover: malformed pid file %s\", pidPath)\n\t}\n\tpid, err := strconv.Atoi(strings.TrimSpace(lines[0]))\n\tif err != nil {\n\t\treturn 0, time.Time{}, fmt.Errorf(\"busdiscover: malformed pid in %s: %w\", pidPath, err)\n\t}\n\tstartTime, err := time.Parse(time.RFC3339, strings.TrimSpace(lines[1]))\n\tif err != nil {\n\t\treturn 0, time.Time{}, fmt.Errorf(\"busdiscover: malformed timestamp in %s: %w\", pidPath, err)\n\t}\n\treturn pid, startTime, nil\n}\n\n// isBusAlive: try-lock the alive file. ErrHeld = live holder; success = stale (release immediately).\nfunc isBusAlive(appDir string) bool {\n\tlockPath := filepath.Join(appDir, aliveLockFileName)\n\tif _, err := vfs.Stat(lockPath); err != nil {\n\t\treturn false\n\t}\n\tprobe := lockfile.New(lockPath)\n\terr := probe.TryLock()\n\tif errors.Is(err, lockfile.ErrHeld) {\n\t\treturn true","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/larksuite/cli/blob/7fd6ef3c07182257ce776cdc5a614e122d5bd4b3/internal/event/adapter/localbus/busdiscover/pidfile.go#L56-L92","documentation":"readPIDFile throws this when the first line of bus.pid is present but strconv.Atoi cannot parse it as an integer PID. Like the malformed-file case, scanLiveBuses logs it and reports the live bus with PID 0 instead of failing the scan. A wrapped strconv.NumError is preserved via %w.","triggerScenarios":"bus.pid's first line is non-numeric — e.g. the file was overwritten with arbitrary text, contains a BOM or CRLF artifacts combined with stray characters, or a tool wrote a different format into the pid slot.","commonSituations":"Manual edits pasting text into bus.pid, another program hijacking the well-known filename, copy-pasted content with whitespace/labels like 'PID: 1234', or encoding issues (UTF-8 BOM) from editors.","solutions":["Remove the corrupt bus.pid and restart the bus so WritePIDFile writes a clean '<pid>\\n<timestamp>' payload","Verify the first line is digits only (inspect with od -c or cat -A for BOM/CRLF) and strip any editor artifacts","Identify and stop whatever external tool is writing a non-standard format into bus.pid","If the bus is dead, delete the whole stale app directory so discovery stops reporting PID 0"],"exampleFix":"// before: pid line not an integer\n$ cat bus.pid\nPID: 1234\n2026-09-04T08:00:00Z\n// after: republish correct payload\n$ rm bus.pid && restart the bus\n$ cat bus.pid\n1234\n2026-09-04T08:00:00Z","handlingStrategy":"validation","validationCode":"// Confirm the pid line parses before relying on scan output\nfunc pidLineIsNumeric(path string) bool {\n    data, err := os.ReadFile(path)\n    if err != nil { return false }\n    first := strings.SplitN(strings.TrimSpace(string(data)), \"\\n\", 2)[0]\n    _, err = strconv.Atoi(strings.TrimSpace(first))\n    return err == nil\n}","typeGuard":"// Narrow scan results to entries with a parseable PID\nfunc withValidPID(ps []busdiscover.Process) []busdiscover.Process {\n    out := make([]busdiscover.Process, 0, len(ps))\n    for _, p := range ps {\n        if p.PID > 0 { out = append(out, p) }\n    }\n    return out\n}","tryCatchPattern":"procs, _ := scanLiveBuses(eventsDir)\nfor _, p := range procs {\n    if p.PID == 0 {\n        log.Warnf(\"bus for %s holds alive lock but pid file is unparseable; skipping signal\", p.AppID)\n        continue\n    }\n    signal(p.PID)\n}","preventionTips":["Keep editors and scripts from writing labeled text (e.g. 'PID: 1234') into bus.pid","Save pid-file edits without BOM or CRLF line endings; prefer not editing at all","Reserve the bus.pid filename for the busdiscover writer only","Delete and republish the file via a bus restart instead of hand-repairing its contents"],"tags":["go","pidfile","parse-error","corrupt-file","localbus"],"backgroundTag":"pid-parse-failed","analyzedSha":"7fd6ef3c07182257ce776cdc5a614e122d5bd4b3","analyzedAt":"2026-09-04T21:17:44.649Z","contentChangedAt":"2026-09-04T21:17:44.649Z","schemaVersion":2},"datasetVersion":"2026-09-12T02:17:10.037Z"}