Skip to content

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
{
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.

7 participants