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.

test(cli): avoid slow refused connections when selecting ports (#11152)

Shahmir Varqha036e1110a246
diff --git a/tests/_cli/test_cli.py b/tests/_cli/test_cli.py
index 940589eb2f..0a860c943f 100644
--- a/tests/_cli/test_cli.py
+++ b/tests/_cli/test_cli.py
@@ -13,7 +13,6 @@ import json
import os
import shutil
import signal
-import socket
import subprocess
import sys
import tempfile
@@ -44,6 +43,7 @@ from marimo._server.workspace import (
)
from marimo._templates import get_version
from marimo._utils.env import is_env_true
+from marimo._utils.net import find_free_port
from marimo._utils.platform import is_windows
from marimo._utils.toml import toml_reader
@@ -216,16 +216,7 @@ def _check_contents(
def _get_port() -> int:
- port = 2718
- max_tries = 25
- for _ in range(max_tries):
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
- in_use = s.connect_ex(("localhost", port)) == 0
- if in_use:
- port += 1
- else:
- return port
- raise OSError("Could not find an unused port.")
+ return find_free_port(2718, attempts=25, addr="127.0.0.1")
def _read_toml(filepath: Path) -> dict[str, Any] | None: