Skip to content

Commit 7cd30c7

Browse files
committed
debug
1 parent 7986de5 commit 7cd30c7

File tree

4 files changed

+77
-49
lines changed

4 files changed

+77
-49
lines changed

ipykernel/kernelbase.py

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

280-
self._iant_lock = threading.Lock()
280+
#self._iant_lock = threading.Lock()
281281

282282
# Kernel application may swap stdout and stderr to OutStream,
283283
# which is the case in `IPKernelApp.init_io`, hence `sys.stdout`
@@ -682,33 +682,33 @@ def _publish_execute_input(self, code, parent, execution_count):
682682
"""Publish the code request on the iopub stream."""
683683
if not self.session:
684684
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-
689-
self.session.send(
690-
self.iopub_socket,
691-
"execute_input",
692-
{"code": code, "execution_count": execution_count},
693-
parent=parent,
694-
ident=self._topic("execute_input"),
695-
)
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+
689+
self.session.send(
690+
self.iopub_socket,
691+
"execute_input",
692+
{"code": code, "execution_count": execution_count},
693+
parent=parent,
694+
ident=self._topic("execute_input"),
695+
)
696696

697697
def _publish_status(self, status, channel, parent=None):
698698
"""send status (busy/idle) on IOPub"""
699699
if not self.session:
700700
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-
705-
self.session.send(
706-
self.iopub_socket,
707-
"status",
708-
{"execution_state": status},
709-
parent=parent or self.get_parent(channel),
710-
ident=self._topic("status"),
711-
)
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+
705+
self.session.send(
706+
self.iopub_socket,
707+
"status",
708+
{"execution_state": status},
709+
parent=parent or self.get_parent(channel),
710+
ident=self._topic("status"),
711+
)
712712

713713
def _publish_status_and_flush(self, status, channel, stream, parent=None):
714714
"""send status on IOPub and flush specified stream to ensure reply is sent before handling the next reply"""
@@ -719,17 +719,17 @@ def _publish_status_and_flush(self, status, channel, stream, parent=None):
719719
def _publish_debug_event(self, event):
720720
if not self.session:
721721
return
722-
with self._iant_lock:
723-
with open("debug.txt", "a") as f:
724-
f.write(f"{threading.current_thread().ident} iopub_socket debug_event\n")
725-
726-
self.session.send(
727-
self.iopub_socket,
728-
"debug_event",
729-
event,
730-
parent=self.get_parent(),
731-
ident=self._topic("debug_event"),
732-
)
722+
#with self._iant_lock:
723+
with open("debug.txt", "a") as f:
724+
f.write(f"{threading.current_thread().ident} iopub_socket debug_event\n")
725+
726+
self.session.send(
727+
self.iopub_socket,
728+
"debug_event",
729+
event,
730+
parent=self.get_parent(),
731+
ident=self._topic("debug_event"),
732+
)
733733

734734
def set_parent(self, ident, parent, channel="shell"):
735735
"""Set the current parent request
@@ -895,7 +895,7 @@ async def execute_request(self, stream, ident, parent):
895895

896896
with open("debug.txt", "a") as f:
897897
f.write(
898-
f"{threading.current_thread().ident} execute_reply {msg_id} {subshell_id}\n"
898+
f"{threading.current_thread().ident} execute_reply {msg_id} {subshell_id} {reply_content}\n"
899899
)
900900

901901
reply_msg: dict[str, t.Any] = self.session.send( # type:ignore[assignment]
@@ -1234,6 +1234,11 @@ async def create_subshell_request(self, socket, ident, parent) -> None:
12341234
self.log.error("Subshells are not supported by this kernel")
12351235
return
12361236

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

12391244
# This should only be called in the control thread if it exists.
@@ -1641,15 +1646,15 @@ async def _at_shutdown(self):
16411646

16421647
finally:
16431648
if self._shutdown_message is not None and self.session:
1644-
with self._iant_lock:
1645-
with open("debug.txt", "a") as f:
1646-
f.write(f"{threading.current_thread().ident} ? _shutdown\n")
1647-
1648-
self.session.send(
1649-
self.iopub_socket,
1650-
self._shutdown_message,
1651-
ident=self._topic("shutdown"),
1652-
)
1649+
#with self._iant_lock:
1650+
with open("debug.txt", "a") as f:
1651+
f.write(f"{threading.current_thread().ident} ? _shutdown\n")
1652+
1653+
self.session.send(
1654+
self.iopub_socket,
1655+
self._shutdown_message,
1656+
ident=self._topic("shutdown"),
1657+
)
16531658
self.log.debug("%s", self._shutdown_message)
16541659
if self.control_stream:
16551660
self.control_stream.flush(zmq.POLLOUT)

ipykernel/subshell_manager.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,16 @@ def _send_on_shell_channel(self, msg) -> None:
225225
assert current_thread().name == SHELL_CHANNEL_THREAD_NAME
226226

227227
with open("debug.txt", "a") as f:
228-
f.write(f"{current_thread().ident} _send_on_shell_channel {msg}\n")
228+
f.write(f"{current_thread().ident} _send_on_shell_channel - start {msg}\n")
229229

230230
with self._lock_shell_socket:
231+
with open("debug.txt", "a") as f:
232+
f.write(f" {self._shell_socket}\n")
231233
self._shell_socket.send_multipart(msg)
232234

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

tests/test_subshells.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def create_subshell_helper(kc: BlockingKernelClient):
2020
kc.control_channel.send(msg)
2121
msg_id = msg["header"]["msg_id"]
2222
reply = get_reply(kc, msg_id, TIMEOUT, channel="control")
23-
wait_for_idle(kc)
23+
#wait_for_idle(kc, msg_id)
2424
return reply["content"]
2525

2626

@@ -29,7 +29,7 @@ def delete_subshell_helper(kc: BlockingKernelClient, subshell_id: str):
2929
kc.control_channel.send(msg)
3030
msg_id = msg["header"]["msg_id"]
3131
reply = get_reply(kc, msg_id, TIMEOUT, channel="control")
32-
wait_for_idle(kc)
32+
#wait_for_idle(kc, msg_id)
3333
return reply["content"]
3434

3535

@@ -38,7 +38,7 @@ def list_subshell_helper(kc: BlockingKernelClient):
3838
kc.control_channel.send(msg)
3939
msg_id = msg["header"]["msg_id"]
4040
reply = get_reply(kc, msg_id, TIMEOUT, channel="control")
41-
wait_for_idle(kc)
41+
#wait_for_idle(kc, msg_id)
4242
return reply["content"]
4343

4444

@@ -52,9 +52,25 @@ def execute_request(kc: BlockingKernelClient, code: str, subshell_id: str | None
5252
def execute_request_subshell_id(
5353
kc: BlockingKernelClient, code: str, subshell_id: str | None, terminator: str = "\n"
5454
):
55+
56+
with open("debug.txt", "a") as f:
57+
f.write("CHECK 1\n")
58+
5559
msg = execute_request(kc, code, subshell_id)
60+
61+
with open("debug.txt", "a") as f:
62+
f.write("CHECK 2\n")
63+
5664
msg_id = msg["header"]["msg_id"]
65+
66+
with open("debug.txt", "a") as f:
67+
f.write("CHECK 3\n")
68+
5769
stdout, _ = assemble_output(kc.get_iopub_msg, None, msg_id)
70+
71+
with open("debug.txt", "a") as f:
72+
f.write("CHECK 4\n")
73+
5874
return stdout.strip()
5975

6076

@@ -110,7 +126,7 @@ def test_thread_ids():
110126
thread_id, main_thread_id = execute_thread_ids(kc)
111127
assert thread_id == main_thread_id
112128

113-
thread_id, main_thread_id = execute_thread_ids(kc, subshell_id)
129+
thread_id, main_thread_id = execute_thread_ids(kc, subshell_id) # This is the problem
114130
assert thread_id != main_thread_id
115131

116132
delete_subshell_helper(kc, subshell_id)

tests/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ def assemble_output(get_msg, timeout=1, parent_msg_id: str | None = None):
178178
stderr = ""
179179
while True:
180180
msg = get_msg(timeout=timeout)
181+
#with open("debug.txt", "a") as f:
182+
# f.write(f"assembly output {msg}\n")
181183
msg_type = msg["msg_type"]
182184
content = msg["content"]
183185
if (

0 commit comments

Comments
 (0)