diff --git a/core/src/main/java/com/taobao/arthas/core/advisor/AdviceListenerAdapter.java b/core/src/main/java/com/taobao/arthas/core/advisor/AdviceListenerAdapter.java index 724414d4373..d8b893f2328 100644 --- a/core/src/main/java/com/taobao/arthas/core/advisor/AdviceListenerAdapter.java +++ b/core/src/main/java/com/taobao/arthas/core/advisor/AdviceListenerAdapter.java @@ -131,7 +131,9 @@ protected Object getExpressionResult(String express, Advice advice, double cost) * @return true 如果超过或者达到了上限 */ protected boolean isLimitExceeded(int limit, int currentTimes) { - return currentTimes >= limit; + //正常来讲,只需要判断currentTimes == limit即可,currentTimes如果是准确的(不能使用process.times().get()), + //那么不会出现自增时跳过limit的问题,为了应对未知的极端情况,仍旧加上currentTimes - limit > 100,以防万一 + return limit <= 0 || currentTimes == limit || currentTimes - limit > 100; } /** diff --git a/core/src/main/java/com/taobao/arthas/core/command/monitor200/AbstractTraceAdviceListener.java b/core/src/main/java/com/taobao/arthas/core/command/monitor200/AbstractTraceAdviceListener.java index e9eaee2f2bb..e6aac616a2f 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/monitor200/AbstractTraceAdviceListener.java +++ b/core/src/main/java/com/taobao/arthas/core/command/monitor200/AbstractTraceAdviceListener.java @@ -93,13 +93,12 @@ private void finishing(ClassLoader loader, Advice advice) { } if (conditionResult) { // 满足输出条件 - process.times().incrementAndGet(); // TODO: concurrency issues for process.write process.appendResult(traceEntity.getModel()); + int times = process.times().incrementAndGet(); // 是否到达数量限制 - if (isLimitExceeded(command.getNumberOfLimit(), process.times().get())) { - // TODO: concurrency issue to abort process + if (isLimitExceeded(command.getNumberOfLimit(), times)) { abortProcess(process, command.getNumberOfLimit()); } } diff --git a/core/src/main/java/com/taobao/arthas/core/command/monitor200/StackAdviceListener.java b/core/src/main/java/com/taobao/arthas/core/command/monitor200/StackAdviceListener.java index 5e58bdb277a..7f91a1d51e5 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/monitor200/StackAdviceListener.java +++ b/core/src/main/java/com/taobao/arthas/core/command/monitor200/StackAdviceListener.java @@ -63,8 +63,8 @@ private void finishing(Advice advice) { StackModel stackModel = ThreadUtil.getThreadStackModel(advice.getLoader(), Thread.currentThread()); stackModel.setTs(LocalDateTime.now()); process.appendResult(stackModel); - process.times().incrementAndGet(); - if (isLimitExceeded(command.getNumberOfLimit(), process.times().get())) { + int times = process.times().incrementAndGet(); + if (isLimitExceeded(command.getNumberOfLimit(), times)) { abortProcess(process, command.getNumberOfLimit()); } } diff --git a/core/src/main/java/com/taobao/arthas/core/command/monitor200/TimeTunnelAdviceListener.java b/core/src/main/java/com/taobao/arthas/core/command/monitor200/TimeTunnelAdviceListener.java index 6c9faf7084f..e518ce5ca4d 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/monitor200/TimeTunnelAdviceListener.java +++ b/core/src/main/java/com/taobao/arthas/core/command/monitor200/TimeTunnelAdviceListener.java @@ -98,8 +98,8 @@ private void afterFinishing(Advice advice) { isFirst = false; } - process.times().incrementAndGet(); - if (isLimitExceeded(command.getNumberOfLimit(), process.times().get())) { + int times = process.times().incrementAndGet(); + if (isLimitExceeded(command.getNumberOfLimit(), times)) { abortProcess(process, command.getNumberOfLimit()); } } diff --git a/core/src/main/java/com/taobao/arthas/core/command/monitor200/WatchAdviceListener.java b/core/src/main/java/com/taobao/arthas/core/command/monitor200/WatchAdviceListener.java index 6d186d34de9..13cac929348 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/monitor200/WatchAdviceListener.java +++ b/core/src/main/java/com/taobao/arthas/core/command/monitor200/WatchAdviceListener.java @@ -102,8 +102,8 @@ private void watching(Advice advice) { } process.appendResult(model); - process.times().incrementAndGet(); - if (isLimitExceeded(command.getNumberOfLimit(), process.times().get())) { + int times = process.times().incrementAndGet(); + if (isLimitExceeded(command.getNumberOfLimit(), times)) { abortProcess(process, command.getNumberOfLimit()); } } diff --git a/labs/arthas-grpc-web-proxy/src/main/java/com/taobao/arthas/grpcweb/grpc/service/advisor/WatchRpcAdviceListener.java b/labs/arthas-grpc-web-proxy/src/main/java/com/taobao/arthas/grpcweb/grpc/service/advisor/WatchRpcAdviceListener.java index 17330fe6ade..206b9da3788 100644 --- a/labs/arthas-grpc-web-proxy/src/main/java/com/taobao/arthas/grpcweb/grpc/service/advisor/WatchRpcAdviceListener.java +++ b/labs/arthas-grpc-web-proxy/src/main/java/com/taobao/arthas/grpcweb/grpc/service/advisor/WatchRpcAdviceListener.java @@ -119,8 +119,8 @@ private void watching(Advice advice) { model.setAccessPoint(AccessPoint.ACCESS_AFTER_THROWING.getKey()); } arthasStreamObserver.appendResult(model); - arthasStreamObserver.times().incrementAndGet(); - if (isLimitExceeded(watchRequestModel.getNumberOfLimit(), arthasStreamObserver.times().get())) { + int times = arthasStreamObserver.times().incrementAndGet(); + if (isLimitExceeded(watchRequestModel.getNumberOfLimit(), times)) { String msg = "Command execution times exceed limit: " + watchRequestModel.getNumberOfLimit() + ", so command will exit.\n"; arthasStreamObserver.end();