I have done more investigation, and I now know the cause of the bash/stty problem. It appears to be a race condition in bash (well, between two different bash shells, actually). I saw a post from a while back about something similar by Ingo Molnar, so I have copied him here too.
Here is the ps tree of the test case where stty has hung: 4704 ? S 0:00 \_ xterm 4706 pts/3 Ss 0:00 | \_ -bash 4739 pts/3 S 0:00 | \_ su 4742 pts/3 S 0:00 | \_ bash 4746 pts/3 S+ 0:00 | \_ su foo 4747 pts/3 S 0:00 | \_ bash 4752 pts/3 T 0:00 | \_ stty -ixany What should happen is: when "su foo" (4746) is run, it spawns a bash shell (4747) that then makes itself the session leader when it initializes its job control. The stty command (in the child bash's .bashrc) will then be able to work (and not hang). However, the hang happens when the parent bash (4742) interferes by reverting the tty session leader back to its child (the "su foo" process: 4746) shortly after the child bash (4747) becomes the leader. The parent does this when it calls execute_command_internal()->stop_pipeline()->give_terminal_to(). This seems to happen at a slightly random time, making the issue intermittent - it depends which one wins the race. In summary, when the bug does *not* occur, here is the approximate sequence (note I am : 1) parent bash (4742) runs 'su foo' (4746) 2) parent bash sets tty leader to 'su' (4746) 3) child bash (4747) initializes and sets itself to be the leader 4) stty command in .bashrc runs successfully When the bug occurs, here is the sequence: 1) parent bash (4742) runs 'su foo' (4746) 2) child bash (4747) initializes and sets itself to be the leader 3) parent bash sets tty leader *back* to 'su' (4746) 4) stty command runs and fails/hangs because its parent is not leader The various calls to tcsetpgrp() that do this are interleaved from the two bash processes, and sometimes the parent does it slightly *after* the child bash initializes job control - that's when the problem happens. I have not looked further to find a solution (but it's a great start to know the cause...!). Any further help is welcome. -Joe