prevent OSError when pipes are closed
fix queue length race
fix queue length race