fix race conditions around header writing