From 150f5694dcc8a4c79e759ab9c7a2e40a5f2eb897 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Wed, 21 Aug 2024 12:44:17 +0100 Subject: [PATCH] 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) --- app/controllers/stylesheets_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/stylesheets_controller.rb b/app/controllers/stylesheets_controller.rb index 12a477122c5..272e1cbe251 100644 --- a/app/controllers/stylesheets_controller.rb +++ b/app/controllers/stylesheets_controller.rb @@ -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