<div dir="ltr">Found this out today when comparing captured logs. Every time I connect to a pty the first few hundreds of bytes seem to always be missing.<div>I then traced to this line <a href="https://github.com/landley/toybox/blob/master/toys/net/microcom.c#L103">https://github.com/landley/toybox/blob/master/toys/net/microcom.c#L103</a> and it's the tcsetattr(TCSAFLUSH) that is discarding buffered input data. Something like this is happening:</div><div><br></div><div>1. (process 1) opens and write "xxxxyyyy" to the ptmx end. The data is not immediately read by another process, so it's buffered somewhere. (in the pty driver?)</div><div>2. (process 2 / microcom) opens and flushes (TCSAFLUSH) the pts end, and then start polling data. Receives "yyyy". Part of the buffered input data were flushed and discarded.</div><div><br></div><div>Man page says (<a href="https://man7.org/linux/man-pages/man3/termios.3.html">https://man7.org/linux/man-pages/man3/termios.3.html</a>): </div><div><br>       TCSANOW<br>              the change occurs immediately.<br><br>       TCSADRAIN<br>              the change occurs after all output written to fd has been<br>              transmitted.  This option should be used when changing<br>              parameters that affect output.<br><br>       TCSAFLUSH<br>              the change occurs after all output written to the object<br>              referred by fd has been transmitted, and all input that<br>              has been received but not read will be discarded before<br>              the change is made.<br></div><div><br></div><div>Is there any particular reason to use TCSAFLUSH here?</div><div>If not, can we change to TCSADRAIN or TCSANOW. I don't think there is good reason to _discard received data_ just to set the terminal mode...? Is there really a real world case that the device termios is so dirty that all data, from before setting raw mode, must be discarded?<br></div><div><br></div><div>I also tried to modify the microcom code to skip tcsetattr() if the device termios is already equal to the mode we are setting it.</div><div>`if (old_termios != new_termios) tcsetattr(new_termios, TCSAFLUSH)`</div><div>However this doesn't work because microcom always tries to set the device baud. For example a pty device might be configured to use buad 38400, but microcom would want it to be 115200, thus flushing it's data. but pty doesn't really care about the baud most of the time AFAIK, so flushing data in this case just seems disruptive to the user experience.</div><div></div></div>