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

多个线程对 self._cli 操作导致 close 找不到 #39

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

Conversation

shijie93
Copy link

No description provided.

@YueChen-C
Copy link
Owner

self._cli 是内部变量,不建议直接修改操作。至于这个锁的使用个人感觉有点突兀,建议放在你的业务层面来做。

@shijie93
Copy link
Author

shijie93 commented Sep 1, 2021

抱歉我没有将遇到的问题描述清楚:
我是在使用py-ios-device的过程中,遇到报错

 def stop(self):

  self._running = False
  if self._recv_thread:
      self._recv_thread = None
  if self._cli:
      self._cli.close() #  NoneType has no attribute 'close'
      self._cli = None

刚开始我也挺纳闷,前一行明明做了非空判断,self._cli.close() 按理是不应该报空类型的错误,能出现此问题只有一种可能,就是多线程对类成员的访问。通读代码之后发现 self._running 是用来关闭 self.start() 启动的线程,线程函数为 _receiver,而线程函数中控制循环的正是self._running,循环退出后的 finally 也又再一次执行了 self.stop()。

所以存在一种情况,直接调用 stop 的代码在执行了 self._running = False后,线程循环结束,执行 finally,再一次调用了同一个对象的 stop方法,两个线程同时进入了 if self._cli 并且其中一个线程先执行了 self._cli = None,另一个就会出现空类型的报错

此处加锁并不一定是最好的解决方法,作者可以根据代码设计的思路进行此问题的修正

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.

2 participants