Skip to content

Commit e05bd3e

Browse files
committed
Execute commands on Python main thread
1 parent 3a47b4a commit e05bd3e

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

src/gdb_cli/gdb_server/handlers.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
import os
9+
import queue
910
from pathlib import Path
1011
from typing import Any, List, Optional, Tuple
1112

@@ -571,7 +572,26 @@ def handle_exec(
571572
}
572573

573574
try:
574-
output = gdb.execute(command, to_string=True)
575+
576+
# The command must run in the main thread, not in the thread that
577+
# receives from the socket. Create a function which will execute
578+
# the command and pass the output through a queue. This definition
579+
# uses lexical scoping for the command and the queue.
580+
result_queue = queue.Queue()
581+
582+
def run_command():
583+
try:
584+
output = gdb.execute(command, to_string=True)
585+
result_queue.put(("ok", output))
586+
except Exception as e:
587+
result_queue.put(("error", str(e)))
588+
589+
# Pass the function through post_event() to the main thread,
590+
# where it will run, and this thread waits to receive the output
591+
# from the queue.
592+
gdb.post_event(run_command)
593+
output_status, output = result_queue.get(timeout=30.0)
594+
575595
return {
576596
"command": command,
577597
"output": output or "(no output)"

0 commit comments

Comments
 (0)