Fix non-atomic CSV and MD writes to prevent corruption on crash (#628) (#631)