Skip to content

Commit

Permalink
fix handling for bad host with prefect server start (#16960)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzstoatzz authored and kevingrismore committed Feb 7, 2025
1 parent cf729fa commit 8dd6e5f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/prefect/cli/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ def start(
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
except socket.gaierror:
exit_with_error(
f"Invalid host '{host}'. Please specify a valid hostname or IP address."
)
except socket.error:
if pid_file.exists():
exit_with_error(
Expand Down
20 changes: 19 additions & 1 deletion tests/cli/test_start_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def temporary_profiles_path(self, tmp_path: Path):
)
yield path

@pytest.fixture(autouse=True)
@pytest.fixture
def stop_server(self):
yield
invoke_and_assert(
Expand All @@ -334,6 +334,7 @@ def stop_server(self):
expected_code=0,
)

@pytest.mark.usefixtures("stop_server")
def test_switch_to_local_profile_by_default(self):
invoke_and_assert(
command=[
Expand All @@ -348,6 +349,7 @@ def test_switch_to_local_profile_by_default(self):
profiles = load_profiles()
assert profiles.active_name == "local"

@pytest.mark.usefixtures("stop_server")
def test_choose_when_multiple_profiles_have_same_api_url(self):
save_profiles(
profiles=ProfilesCollection(
Expand Down Expand Up @@ -378,3 +380,19 @@ def test_choose_when_multiple_profiles_have_same_api_url(self):

profiles = load_profiles()
assert profiles.active_name == "local"

def test_start_invalid_host(self):
"""Test that providing an invalid host returns a clear error message.
this is a regression test for https://github.com/PrefectHQ/prefect/issues/16950
"""
invoke_and_assert(
command=[
"server",
"start",
"--host",
"foo",
],
expected_output_contains="Invalid host 'foo'. Please specify a valid hostname or IP address.",
expected_code=1,
)

0 comments on commit 8dd6e5f

Please sign in to comment.