Add locking before writing the file