shell: better command handling to avoid deadlocks