IMPALA-7893: Correctly handle Ctrl+C for cancelling a non-running query

This patch fixes the issue with Ctrl+C handling for cancelling a
non-running query to behave similar to Linux shell.

Before (pressing Ctrl+C does not do anything):
[localhost:21000] default> select

After (pressing Ctrl+C cancels the query and starts a new prompt):
[localhost:21000] default> select^C
[localhost:21000] default>

Testing:
- Added a new cancellation test
- Ran all shell E2E tests

Change-Id: I80d7b2c2350224d88d0bfeb1745d9ed76e83cf6d
Reviewed-on: http://gerrit.cloudera.org:8080/11990
Reviewed-by: Impala Public Jenkins <[email protected]>
Tested-by: Impala Public Jenkins <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/impala/repo
Commit: http://git-wip-us.apache.org/repos/asf/impala/commit/96f97653
Tree: http://git-wip-us.apache.org/repos/asf/impala/tree/96f97653
Diff: http://git-wip-us.apache.org/repos/asf/impala/diff/96f97653

Branch: refs/heads/master
Commit: 96f976534837dde6d3fb87ea02019b67d9c3ea78
Parents: e546e52
Author: Fredy Wijaya <[email protected]>
Authored: Mon Nov 26 16:12:58 2018 -0800
Committer: Impala Public Jenkins <[email protected]>
Committed: Wed Nov 28 10:28:39 2018 +0000

----------------------------------------------------------------------
 shell/impala_shell.py                   | 4 ++--
 tests/custom_cluster/test_client_ssl.py | 1 -
 tests/shell/test_shell_interactive.py   | 6 ++++++
 3 files changed, 8 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/impala/blob/96f97653/shell/impala_shell.py
----------------------------------------------------------------------
diff --git a/shell/impala_shell.py b/shell/impala_shell.py
index 4f09bd8..340c3e7 100755
--- a/shell/impala_shell.py
+++ b/shell/impala_shell.py
@@ -523,7 +523,7 @@ class ImpalaShell(object, cmd.Cmd):
   def _signal_handler(self, signal, frame):
     """Handles query cancellation on a Ctrl+C event"""
     if self.last_query_handle is None or self.query_handle_closed:
-      return
+      raise KeyboardInterrupt()
     # Create a new connection to the impalad and cancel the query.
     for cancel_try in xrange(ImpalaShell.CANCELLATION_TRIES):
       try:
@@ -1733,7 +1733,7 @@ if __name__ == "__main__":
       try:
         shell.cmdloop(intro)
       except KeyboardInterrupt:
-        intro = '\n'
+        print_to_stderr('^C')
       # A last measure against any exceptions thrown by an rpc
       # not caught in the shell
       except socket.error, (code, e):

http://git-wip-us.apache.org/repos/asf/impala/blob/96f97653/tests/custom_cluster/test_client_ssl.py
----------------------------------------------------------------------
diff --git a/tests/custom_cluster/test_client_ssl.py 
b/tests/custom_cluster/test_client_ssl.py
index edea640..f5ada13 100644
--- a/tests/custom_cluster/test_client_ssl.py
+++ b/tests/custom_cluster/test_client_ssl.py
@@ -107,7 +107,6 @@ class TestClientSsl(CustomClusterTestSuite):
     result = p.get_result()
 
     print result.stderr
-    assert result.rc == 0
     assert "Query Status: Cancelled" in result.stdout
     assert impalad.wait_for_num_in_flight_queries(0)
 

http://git-wip-us.apache.org/repos/asf/impala/blob/96f97653/tests/shell/test_shell_interactive.py
----------------------------------------------------------------------
diff --git a/tests/shell/test_shell_interactive.py 
b/tests/shell/test_shell_interactive.py
index 4d071de..5067bf9 100755
--- a/tests/shell/test_shell_interactive.py
+++ b/tests/shell/test_shell_interactive.py
@@ -180,6 +180,12 @@ class TestImpalaShellInteractive(object):
     assert "Cancelled" not in result.stderr
     assert impalad.wait_for_num_in_flight_queries(0)
 
+    p = ImpalaShell()
+    sleep(3)
+    os.kill(p.pid(), signal.SIGINT)
+    result = p.get_result()
+    assert "^C" in result.stderr
+
   def test_unicode_input(self):
     "Test queries containing non-ascii input"
     # test a unicode query spanning multiple lines

Reply via email to