Skip to content

Conversation

krobelus
Copy link

@krobelus krobelus commented Oct 15, 2025

As reported in MidnightCommander/mc#4789,
after Midnight Commander starts a shell, it writes some characters
to the shells stdin, to initialize it.

Since mc doesn't know when shell initialization
(e.g. .bashrc), it doesn't wait for that.
Hence mc's commands race with bashrc.

This surfaces reliably when running fastfetch from .bashrc with a *.png
logo. Writing the logo involves a call to ffDetectTerminalSize(),
which queries /dev/tty.

It first flushes (TCSAFLUSH) the input since a070519 (IO: disallow
input echo, 2024-07-31), which removes the input queued by mc.

Then it reads from the TTY 1024 bytes at a time.
This is also bad because it might swallow key presses
made by the user.

So maybe

  1. Don't flush.
  2. Reduce the likelihood of swallowing users's key presses by reading
    only one byte at a time.

Note that with or without TCSAFLUSH,
our query responses might be interspersed with keyboard input.
Though I guess TCSAFLUSH makes this less likely.

As reported in MidnightCommander/mc#4789,
after Midnight Commander starts a shell, it writes some characters
to the shells stdin, to initialize it.

Since mc doesn't know when shell initialization
(e.g. .bashrc), it doesn't wait for that.
Hence mc's commands race with bashrc.

This surfaces reliably when running fastfetch from .bashrc with a *.png
logo.  Writing the logo involves a call to ffDetectTerminalSize(),
which queries /dev/tty.

It first flushes (TCSAFLUSH) the input since a070519 (IO: disallow
input echo, 2024-07-31), which removes the input queued by mc.

Then it reads from the TTY 1024 bytes at a time.
This is also bad because it might swallow key presses
made by the user.

So maybe
1. Don't flush.
2. Reduce the likelihood of swallowing users's key presses by reading
   only one byte at a time.

In theory this might result in us reading wrong values if the user
types very quickly at startup (possibly synthesized, as is the case
with mc).
@CarterLi
Copy link
Member

Does it really solve your issue? Even if I don't flush the input, I still have to read and ignore the input before I successfully find the terminal response. The input queued by other programs will be effectively removed just like what TCSAFLUSH does.

@CarterLi
Copy link
Member

CarterLi commented Oct 17, 2025

/dev/tty is a shared resource. You can't assume that it's not touched by other programs. I don't know why do you want to write stdin. If you want to communicate with a subprocess, you should better use other IPC methods.

@CarterLi CarterLi closed this Oct 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants