Skip to content

Port already in use when kernel starts #276

@rolweber

Description

@rolweber

Summary: Before starting a kernel, Jupyter picks some unused port numbers and writes them into the connection file. Then it releases those port numbers, so the starting kernel can use them. During that interval, some other process opening a socket connection can allocate one (or more) of the ports. The starting kernel finds a port blocked and runs into some error condition. In the best of cases, Jupyter detects that the kernel doesn't come up and restarts it. But the restart uses the same connection file and port numbers, so the problem persists.

This is a reincarnation of jupyter-server/kernel_gateway#131, which was closed there because it is an upstream problem. @minrk suggested to restart the kernel with newly selected port numbers if this error condition - failure on initial start of the kernel - is detected.

Unless there are other suggestions to deal with the situation, I'd like to work on a PR that implements this behavior. I'll be on vacation next week, but I'm opening the issue already now, to gather as much feedback as possible.


Looking at the code, I think that the ConnectionFileMixin in connect.py will need some attention. It has to remember which of the port numbers are configured and which are chosen at random, so that only the latter are regenerated.
Another place I need to look at is the KernelManager in manager.py. I thought about deleting the kernel and starting a different one, with possibly a different ID. But there are event handling methods in the class, so it's probably easier to keep the instance and generate a new connection file. Maybe I should introduce a relaunch_kernel, in addition to start_kernel and restart_kernel?
But I'm not sure where to detect and handle the actual error condition. Is this something that should be done in jupyter_client itself? Or rather leave it to notebook, kernel_gateway, and other applications using the jupyter_client? Your suggestions would be most welcome :-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions