Make sure the write operation completed before closing