fix race condition where the stream could end before the file is processed