Use atomic writes in BladeCompiler to prevent race condition#58812
Merged
taylorotwell merged 2 commits intolaravel:12.xfrom Feb 14, 2026
Merged
Use atomic writes in BladeCompiler to prevent race condition#58812taylorotwell merged 2 commits intolaravel:12.xfrom
taylorotwell merged 2 commits intolaravel:12.xfrom
Conversation
Change File::put() to File::replace() when writing compiled Blade views. In threaded servers like FrankenPHP, concurrent requests can race on compiling the same view: file_put_contents() truncates the file to 0 bytes before writing, so another thread doing include() during that window gets empty output. This causes Livewire's insertAttributesIntoHtmlRoot() to throw RootTagMissingFromViewException because there is no root HTML tag to find. File::replace() uses a temp file + rename, which is atomic on POSIX systems, ensuring readers always see either the old or new content. This is the same class of fix applied to Livewire's CacheManager in livewire/livewire#9833.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
DarkGhostHunter
pushed a commit
to DarkGhostHunter/laravel-framework
that referenced
this pull request
Feb 22, 2026
…#58812) * Use atomic writes in BladeCompiler to prevent race condition Change File::put() to File::replace() when writing compiled Blade views. In threaded servers like FrankenPHP, concurrent requests can race on compiling the same view: file_put_contents() truncates the file to 0 bytes before writing, so another thread doing include() during that window gets empty output. This causes Livewire's insertAttributesIntoHtmlRoot() to throw RootTagMissingFromViewException because there is no root HTML tag to find. File::replace() uses a temp file + rename, which is atomic on POSIX systems, ensuring readers always see either the old or new content. This is the same class of fix applied to Livewire's CacheManager in livewire/livewire#9833. * Update BladeCompiler tests to expect replace() instead of put() Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
BladeCompiler::compile()uses$this->files->put()(which wrapsfile_put_contents()) to write compiled Blade views. In threaded PHP servers like FrankenPHP running hundreds of worker threads, concurrent first-requests can race on compiling the same view.file_put_contents()truncates the target file to 0 bytes before writing new content. If another worker thread doesincludeon that file during the truncate-write window, it receives empty output. This causes Livewire'sinsertAttributesIntoHtmlRoot()to throwRootTagMissingFromViewExceptionbecause there is no root HTML tag to find in the empty string.We observed exactly 2 of these exceptions after every Kamal deploy — one per web server — always on the first requests hitting each freshly-started container (typically from Googlebot).
Solution
Change the two
$this->files->put()calls inBladeCompiler::compile()to$this->files->replace().Filesystem::replace()writes to a temp file first and then usesrename(), which is atomic on POSIX systems. This ensures any concurrent reader always sees either the complete old content or the complete new content — never a truncated/empty file.This is the same class of fix already applied to Livewire's
CacheManagerin livewire/livewire#9833, which changedFile::put()→File::replace()to solve an identical race condition.Changes
src/Illuminate/View/Compilers/BladeCompiler.php: Changed both$this->files->put($compiledPath, $contents)calls to$this->files->replace($compiledPath, $contents)in thecompile()method.