Process ends in case of deferred promise is used
Mindaho opened this issue · comments
Having issue where in case of deferred promise used in repeat loop, program completely exists everything. After handling single connection, I would expect it to go back to while loop, waiting for new connection, but instead it exists handleConnection
and afterwards the while loop. No exception, no errors. Only happens if deferred is used.
Loop::run(function () use ($host) {
$server = Server::listen($host, (new BindContext())->withTcpNoDelay());
while ($socket = yield $server->accept()) {
$this->handleConnection($socket);
}
}
private function handleConnection(ResourceSocket $socket): void
{
$counter = 0;
$deferred = new Deferred();
Loop::repeat(100, function ($watcherId) use (&$counter, $deferred) {
if($counter === 3) {
$deferred->resolve();
Loop::cancel($watcherId);
}
$counter++;
});
wait($deferred->promise());
return;
}
Hey @Mindaho,
the issue here is using wait
inside a running event loop, which will call Loop::stop()
internally and exit all nested event loop runs.
It's best to use Amp\call
and a generator there and return a Promise
from handleConnection
as well.
This will be much nicer in v2, where you can just await everything synchronously everywhere.