Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions sdks/python/pmxt/server_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@

import os
import json
import logging
import time
import subprocess
import shutil
import threading
from pathlib import Path
from typing import List, Optional, Dict, Any
import urllib.request
import urllib.error

logger = logging.getLogger(__name__)


class ServerManager:
Expand Down Expand Up @@ -495,7 +497,8 @@ def _check_health(self, port: int, timeout: int = 2) -> bool:
return data.get('status') == 'ok'

return False
except (urllib.error.URLError, urllib.error.HTTPError, Exception):
except (OSError, json.JSONDecodeError) as exc:
logger.debug("Health check failed for port %s: %s", port, exc)
return False

def get_server_info(self) -> Optional[Dict[str, Any]]:
Expand Down
29 changes: 29 additions & 0 deletions sdks/python/tests/test_server_manager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import logging
import urllib.error

import pytest

from pmxt.server_manager import ServerManager
Expand All @@ -20,3 +23,29 @@ def test_wait_for_health_requires_current_home_lock(monkeypatch, tmp_path):

with pytest.raises(Exception, match="Server failed to become healthy"):
manager._wait_for_health()


def test_check_health_logs_expected_request_failures(monkeypatch, caplog):
manager = ServerManager()

def fail_request(*args, **kwargs):
raise urllib.error.URLError("connection refused")

monkeypatch.setattr("pmxt.server_manager.urllib.request.urlopen", fail_request)

with caplog.at_level(logging.DEBUG, logger="pmxt.server_manager"):
assert manager._check_health(3847) is False

assert "Health check failed for port 3847" in caplog.text


def test_check_health_does_not_mask_unexpected_errors(monkeypatch):
manager = ServerManager()

def fail_request(*args, **kwargs):
raise RuntimeError("unexpected failure")

monkeypatch.setattr("pmxt.server_manager.urllib.request.urlopen", fail_request)

with pytest.raises(RuntimeError, match="unexpected failure"):
manager._check_health(3847)