Skip to content

jupyter-session-with-random-ports: ‘jupyter kernel‘ failed to show connection file path #606

@simenojensen

Description

@simenojensen

I updated jupyter using conda, and then C-c C-p (jupyter-run-repl) no longer worked

error message:
jupyter-session-with-random-ports: ‘jupyter kernel‘ failed to show connection file path

The below code which redefines the function fixes the problem

  (defun jupyter-session-with-random-ports ()
    "Return a `jupyter-session' with random channel ports."
    (with-temp-buffer
      (let* ((j (or (executable-find "jupyter")
                    (error "Cannot find `jupyter` executable")))
             (process
              (if (file-remote-p default-directory)
                  ;; Remote: still use start-file-process so TRAMP works
                  (start-file-process
                   "jupyter-session-with-random-ports" (current-buffer)
                   j "kernel")
                ;; Local: capture stderr too
                (make-process
                 :name "jupyter-session-with-random-ports"
                 :buffer (current-buffer)
                 :command (list j "kernel")
                 :stderr (current-buffer)
                 :noquery t))))
        (set-process-query-on-exit-flag process nil)

        (let ((conn-path nil)
              (deadline (+ (float-time) jupyter-long-timeout)))
          (while (and (process-live-p process)
                      (not conn-path)
                      (< (float-time) deadline))
            (accept-process-output process 0.05)
            (save-excursion
              (goto-char (point-min))
              (when (re-search-forward
                     (rx "Connection file:" (* space)
                         (group "/" (+ (not (any "\n\r"))) ".json"))
                     nil t)
                (setq conn-path (match-string 1)))))

          (unless conn-path
            (let ((out (buffer-string)))
              (when (process-live-p process) (delete-process process))
              (error "`jupyter kernel` did not print a parsable connection file path. Captured output:\n%s"
                     (if (> (length out) 4000) (substring out 0 4000) out))))

          (let* ((conn-file (concat
                             (save-match-data (file-remote-p default-directory))
                             conn-path))
                 (conn-info (jupyter-read-connection conn-file)))
            ;; shut down kernel
            (interrupt-process process)

            ;; wait for connection file cleanup
            (jupyter-with-timeout
                (nil (if (file-remote-p conn-file) 0 jupyter-default-timeout)
                     (delete-file conn-file))
              (not (file-exists-p conn-file)))

            (delete-process process)

            (let ((new-key (jupyter-new-uuid)))
              (plist-put conn-info :key new-key)
              (jupyter-session :conn-info conn-info :key new-key)))))))

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