Skip to content

Commit 0674876

Browse files
committed
improve query cancellation on control-c
* sqlexecute.run() no longer returns a tuple, but a Generator of SQLResults * move sqlexecute.connect() within a try block * clarify inner error as e2 * grammar and spelling in commentary * if raising a CommandNotFound(), show the command (this can be part of the relevant backtrace) Due to the first and second bullet, an interrupted query would indeed be cancelled, but the user would at minimum receive poor feedback, and the mycli session could also end. It might also be desirable to tell click not to handle KeyboardInterrupt.
1 parent 17d63ce commit 0674876

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

changelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
Upcoming (TBD)
2+
==============
3+
4+
Bug Fixes
5+
---------
6+
* Improve query cancellation on control-c.
7+
8+
19
1.61.0 (2026/03/07)
210
==============
311

mycli/main.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,14 +1212,13 @@ def one_iteration(text: str | None = None) -> None:
12121212
except KeyboardInterrupt:
12131213
# get last connection id
12141214
connection_id_to_kill = sqlexecute.connection_id or 0
1215-
# some mysql compatible databases may not implemente connection_id()
1215+
# some mysql-compatible databases may not implement connection_id()
12161216
if connection_id_to_kill > 0:
12171217
logger.debug("connection id to kill: %r", connection_id_to_kill)
1218-
# Restart connection to the database
1219-
sqlexecute.connect()
12201218
try:
1221-
for _preamble, _cur, _headers, status in sqlexecute.run(f"kill {connection_id_to_kill}"):
1222-
status_str = str(status).lower()
1219+
sqlexecute.connect()
1220+
for kill_result in sqlexecute.run(f"kill {connection_id_to_kill}"):
1221+
status_str = str(kill_result.status_plain).lower()
12231222
if status_str.find("ok") > -1:
12241223
logger.debug("cancelled query, connection id: %r, sql: %r", connection_id_to_kill, text)
12251224
self.echo(f"Cancelled query id: {connection_id_to_kill}", err=True, fg="blue")
@@ -1230,8 +1229,8 @@ def one_iteration(text: str | None = None) -> None:
12301229
text,
12311230
)
12321231
self.echo(f"Failed to confirm query cancellation, id: {connection_id_to_kill}", err=True, fg="red")
1233-
except Exception as e:
1234-
self.echo(f"Encountered error while cancelling query: {e}", err=True, fg="red")
1232+
except Exception as e2:
1233+
self.echo(f"Encountered error while cancelling query: {e2}", err=True, fg="red")
12351234
else:
12361235
logger.debug("Did not get a connection id, skip cancelling query")
12371236
self.echo("Did not get a connection id, skip cancelling query", err=True, fg="red")

mycli/packages/special/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def execute(cur: Cursor, sql: str) -> list[SQLResult]:
133133
command, verbosity, arg = parse_special_command(sql)
134134

135135
if (command not in COMMANDS) and (command.lower() not in COMMANDS):
136-
raise CommandNotFound()
136+
raise CommandNotFound(f'Command not found: {command}')
137137

138138
try:
139139
special_cmd = COMMANDS[command]

0 commit comments

Comments
 (0)