Skip to content

Commit da237cb

Browse files
committed
fix: swoft-cloud/swoft/issues/1146 add try catch for coroutine complete handle
1 parent e6428a5 commit da237cb

3 files changed

Lines changed: 27 additions & 18 deletions

File tree

src/bean/src/Container.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,9 @@ public function isSingleton(string $name): bool
592592
*/
593593
public function destroyRequest(string $id): void
594594
{
595-
unset($this->requestPool[$id]);
595+
if (isset($this->requestPool[$id])) {
596+
unset($this->requestPool[$id]);
597+
}
596598
}
597599

598600
/**
@@ -602,7 +604,9 @@ public function destroyRequest(string $id): void
602604
*/
603605
public function destroySession(string $sid): void
604606
{
605-
unset($this->sessionPool[$sid]);
607+
if (isset($this->sessionPool[$sid])) {
608+
unset($this->sessionPool[$sid]);
609+
}
606610
}
607611

608612
/**

src/bean/src/Listener/DestroyRequestListener.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ class DestroyRequestListener implements EventHandlerInterface
2222
*/
2323
public function handle(EventInterface $event): void
2424
{
25-
$id = (string)$event->getParam(0, '');
26-
if (empty($id)) {
25+
$id = $event->getParam(0, '');
26+
if (!$id) {
2727
return;
2828
}
2929

30-
BeanFactory::destroyRequest($id);
30+
BeanFactory::destroyRequest((string)$id);
3131
}
3232
}

src/framework/src/Listener/CoroutineCompleteListener.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Swoft\Event\EventInterface;
1313
use Swoft\Log\Logger;
1414
use Swoft\SwoftEvent;
15+
use Throwable;
1516
use function bean;
1617
use function sgo;
1718

@@ -47,24 +48,28 @@ public function handle(EventInterface $event): void
4748
*/
4849
private function coroutineComplete(): void
4950
{
50-
// Wait
51-
Context::getWaitGroup()->wait();
52-
5351
/* @var Logger $logger */
5452
$logger = bean('logger');
5553

56-
// Add notice log
57-
if ($logger->isEnable()) {
58-
$logger->appendNoticeLog();
59-
}
54+
try {
55+
// Wait coroutine
56+
Context::getWaitGroup()->wait();
6057

61-
// Coroutine destroy
62-
Swoft::trigger(SwoftEvent::COROUTINE_DESTROY);
58+
// Add notice log
59+
if ($logger->isEnable()) {
60+
$logger->appendNoticeLog();
61+
}
6362

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

67-
// Destroy context
68-
Context::destroy();
71+
// Destroy context
72+
Context::destroy();
73+
}
6974
}
7075
}

0 commit comments

Comments
 (0)