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

Fix NNGException when msg port is not ready yet #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Saroth
Copy link

@Saroth Saroth commented Mar 9, 2024

  • 代码位置
    clients/python/wcferry/client.py:enable_receiving_msg
  • 问题
    在连接消息端口时,有概率出现消息端口还没有启动监听,导致GetMessage线程出现异常:
    pynng.exceptions.NNGException: Connection shutdown
  • 解决方案
    增加循环sleep等待

@lich0821
Copy link
Owner

lich0821 commented Mar 9, 2024

感谢 PR!把连接放到线程外有好处,不过:

  1. 当出现 Exception 时,靠重试是解决不了问题的;(没启动监听这情况理论上不存在。这是同步的,发指令先启动了,再设置相应参数,然后指令响应返回,才开始去连接)
  2. 退一步说,真有可能出现异常,也应该由 enable_receiving_msg 的使用者来自行处理(再次调用、重启电脑……),它自己无法处理好。

@rehiy
Copy link
Collaborator

rehiy commented Mar 9, 2024

这里应该由消息消费者自行处理

@Saroth
Copy link
Author

Saroth commented Mar 10, 2024

感谢 PR!把连接放到线程外有好处,不过:

1. 当出现 Exception 时,靠重试是解决不了问题的;(没启动监听这情况理论上不存在。这是同步的,发指令先启动了,再设置相应参数,然后指令响应返回,才开始去连接)

2. 退一步说,真有可能出现异常,也应该由 `enable_receiving_msg` 的使用者来自行处理(再次调用、重启电脑……),它自己无法处理好。

感谢回复,关于您说的问题:

  1. 理论上确实不应该出现,但我这边也确实遇到了 :-( 。抛出pynng.exceptions.NNGException的概率超过95%。但只要sleep 0.1秒就总是可以连上了。也许是我的服务器CPU性能比较差;
  2. 此前试过再次调用enable_receiving_msg。但因为self._is_receiving_msg is True,而不会再次启动GetMessage线程。而且因为无法捕获线程中的异常,所以无法外部处理连接失败的情况;

又想了想,是可以通过有点hack的方式解决。比如从wcf.msg_socket获取连接状态,再修改wcf._is_receiving_msg,让enable_receiving_msg可以再次重试 :-)

又试了下,发送指令后到连接成功之间,需要sleep 0.004~0.006秒。性能确实不太理想,but it work

@Ca11back
Copy link

Ca11back commented Sep 7, 2024

出现相同问题,几乎每次启动都会报错NNGException,使用了PR中的方法修复后可以解决

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants