Close the temporary files before unlinking them

See https://github.com/rails/rails/pull/11725.