Skip to content

Commit

Permalink
fix: swoft-cloud/swoft/issues/1146 add try catch for coroutine comple…
Browse files Browse the repository at this point in the history
…te handle
  • Loading branch information
inhere committed Apr 13, 2020
1 parent e6428a5 commit da237cb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
8 changes: 6 additions & 2 deletions src/bean/src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,9 @@ public function isSingleton(string $name): bool
*/
public function destroyRequest(string $id): void
{
unset($this->requestPool[$id]);
if (isset($this->requestPool[$id])) {
unset($this->requestPool[$id]);
}
}

/**
Expand All @@ -602,7 +604,9 @@ public function destroyRequest(string $id): void
*/
public function destroySession(string $sid): void
{
unset($this->sessionPool[$sid]);
if (isset($this->sessionPool[$sid])) {
unset($this->sessionPool[$sid]);
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/bean/src/Listener/DestroyRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class DestroyRequestListener implements EventHandlerInterface
*/
public function handle(EventInterface $event): void
{
$id = (string)$event->getParam(0, '');
if (empty($id)) {
$id = $event->getParam(0, '');
if (!$id) {
return;
}

BeanFactory::destroyRequest($id);
BeanFactory::destroyRequest((string)$id);
}
}
31 changes: 18 additions & 13 deletions src/framework/src/Listener/CoroutineCompleteListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Swoft\Event\EventInterface;
use Swoft\Log\Logger;
use Swoft\SwoftEvent;
use Throwable;
use function bean;
use function sgo;

Expand Down Expand Up @@ -47,24 +48,28 @@ public function handle(EventInterface $event): void
*/
private function coroutineComplete(): void
{
// Wait
Context::getWaitGroup()->wait();

/* @var Logger $logger */
$logger = bean('logger');

// Add notice log
if ($logger->isEnable()) {
$logger->appendNoticeLog();
}
try {
// Wait coroutine
Context::getWaitGroup()->wait();

// Coroutine destroy
Swoft::trigger(SwoftEvent::COROUTINE_DESTROY);
// Add notice log
if ($logger->isEnable()) {
$logger->appendNoticeLog();
}

// Destroy request bean
Swoft::trigger(BeanEvent::DESTROY_REQUEST, $this, Co::tid());
// Coroutine destroy
Swoft::trigger(SwoftEvent::COROUTINE_DESTROY);
} catch (Throwable $e) {
$logger->error('run coroutine complete handle error: ' . $e->getMessage());
} finally { // Use finally ensure context destroy
// Destroy request bean
Swoft::trigger(BeanEvent::DESTROY_REQUEST, $this, Co::tid());

// Destroy context
Context::destroy();
// Destroy context
Context::destroy();
}
}
}

0 comments on commit da237cb

Please sign in to comment.