Skip to content
Merged
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
20 changes: 18 additions & 2 deletions src/inferrsi/harness/executor/serving/box_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,33 @@ def __init__(self, owner):
self.reset_items = owner.profile.reset_items

def start(self, deadline):
saw_socket = False
last_refused = None
while time.monotonic() < min(deadline, self.owner.end):
self.owner.require_owner()
if self.owner.socket.is_socket():
startup = self.owner.control({"action": "startup"}, deadline)
saw_socket = True
try:
startup = self.owner.control({"action": "startup"}, deadline)
except BoxSearchError as error:
# bind() exposes the Unix socket path before listen() activates it.
if isinstance(error.__cause__, ConnectionRefusedError):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议保留最后一次 startup 连接拒绝作为最终超时的原因,或在诊断里区分「没有发现 socket」和「发现 socket 但一直未就绪」,仍保持 TimeoutError 及现有绝对截止时间。现在两种情况都会落到同一条超时消息;CI 下次再抖时,会少一个定位线索。

last_refused = error.__cause__
time.sleep(0.01)
continue
raise
if startup != {"pid": self.owner.process.pid, **self.owner.provenance(
self.owner.loaded_revision, self.owner.parent_hash, self.owner.start_identity
)}:
raise BoxSearchError("Box readiness did not bind the loaded parent and owner")
return self.owner.start_identity
time.sleep(0.01)
raise TimeoutError("Box service startup deadline expired")
if last_refused is not None:
raise TimeoutError(
"Box service startup deadline expired (socket refused connections)"
) from last_refused
detail = "socket never appeared" if not saw_socket else "socket not ready"
raise TimeoutError(f"Box service startup deadline expired ({detail})")

def exchange(self, data, deadline, cancellation):
return encode(self.owner.control(
Expand Down
116 changes: 114 additions & 2 deletions tests/harness/test_managed_box_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ def campaign(
if guard is not None:
configuration["reference_guard"] = guard
configuration["conditions"]["effective_config"] = {"fault": fault}
configuration["sampling"]["stage_timeout_seconds"] = 1.5
configuration["sampling"]["stage_timeout_seconds"] = (
1.5 if fault in ("timeout", "undrained_stream", "undrained_restore") else 10.0
)
configuration["scenarios"] = [
{"id": "a", "workload": "synthetic", "requests": 2, "required_successes": 2}
]
Expand Down Expand Up @@ -555,4 +557,114 @@ def test_vanished_launcher_proves_no_service_and_allows_search_retry(tmp_path, h
pass

launcher.with_suffix(".held").rename(launcher)
assert screen(root, candidates, profile if hot else None, lease)["status"] == "completed"
retried = screen(root, candidates, profile if hot else None, lease)
assert retried["status"] == "completed", retried


def test_attached_lab_start_retries_connection_refused_between_bind_and_listen(tmp_path):
"""Startup polling tolerates bind()-before-listen() but fails fast on owner/protocol errors."""
sock_path = tmp_path / "box.sock"
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
server.bind(str(sock_path))
identity = Identity("inst", "act", "participant", 0, "epoch")
provenance = {"profile": "p", "revision": "r1"}
expected_reply = {"pid": os.getpid(), **provenance}
refused = threading.Event()
accepted = threading.Event()
refused_count = 0

class _FakeOwner:
profile = SimpleNamespace(reset_items=("requests",))
socket = sock_path
end = time.monotonic() + 5.0
process = SimpleNamespace(pid=os.getpid())
loaded_revision = "r1"
parent_hash = "h1"
start_identity = identity
cancellation = None
owner_lost = False
forced_error = None
control_calls = 0

def require_owner(self):
if self.owner_lost:
raise box_search.BoxSearchError("managed Box process owner was lost")

def provenance(self, revision, artifact, ident):
return provenance

def control(self, message, deadline):
nonlocal refused_count
self.control_calls += 1
if self.forced_error is not None:
raise box_search.BoxSearchError("managed Box control channel failed") from (
self.forced_error
)
try:
return box_search.ManagedBoxSearch.control(self, message, deadline)
except box_search.BoxSearchError as error:
if isinstance(error.__cause__, ConnectionRefusedError):
refused_count += 1
refused.set()
raise

def _listen_after_refusal():
if not refused.wait(timeout=2.0):
return
server.listen(1)
conn, _ = server.accept()
with conn:
conn.recv(4096)
frame = encode({"schema_version": SCHEMA_VERSION, "result": expected_reply}) + b"\n"
conn.sendall(frame)
accepted.set()

owner = _FakeOwner()
lab = box_search._AttachedLab(owner)
worker = threading.Thread(target=_listen_after_refusal, daemon=True)
worker.start()
try:
assert lab.start(time.monotonic() + 2.0) == identity
assert accepted.wait(timeout=1.0)
assert refused_count >= 1
finally:
worker.join(timeout=1.0)
server.close()

# A bound socket that never listens expires with the last ConnectionRefusedError cause.
hung_path = tmp_path / "hung.sock"
hung = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
hung.bind(str(hung_path))
owner.socket = hung_path
try:
with pytest.raises(
TimeoutError, match="startup deadline expired \\(socket refused connections\\)"
) as exc:
lab.start(time.monotonic() + 0.05)
assert isinstance(exc.value.__cause__, ConnectionRefusedError)

# A socket that never appears reports a distinct startup timeout diagnostic.
owner.socket = tmp_path / "missing.sock"
with pytest.raises(
TimeoutError, match="startup deadline expired \\(socket never appeared\\)"
) as missing_exc:
lab.start(time.monotonic() + 0.03)
assert missing_exc.value.__cause__ is None

# Non-ConnectionRefusedError control failures abort immediately without retrying.
owner.socket = hung_path
owner.control_calls = 0
owner.forced_error = BrokenPipeError("peer reset")
with pytest.raises(box_search.BoxSearchError, match="control channel failed"):
lab.start(time.monotonic() + 2.0)
assert owner.control_calls == 1

# Lost process owner aborts immediately on require_owner() without calling control().
owner.forced_error = None
owner.control_calls = 0
owner.owner_lost = True
with pytest.raises(box_search.BoxSearchError, match="owner was lost"):
lab.start(time.monotonic() + 2.0)
assert owner.control_calls == 0
finally:
hung.close()
Loading