-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Fix for race condition in console output stream write #28813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -70,7 +70,11 @@ public function getStream() | |||
*/ | |||
protected function doWrite($message, $newline) | |||
{ | |||
if (false === @fwrite($this->stream, $message) || ($newline && (false === @fwrite($this->stream, PHP_EOL)))) { | |||
if ($newline) { | |||
$message .= PHP_EOL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not reassigning was done purposely to save memory (#18932)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, at least, this code avoids the concatenation when not adding a newline (while all writes where doing a concatenation before #18932 and I'm not sure PHP optimizes the concatenation of empty strings).
But I fear we cannot avoid both the extra allocation and the race condition (you could then ensure that your put the newline in your big message instead of asking the console component to add it though)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine for me.
you could then ensure that your put the newline in your big message instead of asking the console component to add it though
Perhaps this should be mentioned somewhere (docblock and/or symfony-docs?)
Thank you @rudolfratusinski. |
…olfratusinski) This PR was merged into the 2.8 branch. Discussion ---------- Fix for race condition in console output stream write | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | In high throughput environments writing `message` and `PHP_EOL` separately causes race condition issues, where messages might be written in order - message - message - EOL - EOL instead of - message - EOL - message - EOL An example below is a laravel application log tail of queue workers (multiple app instances writing to the same log file) handled by supervisor. Before:  After:  Commits ------- 428dea6 Fix for race condition in console output stream write
I thank You for a great work you do every single day! |
In high throughput environments writing
message
andPHP_EOL
separately causes race condition issues, where messages might be written in orderinstead of
An example below is a laravel application log tail of queue workers (multiple app instances writing to the same log file) handled by supervisor.
Before:

After:
