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.

fix: prevent getnameinfo hang on Windows for link-local IPv6

Myles Scolnick3b8b9bdc1625
diff --git a/marimo/_server/api/lifespans.py b/marimo/_server/api/lifespans.py
index af15fa5593..da80d55620 100644
--- a/marimo/_server/api/lifespans.py
+++ b/marimo/_server/api/lifespans.py
@@ -3,6 +3,7 @@ from __future__ import annotations
import asyncio
import contextlib
+import ipaddress
import socket
from typing import TYPE_CHECKING, Any, Optional
@@ -214,28 +215,46 @@ async def etc(app: Starlette) -> AsyncIterator[None]:
yield
+def _pretty_print_host(host: str, port: int) -> str:
+ """Replace loopback addresses with 'localhost' for display.
+
+ Uses ipaddress for a reliable cross-platform loopback check (covers
+ 127.0.0.1, ::1, and the full 127.0.0.0/8 range). Falls back to
+ socket.getnameinfo only for non-IP hosts. getnameinfo is skipped for
+ raw IP addresses because it can hang on Windows for link-local IPv6.
+ """
+ try:
+ if ipaddress.ip_address(host).is_loopback:
+ return "localhost"
+ except ValueError:
+ # Not a valid IP literal — might be a hostname; try getnameinfo
+ try:
+ if (
+ socket.getnameinfo((host, port), socket.NI_NOFQDN)[0]
+ == "localhost"
+ ):
+ return "localhost"
+ except Exception:
+ pass
+ return host
+
+
def _startup_url(state: AppStateBase) -> str:
host = state.host.strip(
"[]"
) # normalize: remove brackets if user passed [addr]
port = state.port
- try:
- # pretty printing:
- # if the address maps to localhost, print "localhost" to stdout
- if (
- socket.getnameinfo((host, port), socket.NI_NOFQDN)[0]
- == "localhost"
- ):
- host = "localhost"
- except Exception:
- # aggressive try/except in case of platform-specific quirks;
- # nothing to handle, since the `try` logic is just for pretty
- # printing the host name
- ...
# Strip IPv6 zone ID (e.g. fe80::1%eth0 -> fe80::1); zone IDs are
- # interface-specific and not valid in URLs
- url_host_bare = host.split("%")[0]
+ # interface-specific and not valid in URLs.
+ # Must happen before getnameinfo — zone IDs can cause it to hang
+ # on Windows.
+ host = host.split("%")[0]
+
+ # pretty printing: show "localhost" for loopback addresses
+ host = _pretty_print_host(host, port)
+
+ url_host_bare = host
# IPv6 addresses must be wrapped in brackets in URLs (RFC 3986)
url_host = f"[{url_host_bare}]" if ":" in url_host_bare else url_host_bare
url = f"http://{url_host}:{port}{state.base_url}"
@@ -256,18 +275,11 @@ def _mcp_startup_url(state: AppStateBase) -> str:
port = state.port
base_url = state.base_url
- # Handle localhost pretty printing (same logic as _startup_url)
- try:
- if (
- socket.getnameinfo((host, port), socket.NI_NOFQDN)[0]
- == "localhost"
- ):
- host = "localhost"
- except Exception:
- ...
-
- # Strip IPv6 zone ID; IPv6 addresses must be wrapped in brackets (RFC 3986)
- url_host_bare = host.split("%")[0]
+ # Strip zone ID, then pretty-print loopback (same logic as _startup_url)
+ host = host.split("%")[0]
+ host = _pretty_print_host(host, port)
+
+ url_host_bare = host
url_host = f"[{url_host_bare}]" if ":" in url_host_bare else url_host_bare
# Construct MCP endpoint URL
mcp_prefix = "/mcp"