Skip to content

Commit cfc0927

Browse files
authored
Merge pull request #569 from ulue/dev2
fix some error
2 parents 7aac80d + da237cb commit cfc0927

4 files changed

Lines changed: 37 additions & 19 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
}

src/websocket-server/src/Swoole/MessageListener.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use Swoole\Websocket\Server;
3232
use Throwable;
3333
use function server;
34+
use const WEBSOCKET_OPCODE_CLOSE;
3435

3536
/**
3637
* Class MessageListener
@@ -55,14 +56,22 @@ class MessageListener implements MessageInterface
5556
public function onMessage(Server $server, Frame $frame): void
5657
{
5758
$fd = $frame->fd;
58-
$sid = (string)$fd;
59+
60+
// Fix: if setting: 'open_websocket_close_frame' => true
61+
if ($frame->opcode === WEBSOCKET_OPCODE_CLOSE) {
62+
CLog::info('Close ws#%d connection by close opcode message, reason: %s', $fd, $frame->reason);
63+
// Swoft::getBean(CloseListener::class)->onClose($server, $fd, 0);
64+
// NOTICE: swoole will auto call close event.
65+
return;
66+
}
5967

6068
server()->log("Message: conn#{$fd} received message data", [], 'debug');
6169

6270
$request = Request::new($frame);
6371
$response = Response::new($fd);
6472

6573
/** @var WsMessageContext $ctx */
74+
$sid = (string)$fd;
6675
$ctx = WsMessageContext::new($request, $response);
6776

6877
// Storage context

0 commit comments

Comments
 (0)