Skip to content

pgrep save strategy emits raw newlines and corrupts the save file #590

Description

@tim-phillips

Summary

save_command_strategies/pgrep.sh uses pgrep -lf -P "$PANE_PID", which prints each child's argument list verbatim. If a pane is running a command whose arguments contain a newline (for example a multi-line snippet typed at the prompt, or bash -c $'a\nb'), the strategy emits multiple lines for that one pane.

save.sh writes one tab-delimited record per pane, so the extra lines land in the save file as garbage records. Restore then either skips the pane or misparses subsequent fields.

Reproduce

In any shell (a tmux pane is not required, only a parent PID):

bash -c 'echo $$ > /tmp/parent.pid; bash -c "sleep 60
sleep 60" & wait' &
sleep 1
save_command_strategies/pgrep.sh "$(cat /tmp/parent.pid)" | cat -A

Output with the current strategy (three lines for one child process):

bash -c sleep 60$
sleep 60$

The default ps strategy renders the same process on a single line, with the newline escaped:

bash -c sleep 60\012sleep 60$

Why it matters

The ps strategy runs ps -ao ppid,args (a full process-table dump) once per pane, which gets slow with many panes and is the likely cause of reports like #544. The pgrep strategy is the natural cheaper alternative, since pgrep -P asks the kernel for one pane's children directly, but it is not safe to use because of the above.

Fix

Ask pgrep -P only for the child PIDs, then format them with ps -o args= -p <pids>. That produces the same output as the ps strategy while keeping the per-pane cost low. PR to follow.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions