# Terminate gatling script execution

**URL:** <https://community.gatling.io/t/terminate-gatling-script-execution/9321>\
**Category:** Gatling (Open-Source)\
**Created:** [September 30, 2024, 7:55am UTC](https://community.gatling.io/t/terminate-gatling-script-execution/9321 "2024-09-30T07:55:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sukura](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@sukura](https://community.gatling.io/u/sukura)\
**Post date:** [September 30, 2024, 7:55am UTC](https://community.gatling.io/t/terminate-gatling-script-execution/9321/1 "2024-09-30T07:55:51Z")

</div>

**Gatling version** : 3.10.5  
**Gatling flavor** : [✅] java kotlin scala javascript typescript  
**Gatling build tool** : [✅] maven gradle sbt bundle npm  
Hello Gatling Community,  
I’m currently running into an issue while using Gatling.main for performance testing. Tests are launched via an asynchronous thread pool. Since the service may crash, it is necessary to have a function that can terminate the performance test during the performance test.  
**The Problem:**

When I attempt to stop the Gatling execution, I get an InterruptedException, and the process crashes. The error occurs within the Future.cancel(true) call, which is likely causing a thread interruption in Gatling’s internal blocking operations.

**Question:**

• What is the best way to stop a Gatling test mid-execution without causing an InterruptedException or crashing the process?

• Is there a recommended method to gracefully stop a running Gatling.main task within an asynchronous thread?

**My Implementation:**

1. I use an asynchronous thread pool to execute Gatling.main:

> // 构建 Gatling 命令行参数  
> String simulationClass = pressureRequest.getRequestName();  
> String gatlingArgs = {“-s”, simulationClass};  
> // 提交异步任务到自定义线程池  
> Future\<?\> future = gatlingTaskExecutor.submit(() → {  
> try {  
> log.info(“Gatling 压力测试任务 [{}] 已启动”, requestName);  
> // 执行 Gatling，期间可以检查中断请求  
> Gatling.main(gatlingArgs);  
> log.info(“Gatling 压力测试任务 [{}] 已完成”, requestName);  
> } catch (Exception e) {  
> log.error(“Gatling 压力测试任务 [{}] 启动失败”, requestName, e);  
> } finally {  
> runningTasks.remove(requestName); // 任务完成后移除记录  
> }  
> });

1. When I attempt to stop the task, I use the following code:

> try {  
> CoordinatedShutdown.get(gatlingActorSystem).runAll(CoordinatedShutdown.ActorSystemTerminateReason$.MODULE$);  
> log.info(“Gatling 压力测试任务 [{}] 已请求优雅终止”, requestName);  
> // 中断线程任务  
> boolean terminated = future.cancel(true);  
> if (terminated) {  
> log.info(“Gatling 压力测试任务 [{}] 已被取消”, requestName);  
> // 任务取消后移除线程池中的线程  
> runningTasks.remove(requestName);  
> return Response.success(true);  
> } else {  
> log.warn(“Gatling 压力测试任务 [{}] 终止失败”, requestName);  
> return Response.fail(ErrorCode.TASK\_TERMINATION\_FAILED);  
> }  
> } catch (Exception e) {  
> log.error(“Gatling 压力测试任务 [{}] 终止失败”, requestName, e);  
> return Response.fail(ErrorCode.TASK\_TERMINATION\_FAILED);  
> }

**log**

> 12:17:27.264 [pool-1-thread-1] ERROR io.gatling.app.Gatling$ - Run crashed  
> java.lang.InterruptedException: null  
> at java.base/java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireSharedNanos(AbstractQueuedSynchronizer.java:1133)  
> at scala.concurrent.impl.Promise$DefaultPromise.tryAwait0(Promise.scala:241)  
> at scala.concurrent.impl.Promise$DefaultPromise.result(Promise.scala:261)  
> at scala.concurrent.Await$.$anonfun$result$1(package.scala:201)  
> at scala.concurrent.BlockContext$DefaultBlockContext$.blockOn(BlockContext.scala:62)  
> at scala.concurrent.Await$.result(package.scala:124)  
> at io.gatling.app.Runner.start(Runner.scala:111)  
> at io.gatling.app.Runner.run(Runner.scala:59)  
> at io.gatling.app.Gatling$.start(Gatling.scala:92)  
> at io.gatling.app.Gatling$.fromArgs(Gatling.scala:54)  
> at io.gatling.app.Gatling$.main(Gatling.scala:42)  
> at io.gatling.app.Gatling.main(Gatling.scala)  
> at io.gatling.service.impl.PressureServiceImpl.lambda$startPressure$0(PressureServiceImpl.java:52)  
> at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:572)  
> at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)  
> at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)  
> at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)  
> at java.base/java.lang.Thread.run(Thread.java:1583)  
> 12:17:27.264 [http-nio-8889-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - Completed 200 OK  
> 12:17:27.266 [GatlingSystem-akka.actor.default-dispatcher-12] INFO akka.actor.CoordinatedShutdown - Running CoordinatedShutdown with reason [ActorSystemTerminateReason]

Any advice or insights would be greatly appreciated. Thank you!

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [September 30, 2024, 8:21am UTC](https://community.gatling.io/t/terminate-gatling-script-execution/9321/2 "2024-09-30T08:21:29Z")

</div>

Hello,

Sorry but this usage is not supported.  
You’re not supposed to directly hack on the Gatling main class, it’s an internal.  
The only supported launchers are the maven, gradle, sbt, npm and Gatling Enterprise.  
We can’t help here with your custom in-house integration.

Regards

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [September 30, 2024, 8:21am UTC](https://community.gatling.io/t/terminate-gatling-script-execution/9321/3 "2024-09-30T08:21:49Z")

</div>


