Skip to content

Commit 72b2e03

Browse files
committed
minor #15511 [DependencyInjection] Minor - add note that file_put_contents is non-atomic (trakos)
This PR was merged into the 5.4 branch. Discussion ---------- [DependencyInjection] Minor - add note that file_put_contents is non-atomic I have minor gripe with article at https://symfony.com/doc/current/components/dependency_injection/compilation.html#dumping-the-configuration-for-performance While the final snippet is fine, there are some examples along the way that suggest using `file_put_contents` to write cached container, without using `LOCK_EX`. I think that could produce some issues in production environment. With multiple concurrent requests, one might partially write the file, while other could try to require it before it is ready. Maybe it's worth adding a note as suggested in this MR, to make readers aware of this fact? Commits ------- ddb3b80 Note that file_put_contents is non-atomic
2 parents d7bf00a + ddb3b80 commit 72b2e03

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

components/dependency_injection/compilation.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,14 @@ serves at dumping the compiled container::
468468
file_put_contents($file, $dumper->dump());
469469
}
470470

471+
472+
.. tip::
473+
474+
Call to `file_put_contents` is not atomic. When generating container in
475+
a production environment with multiple concurrent requests, use `dumpFile`
476+
from `component-filesystem` instead. This generates file in tmp and moves it
477+
to its destination only once it's fully written to.
478+
471479
``ProjectServiceContainer`` is the default name given to the dumped container
472480
class. However, you can change this with the ``class`` option when you
473481
dump it::
@@ -559,6 +567,11 @@ for these resources and use them as metadata for the cache::
559567

560568
require_once $file;
561569
$container = new MyCachedContainer();
570+
571+
.. note::
572+
573+
Using `$containerConfigCache->write` also makes sure that
574+
the file write operation is atomic.
562575

563576
Now the cached dumped container is used regardless of whether debug mode
564577
is on or not. The difference is that the ``ConfigCache`` is set to debug

0 commit comments

Comments
 (0)