Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Process] Add support for Fiber #43678

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

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions 5 src/Symfony/Component/Process/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.1
---

* Add support for `Fiber`

6.4
---

Expand Down
23 changes: 21 additions & 2 deletions 23 src/Symfony/Component/Process/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,12 +429,31 @@ public function wait(?callable $callback = null): int
do {
$this->checkTimeout();
$running = $this->isRunning() && ('\\' === \DIRECTORY_SEPARATOR || $this->processPipes->areOpen());
$this->readPipes($running, '\\' !== \DIRECTORY_SEPARATOR || !$running);
if ($fiber = \Fiber::getCurrent()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe there should be an additional flag to enable this support otherwise it would be a bc break for people already using this method in a Fiber

$this->readPipes(false, '\\' !== \DIRECTORY_SEPARATOR || !$running);
$startedAt = microtime(true);
$fiber->suspend();
$sleepFor = (int) (1000 - (microtime(true) - $startedAt) * 1000000);
if (0 < $sleepFor) {
Comment on lines +434 to +437
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(same below)

Suggested change
$startedAt = microtime(true);
$fiber->suspend();
$sleepFor = (int) (1000 - (microtime(true) - $startedAt) * 1000000);
if (0 < $sleepFor) {
$suspendedAt = microtime(true);
$fiber->suspend();
if (0 < $sleepFor = (int) (1000 - (microtime(true) - $suspendedAt) * 1000000)) {

But I would also make readPipe return true/false whether the process did yield some activity or not. This way, we won't force a sleep while there are things to do.

usleep($sleepFor);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it could use stream_select on the pipes instead of usleep ? so it would directly read / write if there is something to do instead of sleeping ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do use stream_select already so I guess we added this to address a situation where stream_select doesn't apply. git blame could help remember :)

}
} else {
$this->readPipes($running, '\\' !== \DIRECTORY_SEPARATOR || !$running);
}
} while ($running);

while ($this->isRunning()) {
$this->checkTimeout();
usleep(1000);
if ($fiber = \Fiber::getCurrent()) {
$startedAt = microtime(true);
$fiber->suspend();
lyrixx marked this conversation as resolved.
Show resolved Hide resolved
$sleepFor = (int) (1000 - (microtime(true) - $startedAt) * 1000000);
if (0 < $sleepFor) {
usleep($sleepFor);
}
} else {
usleep(1000);
}
}

if ($this->processInformation['signaled'] && $this->processInformation['termsig'] !== $this->latestSignal) {
Expand Down
21 changes: 21 additions & 0 deletions 21 src/Symfony/Component/Process/Tests/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,27 @@ public function testMustRun()
$this->assertEquals('foo'.\PHP_EOL, $process->getOutput());
}

public function testMustRunWithFiber()
{
$process = new Process(['sleep', 1]);
$fiber = new \Fiber(function () use ($process) {
$process->mustRun();
});
$fiber->start();

$fiberHasBeenSuspended = false;
while (!$fiber->isTerminated()) {
$fiberHasBeenSuspended = true;
$this->assertTrue($fiber->isSuspended());
$fiber->resume();

usleep(900);
}

$this->assertTrue($process->isTerminated());
$this->assertTrue($fiberHasBeenSuspended);
}

public function testSuccessfulMustRunHasCorrectExitCode()
{
$process = $this->getProcess('echo foo')->mustRun();
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.