Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

log error on kernel failure to connect (#9289)

Akshay Agrawalc97160797b84
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index cc161341cf..37416f2dc8 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -3598,18 +3598,30 @@ def launch_kernel(
pipe: TypedConnection[KernelMessage] | None = None
if socket_addr is not None:
n_tries = 0
+ last_error: BaseException | None = None
while n_tries < 100:
try:
pipe = TypedConnection[KernelMessage].of(
connection.Client(socket_addr)
)
break
- except Exception:
+ except Exception as e:
+ last_error = e
n_tries += 1
time.sleep(0.01)
if n_tries == 100 or pipe is None:
- LOGGER.debug("Failed to connect to socket.")
+ # The parent may still be waiting for this subprocess to connect,
+ # but startup now watches kernel liveness and will abort if the
+ # kernel exits. Log the cause so the failure is diagnosable
+ # instead of opaque.
+ LOGGER.error(
+ "marimo kernel subprocess failed to connect to %s "
+ "after %d attempts",
+ socket_addr,
+ n_tries,
+ exc_info=last_error,
+ )
return
stream = ThreadSafeStream(
diff --git a/marimo/_session/managers/kernel.py b/marimo/_session/managers/kernel.py
index b8eb70188b..cdc8beb744 100644
--- a/marimo/_session/managers/kernel.py
+++ b/marimo/_session/managers/kernel.py
@@ -154,11 +154,39 @@ class KernelManagerImpl(KernelManager):
self.kernel_task.start() # type: ignore
if listener is not None:
- # First thing kernel does is connect to the socket, so it's safe to
- # call accept
- self._read_conn = TypedConnection[KernelMessage].of(
- listener.accept()
+ # Listener.accept() has no timeout. Run it on a helper thread so
+ # the main path can watchdog kernel_task liveness; otherwise a
+ # child that dies before connecting leaves us blocked forever.
+ result: dict[str, Any] = {}
+
+ def _accept() -> None:
+ try:
+ result["conn"] = listener.accept()
+ except Exception as e:
+ result["error"] = e
+
+ accept_thread = threading.Thread(
+ target=_accept, name="kernel-accept", daemon=True
)
+ accept_thread.start()
+ while True:
+ accept_thread.join(timeout=0.5)
+ if not accept_thread.is_alive():
+ break
+ if not self.kernel_task.is_alive(): # type: ignore[attr-defined]
+ # Closing the listener unblocks accept() in the helper
+ # thread so it can exit cleanly instead of leaking.
+ listener.close()
+ accept_thread.join(timeout=1.0)
+ raise RuntimeError(
+ "marimo kernel subprocess exited before "
+ "connecting (exitcode="
+ f"{getattr(self.kernel_task, 'exitcode', None)})"
+ "; check subprocess stderr for the cause"
+ )
+ if "error" in result:
+ raise result["error"]
+ self._read_conn = TypedConnection[KernelMessage].of(result["conn"])
@property
def pid(self) -> int | None: