Function: nrepl--available-local-port

nrepl--available-local-port is a byte-compiled function defined in nrepl-client.el.

Signature

(nrepl--available-local-port)

Documentation

Return a TCP port on localhost that currently has no listener.

Used to pick the local end of an SSH tunnel. Random ports are probed with nrepl--port-open-p rather than binding a throwaway server socket, which keeps the check portable (notably to MS-Windows, where reading back a system-assigned port is unreliable).

Source Code

;; Defined in ~/.emacs.d/elpa/cider-20260822.1520/nrepl-client.el
(defun nrepl--available-local-port ()
  "Return a TCP port on localhost that currently has no listener.
Used to pick the local end of an SSH tunnel.  Random ports are probed with
`nrepl--port-open-p' rather than binding a throwaway server socket, which
keeps the check portable (notably to MS-Windows, where reading back a
system-assigned port is unreliable)."
  (let ((port nil)
        (tries 0))
    (while (and (not port) (< tries 100))
      (let ((candidate (+ 1024 (random (- 65536 1024)))))
        (unless (nrepl--port-open-p "localhost" candidate)
          (setq port candidate)))
      (setq tries (1+ tries)))
    (or port
        (error "[nREPL] Could not find a free local port for the SSH tunnel"))))