Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

一个很奇怪的bug,nonebot2卡住 #129

Closed
anaivebird opened this issue Dec 31, 2020 · 23 comments
Closed

一个很奇怪的bug,nonebot2卡住 #129

anaivebird opened this issue Dec 31, 2020 · 23 comments
Labels
question Further information is requested

Comments

@anaivebird
Copy link

anaivebird commented Dec 31, 2020

描述问题:

运行了几周了都正常,刚才突然卡住
console上不动了,我加的log也不增加了
但是bot收到新消息,oicq还会显示新的消息(正常),nonebot2就没反应
OICQ那边没有也显示ws断开,说明还连着

我设置的以下日志卡住后都不动了:
logger.add("critical.log",
encoding="utf-8",
rotation="00:00",
diagnose=False,
level="CRITICAL",
format=default_format)

logger.add("info.log",
encoding="utf-8",
rotation="00:00",
diagnose=False,
level="INFO",
format=default_format)

重新启动nonebot,之前卡住累计的信息会一次性发出,
但过一会儿又卡住了,是什么原因呢。

日志如下:
/home/myuser/.pm2/logs/nonebot2-out.log last 15 lines:
1|nonebot2 | 12-31 23:57:38 [DEBUG] nonebot | Stop event propagation
1|nonebot2 | 12-31 23:57:55 [INFO] nonebot | CQHTTP | 2469785829 [message.group.normal]: Message Klxxu5cTd9gAANvRYPbcxF/t9QMB from 2534635480@[群:710731498]: "我要吃鸡号"
1|nonebot2 | 12-31 23:57:55 [DEBUG] nonebot | Checking for matchers in priority 1...
1|nonebot2 | 12-31 23:57:55 [DEBUG] nonebot | Checking for matchers in priority 5...
1|nonebot2 | 12-31 23:57:55 [INFO] nonebot | Event will be handled by <Matcher from shutup, type=message, priority=5, temp=False>
1|nonebot2 | 12-31 23:57:55 [DEBUG] nonebot | Running matcher <Matcher from shutup, type=message, priority=5, temp=False>
1|nonebot2 | 12-31 23:57:55 [INFO] nonebot | Matcher <Matcher from shutup, type=message, priority=5, temp=False> running complete
1|nonebot2 | 12-31 23:57:55 [DEBUG] nonebot | Stop event propagation
1|nonebot2 | 12-31 23:58:26 [INFO] nonebot | CQHTTP | 2469785829 [message.group.anonymous]: Message Jzeo7QTEtAAAAHB3dFaE3F/t9SIB from 80000000@[群:657359249]: "杨桃有点东西"
1|nonebot2 | 12-31 23:58:26 [DEBUG] nonebot | Checking for matchers in priority 1...
1|nonebot2 | 12-31 23:58:26 [DEBUG] nonebot | Checking for matchers in priority 5...
1|nonebot2 | 12-31 23:58:26 [INFO] nonebot | Event will be handled by <Matcher from shutup, type=message, priority=5, temp=False>
1|nonebot2 | 12-31 23:58:26 [DEBUG] nonebot | Running matcher <Matcher from shutup, type=message, priority=5, temp=False>
1|nonebot2 | 12-31 23:58:26 [CRITICAL] shutup | 啊这 呜呜呜
1|nonebot2 | 青枣HP变为1

/home/myuser/.pm2/logs/oicq-out.log last 15 lines:
0|oicq | [2021-01-01T01:08:47.174] [INFO] [BOT:2469785829] - recv from: [Group: xxxxx(657359249), Member: 匿名消息(80000000)] ?
0|oicq | [2021-01-01T01:08:49.896] [INFO] [BOT:2469785829] - recv from: [Group: xxxxx(657359249), Member: 匿名消息(80000000)] 你不是几个小时前发过了吗
0|oicq | [2021-01-01T01:09:22.385] [INFO] [BOT:2469785829] - recv from: [Group: xxxxx(657359249), Member: 匿名消息(80000000)] 噢
0|oicq | [2021-01-01T01:09:31.048] [INFO] [BOT:2469785829] - recv from: [Group: xxxxx(657359249), Member: 匿名消息(80000000)] 我脑瘫
0|oicq | [2021-01-01T01:10:02.100] [INFO] [BOT:2469785829] - recv from: [Group: xxxxx(657359249), Member: 匿名消息(80000000)] 九头驸马有点东西
0|oicq | [2021-01-01T01:11:43.683] [INFO] [BOT:2469785829] - recv from: [Group: xxxxx(657359249), Member: 匿名消息(80000000)] 马上上
0|oicq | [2021-01-01T01:12:43.710] [INFO] [BOT:2469785829] - recv from: [Group: xxxxx(657359249), Member: 匿名消息(80000000)] [CQ:reply,id=Jzeo7QTEtAAAAHGRZK0iDV/uBaEB]?
0|oicq | [2021-01-01T01:13:14.205] [INFO] [BOT:2469785829] - recv from: [Group: xxxxx(657359249), Member: 匿名消息(80000000)] 您是?
0|oicq | [2021-01-01T01:14:25.105] [INFO] [BOT:2469785829] - recv from: [Group: xxxxx(657359249), Member: 匿名消息(80000000)] [CQ:reply,id=Jzeo7QTEtAAAAHGWQVw3ml/uBosB]别管我,我脑瘫
0|oicq | [2021-01-01T01:14:49.301] [INFO] [BOT:2469785829] - recv from: [Group: xxxxx(657359249), Member: 匿名消息(80000000)] 警告,咱们还是接着聊吧
0|oicq | [2021-01-01T01:15:02.429] [INFO] [BOT:2469785829] - recv from: [Group: xxxxx(657359249), Member: 匿名消息(80000000)] 鼍龙怪有点东西
0|oicq | [2021-01-01T01:15:09.606] [INFO] [BOT:2469785829] - recv from: [Group: xxxxx(657359249), Member: 匿名消息(80000000)] bot好像关了
0|oicq | [2021-01-01T01:16:28.344] [INFO] [BOT:2469785829] - recv from: [Group: xxxxx(657359249), Member: 匿名消息(80000000)] 要不xdm众筹下把bot放服务器上跑?

看oicq日志最后一条的时间是最新的,而nonebot2就卡在卡住的时间之后就不动了

如何复现?
之前好的,现在就这样了,触发条件不明

环境信息:

  • OS: [Centos7]
  • Python Version: [3.8]
  • Nonebot Version: [2.0.0]
@anaivebird anaivebird added the bug Something isn't working label Dec 31, 2020
@anaivebird
Copy link
Author

anaivebird commented Dec 31, 2020

找到错误的日志了:1|nonebot2 | ESC[33mESC[1mTraceback (most recent call last):ESC[0m
1|nonebot2 | File "", line 1, in
1|nonebot2 | File "/usr/local/python3/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main
1|nonebot2 | exitcode = _main(fd, parent_sentinel)
1|nonebot2 | File "/usr/local/python3/lib/python3.8/multiprocessing/spawn.py", line 129, in _main
1|nonebot2 | return self._bootstrap(parent_sentinel)
1|nonebot2 | File "/usr/local/python3/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
1|nonebot2 | self.run()
1|nonebot2 | File "/usr/local/python3/lib/python3.8/multiprocessing/process.py", line 108, in run
1|nonebot2 | self.target(*self.args, **self.kwargs)
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/uvicorn/subprocess.py", line 62, in subprocess_started
1|nonebot2 | target(sockets=sockets)
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/uvicorn/main.py", line 390, in run
1|nonebot2 | loop.run_until_complete(self.serve(sockets=sockets))
1|nonebot2 | > File "/home/anelderly/.local/lib/python3.8/site-packages/nonebot/message.py", line 164, in run_matcher
1|nonebot2 | await matcher.run(bot, event, state)
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/nonebot/matcher.py", line 419, in run
1|nonebot2 | await handler(bot, event, self.state)
1|nonebot2 | File "ESC[32mmynbrobot/plugins/shutup/ESC[0mESC[32mESC[1m__init
.pyESC[0m", line ESC[33m264ESC[0m, in ESC[35mhandle_first_receiveESC[0m
1|nonebot2 | ESC[35mESC[1mawaitESC[0m ESC[1mhurtHPandBanIfNeededESC[0mESC[1m(ESC[0mESC[1mbotESC[0mESC[1m,ESC[0m ESC[1msenderIdESC[0mESC[1m,ESC[0m ESC[34mESC[1m2ESC[0mESC[1m,ESC[0m ESC[36m'为实名、无效或bot启动后未说过话的用户'ESC[0mESC[1m)ESC[0m
1|nonebot2 | File "ESC[32mmynbrobot/plugins/shutup/ESC[0mESC[32mESC[1m__init
.pyESC[0m", line ESC[33m115ESC[0m, in ESC[35mhurtHPandBanIfNeededESC[0m
1|nonebot2 | ESC[35mESC[1mawaitESC[0m ESC[1mbotESC[0mESC[35mESC[1m.ESC[0mESC[1msetGroupAnonymousBanESC[0mESC[1m(ESC[0mESC[1mgroup_idESC[0mESC[35mESC[1m=ESC[0mESC[1mshutup_group_idESC[0mESC[1m,ESC[0m ESC[1mflagESC[0mESC[35mESC[1m=ESC[0mESC[1mbannedUserFlagESC[0mESC[1m,ESC[0m ESC[1mdurationESC[0mESC[35mESC[1m=ESC[0mESC[1mban_durationESC[0mESC[1m)ESC[0m
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/nonebot/adapters/cqhttp/bot.py", line 342, in call_api
1|nonebot2 | await self.websocket.send({
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/nonebot/drivers/fastapi.py", line 256, in send
1|nonebot2 | await self.websocket.send({"type": "websocket.send", "text": text})
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/starlette/websockets.py", line 68, in send
1|nonebot2 | await self._send(message)
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 225, in asgi_send
1|nonebot2 | raise RuntimeError(msg % message_type)

@yanyongyu
Copy link
Member

注意不要在handler中进行过于耗时同步操作,甚至阻塞或者死锁。耗时操作应使用异步或后台执行。

@yanyongyu
Copy link
Member

|nonebot2 | raise RuntimeError(msg % message_type)

这里的报错是什么

@bytemain
Copy link
Member

bytemain commented Jan 1, 2021

帮你处理一下

sed "s/ESC\[[0-9;]*m//g" 1.txt

结果:

1|nonebot2 | File "", line 1, in
1|nonebot2 | File "/usr/local/python3/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main
1|nonebot2 | exitcode = _main(fd, parent_sentinel)
1|nonebot2 | File "/usr/local/python3/lib/python3.8/multiprocessing/spawn.py", line 129, in _main
1|nonebot2 | return self._bootstrap(parent_sentinel)
1|nonebot2 | File "/usr/local/python3/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
1|nonebot2 | self.run()
1|nonebot2 | File "/usr/local/python3/lib/python3.8/multiprocessing/process.py", line 108, in run
1|nonebot2 | self.target(*self.args, **self.kwargs)
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/uvicorn/subprocess.py", line 62, in subprocess_started
1|nonebot2 | target(sockets=sockets)
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/uvicorn/main.py", line 390, in run
1|nonebot2 | loop.run_until_complete(self.serve(sockets=sockets))
1|nonebot2 | > File "/home/anelderly/.local/lib/python3.8/site-packages/nonebot/message.py", line 164, in run_matcher
1|nonebot2 | await matcher.run(bot, event, state)
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/nonebot/matcher.py", line 419, in run
1|nonebot2 | await handler(bot, event, self.state)
1|nonebot2 | File "mynbrobot/plugins/shutup/__init.py", line 264, in handle_first_receive
1|nonebot2 | await hurtHPandBanIfNeeded(bot, senderId, 2, '为实名、无效或bot启动后未说过话的用户')
1|nonebot2 | File "mynbrobot/plugins/shutup/__init.py", line 115, in hurtHPandBanIfNeeded
1|nonebot2 | await bot.setGroupAnonymousBan(group_id=shutup_group_id, flag=bannedUserFlag, duration=ban_duration)
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/nonebot/adapters/cqhttp/bot.py", line 342, in call_api
1|nonebot2 | await self.websocket.send({
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/nonebot/drivers/fastapi.py", line 256, in send
1|nonebot2 | await self.websocket.send({"type": "websocket.send", "text": text})
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/starlette/websockets.py", line 68, in send
1|nonebot2 | await self._send(message)
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 225, in asgi_send
1|nonebot2 | raise RuntimeError(msg % message_type)

@yanyongyu
Copy link
Member

yanyongyu commented Jan 2, 2021

await bot.setGroupAnonymousBan(group_id=shutup_group_id, flag=bannedUserFlag, duration=ban_duration)

cqhttp 似乎并没有这个驼峰API,只有蛇形命名的API

@bytemain
Copy link
Member

bytemain commented Jan 2, 2021

pip list 给一下 安装的 uvicorn 版本

这个报错看起来是从 uvicorn 里报出来的

@yanyongyu yanyongyu removed the bug Something isn't working label Jan 2, 2021
@anaivebird
Copy link
Author

pip list 给一下 安装的 uvicorn 版本

这个报错看起来是从 uvicorn 里报出来的

uvicorn 0.11.8

@anaivebird
Copy link
Author

await bot.setGroupAnonymousBan(group_id=shutup_group_id, flag=bannedUserFlag, duration=ban_duration)

cqhttp 似乎并没有这个驼峰API,只有蛇形命名的API

我调用的是OICQ的,它是驼峰的,是可以调用成功的,一天调用几千次都没问题,就是最近经常nonebot2卡住

@bytemain
Copy link
Member

bytemain commented Jan 2, 2021

没有更多日志以及其他的 issue 反馈,需要更多信息

翻了一下 uvicorn 的更新记录,没发现有类似的问题。
不过建议更新下 uvicorn 的版本试试吧

@bytemain
Copy link
Member

bytemain commented Jan 2, 2021

之前的报错也没贴全,希望能能多贴一点

@anaivebird
Copy link
Author

之前的报错也没贴全,希望能能多贴一点

现在启动不了了,一直是这个问题,想问下我之前发的这个报错日志是从哪里看的
现在忘了从哪里找的,控制台输出卡死之后就没了,没有报错

1|nonebot2 | File "", line 1, in
1|nonebot2 | File "/usr/local/python3/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main
1|nonebot2 | exitcode = _main(fd, parent_sentinel)
1|nonebot2 | File "/usr/local/python3/lib/python3.8/multiprocessing/spawn.py", line 129, in _main
1|nonebot2 | return self._bootstrap(parent_sentinel)
1|nonebot2 | File "/usr/local/python3/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
1|nonebot2 | self.run()
1|nonebot2 | File "/usr/local/python3/lib/python3.8/multiprocessing/process.py", line 108, in run
1|nonebot2 | self.target(*self.args, **self.kwargs)
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/uvicorn/subprocess.py", line 62, in subprocess_started
1|nonebot2 | target(sockets=sockets)
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/uvicorn/main.py", line 390, in run
1|nonebot2 | loop.run_until_complete(self.serve(sockets=sockets))
1|nonebot2 | > File "/home/anelderly/.local/lib/python3.8/site-packages/nonebot/message.py", line 164, in run_matcher
1|nonebot2 | await matcher.run(bot, event, state)
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/nonebot/matcher.py", line 419, in run
1|nonebot2 | await handler(bot, event, self.state)
1|nonebot2 | File "mynbrobot/plugins/shutup/__init.py", line 264, in handle_first_receive
1|nonebot2 | await hurtHPandBanIfNeeded(bot, senderId, 2, '为实名、无效或bot启动后未说过话的用户')
1|nonebot2 | File "mynbrobot/plugins/shutup/__init.py", line 115, in hurtHPandBanIfNeeded
1|nonebot2 | await bot.setGroupAnonymousBan(group_id=shutup_group_id, flag=bannedUserFlag, duration=ban_duration)
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/nonebot/adapters/cqhttp/bot.py", line 342, in call_api
1|nonebot2 | await self.websocket.send({
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/nonebot/drivers/fastapi.py", line 256, in send
1|nonebot2 | await self.websocket.send({"type": "websocket.send", "text": text})
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/starlette/websockets.py", line 68, in send
1|nonebot2 | await self._send(message)
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 225, in asgi_send
1|nonebot2 | raise RuntimeError(msg % message_type)

@bytemain
Copy link
Member

bytemain commented Jan 2, 2021

image

是你说的这个文件吗?

@anaivebird
Copy link
Author

谢谢大家了,没想到这么多人回复,我先升级uvicorn了,看看这两天会不会有问题

@anaivebird
Copy link
Author

anaivebird commented Jan 2, 2021

这个nonebot2-out.log就是运行nonebot2的shell上的输出,这里面bot卡住之后就没显示了,所以看不到报错

那天我猜就是在nonebot2-out.log上看到的这些报错,但这个是偶发的现象,我这里遇到的卡住,大多数都没有报错,直接卡住了,nonebot2-out.log的最后一行是正常的消息
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/starlette/websockets.py", line 68, in send
1|nonebot2 | await self._send(message)
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 225, in asgi_send
1|nonebot2 | raise RuntimeError(msg % message_type)

@anaivebird
Copy link
Author

升级最新版uvicorn还是会卡住

@yanyongyu
Copy link
Member

1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/starlette/websockets.py", line 68, in send
1|nonebot2 | await self._send(message)
1|nonebot2 | File "/home/anelderly/.local/lib/python3.8/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 225, in asgi_send
1|nonebot2 | raise RuntimeError(msg % message_type)

这里报错的信息是什么,应该在traceback上方

RuntimeError(msg % message_type)

@anaivebird
Copy link
Author

我现在差不多知道了,pm2运行的时候,log里面不会显示python的一些runtime error
我的pm2配置如下,原因不明
{
name : "nonebot2",
cwd : "/home/anelderly/botStable/mynbrobot/",
script : "/home/anelderly/botStable/mynbrobot/bot.py",
interpreter: "python3",
}

@anaivebird
Copy link
Author

不知道在handle_first_receive函数中,调用requests.get会不会导致这个问题,我怀疑是这个导致的,现在换成了http3.AsyncClient()

@anaivebird
Copy link
Author

225, in asgi_send

RuntimeError Unexpected ASGI message 'websocket.send', after sending 'websocket.close'

@anaivebird
Copy link
Author

这三天没出问题了,在handle_first_receive函数中,最好不要调用requests.get,我怀疑是这个导致的,现在换成了http3.AsyncClient()几天没卡住了

@yanyongyu
Copy link
Member

那就先close了

@bytemain
Copy link
Member

也许可以把这个issue 作为备注加到文档里? @yanyongyu

@yanyongyu
Copy link
Member

也许可以把这个issue 作为备注加到文档里? @yanyongyu

文档里已经有提示了

@mnixry mnixry added the question Further information is requested label Jan 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Development

No branches or pull requests

4 participants