Fix occasional crashing when writing logs