alibaba / alibaba/Sentinel

[BUG] 关于`com.alibaba.csp.sentinel.Entry`为支持`try-with-resources`特性引发的异常

Open
#3,473 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
23.1k
Forks
8.1k
PR merge metrics
No merged PRs in 30d

Description

在使用`Sentinel`(版本:`1.8.2`)时,根据`com.alibaba.csp.sentinel.Entry`支持`try-with-resources`的特性,源码如图:
![image](https://github.com/user-attachments/assets/95b12378-2907-42dc-9659-e617b5d5c173)
用户文档也特别介绍,如图:
![image](https://github.com/user-attachments/assets/8cff5aae-8df6-4d53-b110-58f6a8815373)
本人在做测试时,代码如下:
![image](https://github.com/user-attachments/assets/e1d2da86-8edb-415c-bea0-511932cab1f9)
其中第`49`行使用了`try-with-resources`语法,在第`59`行,使用`Tracer.trace(e)`跟踪错误信息,测试发现,错误数总为`0`,后经过调试发现,`try-with-resources`的`close()`方法在`Tracer.trace(e)`方法调用之前已经调用,如图:
![image](https://github.com/user-attachments/assets/191d3800-790f-4e54-b1fe-dc646023bda3)
![image](https://github.com/user-attachments/assets/8d0a4e47-e2fd-4b38-9ab2-c3cea9e49c4c)
也就是`com.alibaba.csp.sentinel.Entry`提前`exit()`了,这就导致` ContextUtil.getContext()`获取的`Context`总是为空,如图:
![image](https://github.com/user-attachments/assets/c76e4617-5341-4aa2-80cc-92c29518d443)
因此也就无法记录到错误,如图:
![image](https://github.com/user-attachments/assets/fab62a14-3238-4f7d-bd02-1533679a1453)
后来看了一下官方`web`(`com.alibaba.csp.sentinel.adapter.spring.webmvc.AbstractSentinelInterceptor#preHandle`)和非`web`(`com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect#invokeResourceWithSentinel`)的两个处理类,也都没有用`try-with-resources`特性(你们这群老六😅),如图:
![image](https://github.com/user-attachments/assets/1d21a1fa-70f3-41f3-99a5-32f2ee07934e)
最后只能采用普通的`try-catch-finally`,错误数统计才正常,请后续修复一下代码!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.