Fix write.close() returning before all data is flushed on disk