-
-
Notifications
You must be signed in to change notification settings - Fork 9k
修复企业微信会话存档SDK生命周期管理导致的JVM崩溃问题 #3848
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
Conversation
Co-authored-by: binarywang <[email protected]>
Co-authored-by: binarywang <[email protected]>
Co-authored-by: binarywang <[email protected]>
Co-authored-by: binarywang <[email protected]>
🤖 Augment PR SummarySummary: 本PR修复企业微信会话存档 SDK 生命周期管理不当导致的 JVM 偶发崩溃(SIGSEGV)问题。 Changes:
Technical Notes: 核心思路是让框架统一管理 SDK 缓存 + 生命周期,避免业务方手动 Destroy 与内部缓存复用机制冲突。 🤖 Was this summary useful? React with 👍 or 👎 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMsgAuditServiceImpl.java
Outdated
Show resolved
Hide resolved
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMsgAuditServiceImpl.java
Outdated
Show resolved
Hide resolved
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMsgAuditServiceImpl.java
Outdated
Show resolved
Hide resolved
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpDefaultConfigImpl.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
该 PR 修复了企业微信会话存档 SDK 生命周期管理混乱导致的 JVM 崩溃问题。核心解决方案是引入引用计数机制来自动管理 SDK 生命周期,并提供新的安全 API 方法,将 SDK 管理完全封装在框架内部。
Changes:
- 在配置存储层引入引用计数机制(incrementMsgAuditSdkRefCount、decrementMsgAuditSdkRefCount、getMsgAuditSdkRefCount)
- 新增 4 个安全 API 方法,不暴露 SDK 给调用方(getChatRecords、getDecryptChatData、getChatRecordPlainText、downloadMediaFile)
- 将旧 API 标记为 @deprecated,并在注释中说明迁移路径
- 新增完整的迁移指南文档和测试用例
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 10 comments.
Show a summary per file
| File | Description |
|---|---|
| weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpConfigStorage.java | 在接口中定义引用计数方法,用于 SDK 生命周期管理 |
| weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpDefaultConfigImpl.java | 实现引用计数机制,通过 synchronized 保证线程安全,计数归零时自动销毁 SDK |
| weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpRedisConfigImpl.java | 在 Redis 配置实现中添加引用计数机制,保持与默认实现一致 |
| weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMsgAuditService.java | 定义 4 个新的安全 API 方法,将旧方法标记为 @deprecated |
| weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMsgAuditServiceImpl.java | 实现新的安全 API,使用 try-finally 模式自动管理引用计数 |
| weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpMsgAuditTest.java | 新增测试用例验证新 API 的功能 |
| docs/CP_MSG_AUDIT_SDK_SAFE_USAGE.md | 提供详细的迁移指南,包含问题背景、解决方案、使用示例和常见问题 |
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpDefaultConfigImpl.java
Show resolved
Hide resolved
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpRedisConfigImpl.java
Show resolved
Hide resolved
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMsgAuditServiceImpl.java
Outdated
Show resolved
Hide resolved
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMsgAuditServiceImpl.java
Outdated
Show resolved
Hide resolved
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMsgAuditServiceImpl.java
Outdated
Show resolved
Hide resolved
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMsgAuditServiceImpl.java
Outdated
Show resolved
Hide resolved
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMsgAuditServiceImpl.java
Outdated
Show resolved
Hide resolved
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpDefaultConfigImpl.java
Show resolved
Hide resolved
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpRedisConfigImpl.java
Show resolved
Hide resolved
|
@copilot apply changes based on the comments in this thread |
Co-authored-by: binarywang <[email protected]>
bbc138a to
30914f3
Compare
1. getChatRecords() 返回空列表而非 null,避免 NPE 2. downloadMediaFile() 正确传播文件写入异常 3. updateMsgAuditSdk() 确保旧 SDK 被销毁,避免资源泄漏
修复企业微信会话存档SDK生命周期管理导致的JVM崩溃问题
主要改进(已完成代码审查反馈)
1. 解决并发竞态条件问题
问题: 在
initSdk()和incrementMsgAuditSdkRefCount()之间存在时间窗口,可能导致引用计数操作在错误的SDK上解决方案:
acquireMsgAuditSdk()原子操作方法,在一个同步块内完成SDK获取和引用计数增加releaseMsgAuditSdk()原子操作方法,在一个同步块内完成引用计数减少和SDK销毁检查acquireSdk()和releaseSdk()辅助方法封装原子操作逻辑2. 改进SDK不匹配时的返回值
问题: 当传入的SDK与缓存的SDK不匹配时,返回0无法区分是SDK不匹配还是引用计数为0
解决方案:
incrementMsgAuditSdkRefCount()SDK不匹配时返回-1(而非0)decrementMsgAuditSdkRefCount()SDK不匹配时返回-1(而非0)getMsgAuditSdkRefCount()SDK不匹配时返回-1(而非0)3. 加强SDK销毁时的检查
问题: 在销毁SDK时需要再次检查SDK是否仍然是当前缓存的SDK
解决方案:
decrementMsgAuditSdkRefCount()和releaseMsgAuditSdk()中,销毁前再次验证this.msgAuditSdk == sdkupdateMsgAuditSdk()中,如果有旧SDK且引用计数为0,先销毁旧SDK再更新4. 新增原子操作接口
在
WxCpConfigStorage接口中新增:核心改进对比
修复前:
修复后:
技术实现细节
acquireSdk() 方法逻辑:
acquireMsgAuditSdk())initSdk()初始化releaseSdk() 方法逻辑:
releaseMsgAuditSdk()原子减少引用计数测试验证
代码审查反馈处理
影响范围
Original prompt
This section details on the original issue you should resolve
<issue_title>企业微信会话存档下载时总是时不时出现报错,导致整个JVM直接崩溃</issue_title>
<issue_description>
简要描述
企业微信会话存档下载时总是时不时出现报错,导致整个JVM直接崩溃
模块版本情况
详细描述
public List pullWeworkChatData(long seq, long limit, Predicate duplicateMessageFilter) throws Exception {
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.