Skip to content

Handling errors in extensions and custom code on startup #103

@andrus

Description

@andrus

Not yet ready to commit to this solution, but wanted to document the idea.

Kernel may potentially load a bunch of user code on startup as extensions and custom code snippets (via JJAVA_STARTUP_SCRIPT). Any one of them can result in an error. Currently, the errors are printed in console, the kernel process dies, Jupyter tries to restart it, resulting in a restart loop (5 restart tries), and then still spinning not able to assign the kernel to the notebook.

I wonder if a more sensible behavior would be to catch and report the exceptions in the console, still start the kernel in a partially-broken state, and then on first eval, print the startup errors in the notebook to alert the user?

I guess no matter what we do here, identifying the boot step where the kernel failed, and pointing to a specific extension location or to the JJAVA_STARTUP_SCRIPT as the source of trouble would help users a lot. Maybe this part can be addressed simply by printing each startup and extension loading step as a debug message.

# intentionally bad code
$ export JJAVA_STARTUP_SCRIPT='var x = y' 

$ jupyter lab
...
[I 2025-11-08 09:26:38.905 ServerApp] Kernel started: a9c2319b-4365-4f8f-9122-69064bb204b3
[I 2025-11-08 09:26:38.962 KernelLauncher] Starting kernel: [java, -Xmx20480m, --add-opens=java.base/java.nio=ALL-UNNAMED, 
--add-opens, jdk.jshell/jdk.jshell=ALL-UNNAMED, -jar, /Users/andrus/Library/Jupyter/kernels/java/jjava.jar, 
/Users/andrus/Library/Jupyter/runtime/kernel-a9c2319b-4365-4f8f-9122-69064bb204b3.json]
Exception in thread "main" org.dflib.jjava.kernel.execution.CompilationException: [snippet: [var x = y;], status: REJECTED]
        at org.dflib.jjava.kernel.execution.CodeEvaluator.evalSingle(CodeEvaluator.java:118)
        at org.dflib.jjava.kernel.execution.CodeEvaluator.eval(CodeEvaluator.java:131)
        at org.dflib.jjava.kernel.JavaKernel.evalRaw(JavaKernel.java:234)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
        at org.dflib.jjava.distro.JJava.main(JJava.java:82)
[I 2025-11-08 09:26:39.714 KernelLauncher] Kernel terminated.

[I 2025-11-08 09:26:41.892 ServerApp] AsyncIOLoopKernelRestarter: restarting kernel (1/5), new random ports
[I 2025-11-08 09:26:41.984 KernelLauncher] Starting kernel: [java, -Xmx20480m, --add-opens=java.base/java.nio=ALL-UNNAMED, 
--add-opens, jdk.jshell/jdk.jshell=ALL-UNNAMED, -jar, /Users/andrus/Library/Jupyter/kernels/java/jjava.jar, 
/Users/andrus/Library/Jupyter/runtime/kernel-a9c2319b-4365-4f8f-9122-69064bb204b3.json]
Exception in thread "main" org.dflib.jjava.kernel.execution.CompilationException: [snippet: [var x = y;], status: REJECTED]
        at org.dflib.jjava.kernel.execution.CodeEvaluator.evalSingle(CodeEvaluator.java:118)
        at org.dflib.jjava.kernel.execution.CodeEvaluator.eval(CodeEvaluator.java:131)
        at org.dflib.jjava.kernel.JavaKernel.evalRaw(JavaKernel.java:234)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
        at org.dflib.jjava.distro.JJava.main(JJava.java:82)
[I 2025-11-08 09:26:42.734 KernelLauncher] Kernel terminated.
[I 2025-11-08 09:26:44.916 ServerApp] AsyncIOLoopKernelRestarter: restarting kernel (2/5), new random ports

... after 5 restart attempts

[W 2025-11-08 09:41:35.608 ServerApp] wrote error: 'Kernel does not exist: a9c2319b-4365-4f8f-9122-69064bb204b3'
    Traceback (most recent call last):
      File "/opt/homebrew/Cellar/jupyterlab/4.4.0/libexec/lib/python3.13/site-packages/tornado/web.py", line 1790, in _execute
        result = await result
                 ^^^^^^^^^^^^
      File "/opt/homebrew/Cellar/jupyterlab/4.4.0/libexec/lib/python3.13/site-packages/jupyter_server/auth/decorator.py", line 73, in inner
        return await out
               ^^^^^^^^^
      File "/opt/homebrew/Cellar/jupyterlab/4.4.0/libexec/lib/python3.13/site-packages/jupyter_server/services/kernels/handlers.py", line 75, in get
        model = await ensure_async(km.kernel_model(kernel_id))
                                   ~~~~~~~~~~~~~~~^^^^^^^^^^^
      File "/opt/homebrew/Cellar/jupyterlab/4.4.0/libexec/lib/python3.13/site-packages/jupyter_server/services/kernels/kernelmanager.py", line 508, in kernel_model
        self._check_kernel_id(kernel_id)
        ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
      File "/opt/homebrew/Cellar/jupyterlab/4.4.0/libexec/lib/python3.13/site-packages/jupyter_server/services/kernels/kernelmanager.py", line 539, in _check_kernel_id
        raise web.HTTPError(404, "Kernel does not exist: %s" % kernel_id)
    tornado.web.HTTPError: HTTP 404: Not Found (Kernel does not exist: a9c2319b-4365-4f8f-9122-69064bb204b3)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions