Race condition on terminal settings when piping lxc-attach output

Skip to first unread message

Time Crystal

May 17, 2021, 11:47:07 PM5/17/21
to lxc-...@lists.linuxcontainers.org

lxc-attach changes terminal settings for the duration of the command it
runs. When piping the output of a long running command within lxc-attach
to another command that writes data to standard out, the output will be
subject to the tty settings that lxc-attach set, not the settings that
were set before lxc-attach was run. For instance, observe the output

$ lxc-attach -n container 'journalctl -f' | cat

Since lxc-attach disabled the stty setting "onlcr," newlines (lf) will
not be transformed into carriage-return-newlines (crlf) automatically by
the tty.

One hacky way to fix this:

$ lxc-attach -n container 'journalctl -f' | (read -r _dummy; stty -F
/dev/tty onclr; cat) | cat

That command first waits for output from the container command to make
sure lxc-attach has already done its modification, then sets the correct
stty setting, then goes about piping the output as normal. Unfortunately
this loses the first line of output.

Is this a known issue? Is there a better workaround? Nothing in the
lxc-attach manpage seemed to help. Thank you for your consideration.

Time Crystal
Reply all
Reply to author
0 new messages