Skip to content

Commit 96f8684

Browse files
binarywangCopilot
andauthored
Potential fix for pull request finding
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 8ec7cb8 commit 96f8684

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/AbstractWxCpInRedisConfigImpl.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import me.chanjar.weixin.common.redis.WxRedisOps;
77
import org.apache.commons.lang3.StringUtils;
88

9+
import java.util.concurrent.CancellationException;
910
import java.util.concurrent.TimeUnit;
1011
import java.util.concurrent.locks.Lock;
1112

@@ -127,12 +128,31 @@ public boolean isAccessTokenExpired() {
127128
return expire == null || expire < 2;
128129
} catch (Exception e) {
129130
log.warn("获取access_token过期时间时发生异常,将视为已过期以触发刷新,异常信息: {}", e.getMessage());
130-
// 清除中断标志,确保后续的锁获取和token刷新操作能够正常执行
131-
Thread.interrupted();
131+
// 仅在当前线程已中断且异常为中断相关时,才清除中断标志,避免吞掉上层的中断语义
132+
if (Thread.currentThread().isInterrupted() && isInterruptionRelated(e)) {
133+
Thread.interrupted();
134+
}
132135
return true;
133136
}
134137
}
135138

139+
/**
140+
* 判断异常及其原因链是否为中断相关异常。
141+
*
142+
* @param throwable 异常
143+
* @return 如果异常链中包含 {@link InterruptedException} 或 {@link CancellationException},返回 true;否则返回 false
144+
*/
145+
private boolean isInterruptionRelated(Throwable throwable) {
146+
Throwable current = throwable;
147+
while (current != null) {
148+
if (current instanceof InterruptedException || current instanceof CancellationException) {
149+
return true;
150+
}
151+
current = current.getCause();
152+
}
153+
return false;
154+
}
155+
136156
@Override
137157
public void updateAccessToken(WxAccessToken accessToken) {
138158
redisOps.setValue(this.accessTokenKey, accessToken.getAccessToken(), accessToken.getExpiresIn(), TimeUnit.SECONDS);

0 commit comments

Comments
 (0)