generate_precompile script: try to improve reliability (#30040)
TTY objects are inherently unreliable input channels (documented, per posix design),
so try to slow down the rate of input and speed up the rate of output
by watching the output stream more closely, and detecting intermediate errors.
And also just general cleanup some of our IO handling.