Skip to content
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

Fix for race condition in console output stream write #28813

Merged
merged 1 commit into from Oct 14, 2018
Merged

Fix for race condition in console output stream write #28813

merged 1 commit into from Oct 14, 2018

Conversation

rudolfratusinski
Copy link
Contributor

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:
ezgif-1-77388f9210cf

After:
ezgif-1-0b839d642644

@chalasr chalasr added this to the 2.8 milestone Oct 11, 2018
@@ -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;
Copy link
Member

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)

Copy link
Member

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)

Copy link
Member

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?)

@nicolas-grekas
Copy link
Member

Thank you @rudolfratusinski.

@nicolas-grekas nicolas-grekas merged commit 428dea6 into symfony:2.8 Oct 14, 2018
nicolas-grekas added a commit that referenced this pull request Oct 14, 2018
…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:
![ezgif-1-77388f9210cf](https://user-images.githubusercontent.com/2752769/46792349-bec13180-cd75-11e8-8f91-92f05762f964.gif)

After:
![ezgif-1-0b839d642644](https://user-images.githubusercontent.com/2752769/46792420-e617fe80-cd75-11e8-9414-4bfc85d9c569.gif)

Commits
-------

428dea6 Fix for race condition in console output stream write
@rudolfratusinski
Copy link
Contributor Author

Thank you @rudolfratusinski.

I thank You for a great work you do every single day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants