Skip to content

Commit 20cdb0f

Browse files
committed
Debug
1 parent d8df24d commit 20cdb0f

File tree

9 files changed

+190
-28
lines changed

9 files changed

+190
-28
lines changed

ipykernel/debugger.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
except ImportError:
1818
from jupyter_client.jsonutil import date_default as json_default
1919

20+
import threading
21+
2022
from .compiler import get_file_name, get_tmp_directory, get_tmp_hash_seed
2123

2224
try:
@@ -420,6 +422,10 @@ def start(self):
420422
code = "import debugpy;"
421423
code += 'debugpy.listen(("' + host + '",' + port + "))"
422424
content = {"code": code, "silent": True}
425+
426+
#with open("debug.txt", "a") as f:
427+
# f.write(f"{threading.current_thread().ident} shell_socket execute_request\n")
428+
423429
self.session.send(
424430
self.shell_socket,
425431
"execute_request",

ipykernel/displayhook.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import builtins
88
import sys
9+
import threading
910
import typing as t
1011

1112
from IPython.core.displayhook import DisplayHook
@@ -44,6 +45,10 @@ def __call__(self, obj):
4445
"data": {"text/plain": repr(obj)},
4546
"metadata": {},
4647
}
48+
49+
#with open("debug.txt", "a") as f:
50+
# f.write(f"{threading.current_thread().ident} pub_socket execute_result\n")
51+
4752
self.session.send(
4853
self.pub_socket, "execute_result", contents, parent=self.parent_header, ident=self.topic
4954
)
@@ -97,5 +102,8 @@ def finish_displayhook(self):
97102
sys.stdout.flush()
98103
sys.stderr.flush()
99104
if self.msg and self.msg["content"]["data"] and self.session:
105+
#with open("debug.txt", "a") as f:
106+
# f.write(f"{threading.current_thread().ident} pub_socket ?finish_displayhook\n")
107+
100108
self.session.send(self.pub_socket, self.msg, ident=self.topic)
101109
self.msg = None

ipykernel/iostream.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,15 @@ def __init__(self, socket, pipe=False):
7878
self.thread.is_pydev_daemon_thread = True # type:ignore[attr-defined]
7979
self.thread.name = "IOPub"
8080

81+
#with open("debug.txt", "a") as f:
82+
# f.write(" IOPubThread.__init__\n")
83+
8184
def _thread_main(self):
8285
"""The inner loop that's actually run in a thread"""
8386

87+
#with open("debug.txt", "a") as f:
88+
# f.write(f"{threading.current_thread().ident} IOPubThread._thread_main\n")
89+
8490
def _start_event_gc():
8591
self._event_pipe_gc_task = asyncio.ensure_future(self._run_event_pipe_gc())
8692

@@ -280,6 +286,9 @@ def _really_send(self, msg, *args, **kwargs):
280286
if self.closed:
281287
return
282288

289+
#with open("debug.txt", "a") as f:
290+
# f.write(f"{threading.current_thread().ident} _really_send iopub\n")
291+
283292
mp_mode = self._check_mp_mode()
284293

285294
if mp_mode != CHILD:
@@ -646,6 +655,9 @@ def _flush(self):
646655
if msg is None:
647656
return
648657

658+
#with open("debug.txt", "a") as f:
659+
# f.write(f"{threading.current_thread().ident} pub_thread ?OutStream.flush\n")
660+
649661
self.session.send(
650662
self.pub_thread,
651663
msg,

ipykernel/kernelbase.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ def __init__(self, **kwargs):
277277
"""Initialize the kernel."""
278278
super().__init__(**kwargs)
279279

280+
#self._iant_lock = threading.Lock()
281+
280282
# Kernel application may swap stdout and stderr to OutStream,
281283
# which is the case in `IPKernelApp.init_io`, hence `sys.stdout`
282284
# can already by different from TextIO at initialization time.
@@ -571,6 +573,10 @@ def schedule_dispatch(self, dispatch, *args):
571573

572574
def start(self):
573575
"""register dispatchers for streams"""
576+
577+
#with open("debug.txt", "a") as f:
578+
# f.write(f"--- kernelbase start --- {threading.main_thread().ident}\n")
579+
574580
self.io_loop = ioloop.IOLoop.current()
575581
self.msg_queue: Queue[t.Any] = Queue()
576582
if not self.shell_channel_thread:
@@ -609,6 +615,11 @@ async def shell_channel_thread_main(self, msg):
609615
msg3 = self.session.deserialize(msg2, content=False, copy=False)
610616
subshell_id = msg3["header"].get("subshell_id")
611617

618+
#with open("debug.txt", "a") as f:
619+
# f.write(
620+
# f"{threading.current_thread().ident} shell_channel_thread_main msg received for {subshell_id}\n"
621+
# )
622+
612623
# Find inproc pair socket to use to send message to correct subshell.
613624
subshell_manager = self.shell_channel_thread.manager
614625
socket = subshell_manager.get_shell_channel_stream(subshell_id)
@@ -622,6 +633,10 @@ async def shell_channel_thread_main(self, msg):
622633

623634
async def shell_main(self, subshell_id: str | None, msg):
624635
"""Handler of shell messages for a single subshell"""
636+
637+
#with open("debug.txt", "a") as f:
638+
# f.write(f"{threading.current_thread().ident} shell_main msg recvd on {subshell_id}\n")
639+
625640
if self._supports_kernel_subshells:
626641
if subshell_id is None:
627642
assert threading.current_thread() == threading.main_thread()
@@ -667,6 +682,10 @@ def _publish_execute_input(self, code, parent, execution_count):
667682
"""Publish the code request on the iopub stream."""
668683
if not self.session:
669684
return
685+
#with self._iant_lock:
686+
#with open("debug.txt", "a") as f:
687+
# f.write(f"{threading.current_thread().ident} iopub_socket execute_input\n")
688+
670689
self.session.send(
671690
self.iopub_socket,
672691
"execute_input",
@@ -679,6 +698,11 @@ def _publish_status(self, status, channel, parent=None):
679698
"""send status (busy/idle) on IOPub"""
680699
if not self.session:
681700
return
701+
#with self._iant_lock:
702+
#with open("debug.txt", "a") as f:
703+
# f.write(f"{threading.current_thread().ident} iopub_socket status {status}\n")
704+
# f.write(f"SESSION {self.session}\n")
705+
682706
self.session.send(
683707
self.iopub_socket,
684708
"status",
@@ -696,6 +720,10 @@ def _publish_status_and_flush(self, status, channel, stream, parent=None):
696720
def _publish_debug_event(self, event):
697721
if not self.session:
698722
return
723+
#with self._iant_lock:
724+
#with open("debug.txt", "a") as f:
725+
# f.write(f"{threading.current_thread().ident} iopub_socket debug_event\n")
726+
699727
self.session.send(
700728
self.iopub_socket,
701729
"debug_event",
@@ -763,6 +791,10 @@ def send_response(
763791
"""
764792
if not self.session:
765793
return None
794+
795+
#with open("debug.txt", "a") as f:
796+
# f.write(f"{threading.current_thread().ident} ? ?send_response\n")
797+
766798
return self.session.send(
767799
stream,
768800
msg_or_type,
@@ -835,6 +867,14 @@ async def execute_request(self, stream, ident, parent):
835867
if self._do_exec_accepted_params["cell_id"]:
836868
do_execute_args["cell_id"] = cell_id
837869

870+
subshell_id = parent["header"].get("subshell_id")
871+
msg_id = parent["header"].get("msg_id")
872+
873+
#with open("debug.txt", "a") as f:
874+
# f.write(
875+
# f"{threading.current_thread().ident} about to execute_request {msg_id} {subshell_id} {code}\n"
876+
# )
877+
838878
# Call do_execute with the appropriate arguments
839879
reply_content = self.do_execute(**do_execute_args)
840880

@@ -854,6 +894,11 @@ async def execute_request(self, stream, ident, parent):
854894
reply_content = json_clean(reply_content)
855895
metadata = self.finish_metadata(parent, metadata, reply_content)
856896

897+
#with open("debug.txt", "a") as f:
898+
# f.write(
899+
# f"{threading.current_thread().ident} execute_reply {msg_id} {subshell_id} {reply_content}\n"
900+
# )
901+
857902
reply_msg: dict[str, t.Any] = self.session.send( # type:ignore[assignment]
858903
stream,
859904
"execute_reply",
@@ -943,6 +988,10 @@ async def history_request(self, stream, ident, parent):
943988
reply_content = await reply_content
944989

945990
reply_content = json_clean(reply_content)
991+
992+
#with open("debug.txt", "a") as f:
993+
# f.write(f"{threading.current_thread().ident} ? history_reply\n")
994+
946995
msg = self.session.send(stream, "history_reply", reply_content, parent, ident)
947996
self.log.debug("%s", msg)
948997

@@ -967,6 +1016,10 @@ async def connect_request(self, stream, ident, parent):
9671016
return
9681017
content = self._recorded_ports.copy() if self._recorded_ports else {}
9691018
content["status"] = "ok"
1019+
1020+
#with open("debug.txt", "a") as f:
1021+
# f.write(f"{threading.current_thread().ident} ? connect_reply\n")
1022+
9701023
msg = self.session.send(stream, "connect_reply", content, parent, ident)
9711024
self.log.debug("%s", msg)
9721025

@@ -991,6 +1044,10 @@ async def kernel_info_request(self, stream, ident, parent):
9911044
return
9921045
content = {"status": "ok"}
9931046
content.update(self.kernel_info)
1047+
1048+
#with open("debug.txt", "a") as f:
1049+
# f.write(f"{threading.current_thread().ident} ? kernel_info_reply\n")
1050+
9941051
msg = self.session.send(stream, "kernel_info_reply", content, parent, ident)
9951052
self.log.debug("%s", msg)
9961053

@@ -1058,6 +1115,10 @@ async def shutdown_request(self, stream, ident, parent):
10581115
content = self.do_shutdown(parent["content"]["restart"])
10591116
if inspect.isawaitable(content):
10601117
content = await content
1118+
1119+
#with open("debug.txt", "a") as f:
1120+
# f.write(f"{threading.current_thread().ident} ? shutdown_reply\n")
1121+
10611122
self.session.send(stream, "shutdown_reply", content, parent, ident=ident)
10621123
# same content, but different msg_id for broadcasting on IOPub
10631124
self._shutdown_message = self.session.msg("shutdown_reply", content, parent)
@@ -1108,6 +1169,10 @@ async def debug_request(self, stream, ident, parent):
11081169
if inspect.isawaitable(reply_content):
11091170
reply_content = await reply_content
11101171
reply_content = json_clean(reply_content)
1172+
1173+
#with open("debug.txt", "a") as f:
1174+
# f.write(f"{threading.current_thread().ident} ? debug_reply\n")
1175+
11111176
reply_msg = self.session.send(stream, "debug_reply", reply_content, parent, ident)
11121177
self.log.debug("%s", reply_msg)
11131178

@@ -1170,13 +1235,20 @@ async def create_subshell_request(self, socket, ident, parent) -> None:
11701235
self.log.error("Subshells are not supported by this kernel")
11711236
return
11721237

1238+
#with open("debug.txt", "a") as f:
1239+
# f.write(f"{threading.current_thread().ident} ? create_subshell_request\n")
1240+
11731241
assert threading.current_thread().name == CONTROL_THREAD_NAME
11741242

11751243
# This should only be called in the control thread if it exists.
11761244
# Request is passed to shell channel thread to process.
11771245
other_socket = self.shell_channel_thread.manager.get_control_other_socket()
11781246
other_socket.send_json({"type": "create"})
11791247
reply = other_socket.recv_json()
1248+
1249+
#with open("debug.txt", "a") as f:
1250+
# f.write(f"{threading.current_thread().ident} ? create_subshell_reply\n")
1251+
11801252
self.session.send(socket, "create_subshell_reply", reply, parent, ident)
11811253

11821254
async def delete_subshell_request(self, socket, ident, parent) -> None:
@@ -1200,6 +1272,10 @@ async def delete_subshell_request(self, socket, ident, parent) -> None:
12001272
other_socket = self.shell_channel_thread.manager.get_control_other_socket()
12011273
other_socket.send_json({"type": "delete", "subshell_id": subshell_id})
12021274
reply = other_socket.recv_json()
1275+
1276+
#with open("debug.txt", "a") as f:
1277+
# f.write(f"{threading.current_thread().ident} ? delete_subshell_reply\n")
1278+
12031279
self.session.send(socket, "delete_subshell_reply", reply, parent, ident)
12041280

12051281
async def list_subshell_request(self, socket, ident, parent) -> None:
@@ -1216,6 +1292,10 @@ async def list_subshell_request(self, socket, ident, parent) -> None:
12161292
other_socket = self.shell_channel_thread.manager.get_control_other_socket()
12171293
other_socket.send_json({"type": "list"})
12181294
reply = other_socket.recv_json()
1295+
1296+
#with open("debug.txt", "a") as f:
1297+
# f.write(f"{threading.current_thread().ident} ? list_subshell_reply\n")
1298+
12191299
self.session.send(socket, "list_subshell_reply", reply, parent, ident)
12201300

12211301
# ---------------------------------------------------------------------------
@@ -1280,6 +1360,10 @@ async def abort_request(self, stream, ident, parent): # pragma: no cover
12801360
content = dict(status="ok")
12811361
if not self.session:
12821362
return
1363+
1364+
#with open("debug.txt", "a") as f:
1365+
# f.write(f"{threading.current_thread().ident} ? abort_reply\n")
1366+
12831367
reply_msg = self.session.send(
12841368
stream, "abort_reply", content=content, parent=parent, ident=ident
12851369
)
@@ -1378,6 +1462,9 @@ def _send_abort_reply(self, stream, msg, idents):
13781462
md = self.finish_metadata(msg, md, status)
13791463
md.update(status)
13801464

1465+
#with open("debug.txt", "a") as f:
1466+
# f.write(f"{threading.current_thread().ident} ? {reply_type}\n")
1467+
13811468
self.session.send(
13821469
stream,
13831470
reply_type,
@@ -1558,6 +1645,10 @@ async def _at_shutdown(self):
15581645

15591646
finally:
15601647
if self._shutdown_message is not None and self.session:
1648+
#with self._iant_lock:
1649+
#with open("debug.txt", "a") as f:
1650+
# f.write(f"{threading.current_thread().ident} ? _shutdown\n")
1651+
15611652
self.session.send(
15621653
self.iopub_socket,
15631654
self._shutdown_message,

ipykernel/subshell_manager.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,18 @@ def _process_control_request(
223223

224224
def _send_on_shell_channel(self, msg) -> None:
225225
assert current_thread().name == SHELL_CHANNEL_THREAD_NAME
226+
227+
#with open("debug.txt", "a") as f:
228+
# f.write(f"{current_thread().ident} _send_on_shell_channel - start {msg}\n")
229+
226230
with self._lock_shell_socket:
231+
#with open("debug.txt", "a") as f:
232+
# f.write(f" {self._shell_socket}\n")
227233
self._shell_socket.send_multipart(msg)
228234

235+
#with open("debug.txt", "a") as f:
236+
# f.write(f"{current_thread().ident} _send_on_shell_channel - end\n")
237+
229238
def _stop_subshell(self, subshell_thread: SubshellThread) -> None:
230239
"""Stop a subshell thread and close all of its resources."""
231240
assert current_thread().name == SHELL_CHANNEL_THREAD_NAME

ipykernel/thread.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
SHELL_CHANNEL_THREAD_NAME = "Shell channel"
88

99

10+
import threading
11+
12+
1013
class BaseThread(Thread):
1114
"""Base class for threads."""
1215

@@ -20,6 +23,9 @@ def __init__(self, **kwargs):
2023
def run(self) -> None:
2124
"""Run the thread."""
2225
try:
26+
#with open("debug.txt", "a") as f:
27+
# f.write(f"{threading.current_thread().ident} io_loop - about to start\n")
28+
2329
self.io_loop.start()
2430
finally:
2531
self.io_loop.close()

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ testpaths = [
158158
"tests",
159159
]
160160
asyncio_mode = "auto"
161-
timeout = 300
161+
#timeout = 300
162+
timeout = 10
162163
# Restore this setting to debug failures
163164
#timeout_method = "thread"
164165
filterwarnings= [

0 commit comments

Comments
 (0)