Bugfix: protect the output queue from being written while another thread is flushing it.