The script
Handling a pause
The loop above exits onclosed or failed. A real client also has to handle a run that stops and waits — awaiting_input is not terminal, so the loop would spin forever.
Poll the thread list instead, which tells you why it paused: