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

handler 对 插件实例的引用导致 scanp 命令引起内存泄漏 #2412

Closed
2 tasks done
ayasa520 opened this issue Dec 3, 2024 · 2 comments · Fixed by #2413
Closed
2 tasks done

handler 对 插件实例的引用导致 scanp 命令引起内存泄漏 #2412

ayasa520 opened this issue Dec 3, 2024 · 2 comments · Fixed by #2413

Comments

@ayasa520
Copy link
Contributor

ayasa520 commented Dec 3, 2024

前置确认

  • 我确认我运行的是最新版本的代码,并且安装了所需的依赖,在FAQS中也未找到类似问题。

⚠️ 搜索issues中是否已存在类似问题

  • 我已经搜索过issues和disscussions,没有跟我遇到的问题相关的issue

操作系统类型?

Linux

运行的python版本是?

other

使用的chatgpt-on-wechat版本是?

Latest Release

运行的channel类型是?

wx(个人微信, itchat)

复现步骤 🕹

开启管理员插件以及任意其他插件, 执行 #scanp 命令

问题描述 😯

期待行为是原本插件的实例被 gc 回收, 但是由于插件中 handler 对插件 self 的引用, 导致之前的插件实例永远不会被销毁.
执行 gc.get_referrers(self.instances[name]) 结果如下图
image

解决方法有两种:

  1. self.instances[name] = instance之前清空 self.instances[name].handlers, 以消除对插件实例的引用.
    image

  2. 使用 weakref
    在插件中添加handler时

self.handlers[event.on_handle_context] = weakref.weakmethod(self.on_handle_context)

在 emit_event 中

    def emit_event(self, e_context: EventContext, *args, **kwargs):
                    ...
                    handler = instance.handlers[e_context.event]()
                    if handler:
                        handler(e_context, *args, **kwargs)

显然方法 1 改动更少, 方法 2 需要更改现存的所有插件. 我会就方法 1 提一个 PR

终端日志 📒

无需日志

@Saboteur7
Copy link
Collaborator

Thanks for the PR, it has been merged.

@spacex-3
Copy link

Thanks for the PR, it has been merged.

大佬,同理在Event.ON_RECEIVE_MESSAGE也存在这种情况,只要每scanp一次,这个就会重复出现多一次,可否也帮忙调整下?具体怎么解决我就真的小白搞不懂了不好意思

[DEBUG][2024-12-19 09:46:05][plugin_manager.py:194] - Plugin DOUYINPLUGIN triggered by event Event.ON_RECEIVE_MESSAGE [DEBUG][2024-12-19 09:46:05][plugin_manager.py:194] - Plugin DOUYINPLUGIN triggered by event Event.ON_RECEIVE_MESSAGE [DEBUG][2024-12-19 09:46:05][plugin_manager.py:194] - Plugin DOUYINPLUGIN triggered by event Event.ON_RECEIVE_MESSAGE [DEBUG][2024-12-19 09:46:05][plugin_manager.py:194] - Plugin DOUYINPLUGIN triggered by event Event.ON_RECEIVE_MESSAGE [DEBUG][2024-12-19 09:46:05][plugin_manager.py:194] - Plugin DOUYINPLUGIN triggered by event Event.ON_RECEIVE_MESSAGE [DEBUG][2024-12-19 09:46:05][plugin_manager.py:194] - Plugin DOUYINPLUGIN triggered by event Event.ON_RECEIVE_MESSAGE

zcpisme pushed a commit to zcpisme/dify-on-wechat that referenced this issue Dec 28, 2024
zcpisme pushed a commit to zcpisme/dify-on-wechat that referenced this issue Dec 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants