Skip to content
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

Add Shutdown Hook to enable trapping kill or CTRL+C signals #3120

Closed
asfimport opened this issue May 9, 2013 · 4 comments
Closed

Add Shutdown Hook to enable trapping kill or CTRL+C signals #3120

asfimport opened this issue May 9, 2013 · 4 comments

Comments

@asfimport
Copy link
Collaborator

@pmouawad (Bug 54945):
Necessary to avoid losing test data if kill or CTRL+C are called with the following setting (available since JMETER 2.10)
jmeter.save.saveservice.autoflush=false

OS: All

@asfimport
Copy link
Collaborator Author

@pmouawad (migrated from Bugzilla):
Hello,
Attached is a first proposal of Shutdown Hook implementation.
I had to add a join on StopThread otherwise it can happen exit occurs before thread is started.

Anyway there is still an issue as if shutodwn of threads takes more than jmeterengine.threadstop.wait (ThreadGroup#WAIT_TO_DIE) then it can happen that close will not be called on PrintWriter.

Created attachment BUG_54945.patch: Draft of patch

BUG_54945.patch
Index: src/core/org/apache/jmeter/JMeter.java
===================================================================
--- src/core/org/apache/jmeter/JMeter.java	(revision 1480665)
+++ src/core/org/apache/jmeter/JMeter.java	(working copy)
@@ -821,6 +821,7 @@
                 log.info("Remote engines have been started");
             }
             startUdpDdaemon(engines);
+            Runtime.getRuntime().addShutdownHook(new Thread(new JMeterShutdownHook(engines)));
         } catch (Exception e) {
             System.out.println("Error in NonGUIDriver " + e.toString());
             log.error("Error in NonGUIDriver", e);
@@ -828,6 +829,28 @@
             JOrphanUtils.closeQuietly(reader);
         }
     }
+    
+    private static final class JMeterShutdownHook implements Runnable {
+        private static final Logger log = LoggingManager.getLoggerFor(JMeterShutdownHook.class.getName());
+        private List<JMeterEngine> engines;
+
+        public JMeterShutdownHook(List<JMeterEngine> engines) {
+            this.engines = engines;
+        }
+
+        @Override
+        public void run() {
+            log.warn("Shutdown hook started");
+            for(JMeterEngine engine : engines) {
+                if(engine.isActive()) {
+                    log.warn("Shutdown hook is stopping engine:"+engine);
+                    engine.stopTest(true);
+                    log.warn("Shutdown hook has stopped engine:"+engine);
+                }
+            }
+            log.warn("Shutdown hook ended");
+        }     
+    }
 
     /**
      * Refactored from AbstractAction.java
Index: src/core/org/apache/jmeter/engine/StandardJMeterEngine.java
===================================================================
--- src/core/org/apache/jmeter/engine/StandardJMeterEngine.java	(revision 1480665)
+++ src/core/org/apache/jmeter/engine/StandardJMeterEngine.java	(working copy)
@@ -258,6 +258,11 @@
         shutdown = !now;
         Thread stopThread = new Thread(new StopTest(now));
         stopThread.start();
+        try {
+            stopThread.join();
+        } catch (InterruptedException e) {
+            log.warn("Stop thread was interrupted", e);
+        }
     }
 
     private class StopTest implements Runnable {

@asfimport
Copy link
Collaborator Author

Sebb (migrated from Bugzilla):
Note: the code in 2.9 and before used autoflush = true

@asfimport
Copy link
Collaborator Author

Sebb (migrated from Bugzilla):
Note that the shutdown hook is run even if JMeter shuts down normally.
In which case it should not call engine.stop(true).

I wonder if it would be better to implement the shutdown hook in ResultCollector instead.

That might avoid some of the problems?

@asfimport
Copy link
Collaborator Author

@pmouawad (migrated from Bugzilla):
Nice idea sebb.
I commited this way.

Date: Thu May 9 20:32:55 2013
New Revision: 1480762

URL: http://svn.apache.org/r1480762
Log:
#3120 - Add Shutdown Hook to enable trapping kill or CTRL+C signals
#3120

Modified:
jmeter/trunk/src/core/org/apache/jmeter/reporters/ResultCollector.java
jmeter/trunk/xdocs/changes.xml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant