mamoe / mirai

高效率 QQ 机器人支持库
https://mirai.mamoe.net
GNU Affero General Public License v3.0
14.38k stars 2.53k forks source link

Bot.close后再join貌似会抛异常 #1347

Closed Alceatraz closed 3 years ago

Alceatraz commented 3 years ago

Zulu java 11 Mirai 266

我把bot的两个logger分辨叫做MiraiBot和MiraiNet

configuration.setBotLoggerSupplier(botInstance -> new LoggerX("MiraiBot"));
configuration.setNetworkLoggerSupplier(botInstance -> new LoggerX("MiraiNet"));

启动时注册了:

userMessageEventListener = GlobalEventChannel.INSTANCE.subscribeAlways(UserMessageEvent.class, this::handleUsersMessage);
groupMessageEventListener = GlobalEventChannel.INSTANCE.subscribeAlways(GroupMessageEvent.class, this::handleGroupMessage);

关闭时会执行以下代码:

logger.hint("关闭路由系统");
userMessageEventListener.complete();
groupMessageEventListener.complete();
logger.info("等待Mirai关闭");
bot.close();
bot.join();
logger.hint("关闭核心系统");
// ..... ......
System.out.println("[FurryBlack][MAIN]FurryBlackPlus closed, Bye.");

在IDE中大概率出现的效果是正常关闭:

[2021-06-18 14:07:36][HINT][Driver]执行关闭流程
[2021-06-18 14:07:36][HINT][Driver]关闭路由系统
[2021-06-18 14:07:36][INFO][Systemd]等待Mirai关闭
[2021-06-18 14:07:36][VERB][MiraiNet]Send: StatSvc.register
[2021-06-18 14:07:36][INFO][MiraiBot]Bot cancelled: Job was cancelled
[2021-06-18 14:07:36][HINT][Driver]关闭核心系统
[FurryBlack][MAIN]FurryBlackPlus closed, Bye.
>> JVM Exit

但是在生产和某些时候的IDE中出现:

[2021-06-18 14:11:47][INFO][Driver]执行关闭流程
[2021-06-18 14:11:47][HINT][Driver]关闭路由系统
[2021-06-18 14:11:47][INFO][Systemd]等待Mirai关闭
[2021-06-18 14:11:47][VERB][MiraiNet]Send: StatSvc.register
[2021-06-18 14:11:47][VERB][MiraiNet]Channel closed.
[2021-06-18 14:11:47][INFO][MiraiBot]Bot is closed manually 
没有异常
[2021-06-18 14:11:47][INFO][MiraiBot]Bot cancelled: Job was cancelled
[2021-06-18 14:11:47][HINT][Driver]关闭核心系统
[2021-06-18 14:11:47][WARN][MiraiBot]Event processing: An exception occurred but no CoroutineExceptionHandler found, 
                        either in coroutineContext from Handler job, or in subscriberContext
kotlinx.coroutines.JobCancellationException: Job was cancelled; job=SupervisorJobImpl{Cancelled}@69dbf23c

[FurryBlack][MAIN]FurryBlackPlus closed, Bye.

>> JVM Exit

然后我改成

bot.closeAndJoin(null);

貌似就没再出过问题

推测是执行join时已经关闭了导致报错?

Him188 commented 3 years ago

JobCancellationException 属于正常情况

Alceatraz commented 3 years ago

JobCancellationException 属于正常情况 有优雅的办法消音吗,全程完美就最后爆一个没拦截住的exception强迫症犯了(

Him188 commented 3 years ago

join的时候catch

Him188 commented 3 years ago

由于 deferred.await 等协程库的 API 也会抛出 CancellationException,及时 mirai 内部都正确处理了,但我仍然担心用户会在 Bot 协程域启动协程并没有捕获 await 的 CE 而直接导致难以调试的情况,就没有过滤 CE。

Him188 commented 3 years ago

...我发现这个是 2.6.6,由于网络架构已经完全修改,现在有可能不会再有 CE 发生,请在 2.7-M1 测试