FIX: Write stylesheet cache atomically (#28457)

In some situations, the filesystem cache will be read and persisted to the database. If the file being read is still being written, then that can lead to empty/partial caches being stored in the database.

This commit ensures that cannot happen by switching to our `atomic_write_file` helper (which writes to a temp file, and then does an atomic `mv` operation to move it to the destination)
This commit is contained in:
David Taylor 2024-08-21 12:44:17 +01:00 committed by GitHub
parent 9dd47ca755
commit 150f5694dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -71,7 +71,7 @@ class StylesheetsController < ApplicationController
unless File.exist?(location)
if current = query.pick(source_map ? :source_map : :content)
FileUtils.mkdir_p(cache_path)
File.write(location, current)
Discourse::Utils.atomic_write_file(location, current)
else
raise Discourse::NotFound
end