-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(stdio): recover from a reload-orphaned socket instead of hanging #1217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mertakdut
wants to merge
3
commits into
CoplayDev:beta
Choose a base branch
from
mertakdut:beta
base: beta
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+163
−19
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| """Connection resilience when a domain reload leaves the Unity socket half-open.""" | ||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import socket | ||
| import threading | ||
| import time | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from core.config import config | ||
| import transport.legacy.unity_connection as uc | ||
| from transport.legacy.unity_connection import UnityConnection | ||
|
|
||
|
|
||
| def _start_silent_bridge(): | ||
| """Accept and handshake, then never answer (a wedged half-open bridge).""" | ||
| srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
| srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
| srv.bind(("127.0.0.1", 0)) | ||
| srv.listen(8) | ||
| port = srv.getsockname()[1] | ||
| accepted: list[socket.socket] = [] | ||
| stop = threading.Event() | ||
|
|
||
| def serve(): | ||
| srv.settimeout(0.5) | ||
| while not stop.is_set(): | ||
| try: | ||
| client, _ = srv.accept() | ||
| except socket.timeout: | ||
| continue | ||
| except OSError: | ||
| break | ||
| accepted.append(client) | ||
| try: | ||
| client.sendall(b"WELCOME UNITY-MCP 1 FRAMING=1\n") | ||
| except OSError: | ||
| pass | ||
|
|
||
| threading.Thread(target=serve, daemon=True).start() | ||
| return srv, port, stop, accepted | ||
|
|
||
|
|
||
| def _write_reloading_status(tmp_path): | ||
| status_dir = Path(tmp_path) / ".unity-mcp" | ||
| status_dir.mkdir(parents=True, exist_ok=True) | ||
| (status_dir / "unity-mcp-status-deadbeef.json").write_text( | ||
| json.dumps({"reloading": True, "reason": "reloading", "project_path": "x"}) | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def silent_bridge(monkeypatch, tmp_path): | ||
| srv, port, stop, accepted = _start_silent_bridge() | ||
| monkeypatch.setattr(uc.Path, "home", lambda: Path(tmp_path)) | ||
| monkeypatch.setattr(uc.stdio_port_registry, "get_port", lambda instance_id=None: port) | ||
| monkeypatch.setattr(uc.stdio_port_registry, "get_instance", lambda instance_id: None) | ||
| monkeypatch.setattr(config, "connection_timeout", 1.0) | ||
| monkeypatch.setattr(config, "command_total_timeout", 2.0, raising=False) | ||
| try: | ||
| yield port | ||
| finally: | ||
| stop.set() | ||
| try: | ||
| srv.close() | ||
| except OSError: | ||
| pass | ||
| for client in accepted: | ||
| try: | ||
| client.close() | ||
| except OSError: | ||
| pass | ||
|
|
||
|
|
||
| def test_send_command_against_wedged_socket_is_bounded(silent_bridge, monkeypatch): | ||
| """A connection_timeout longer than the total budget must not let a single blocking | ||
| recv overrun the ceiling: the deadline caps each recv, so the call still stops near | ||
| command_total_timeout rather than connection_timeout.""" | ||
| monkeypatch.setattr(config, "connection_timeout", 5.0) | ||
| conn = UnityConnection(port=silent_bridge, instance_id="Repro@deadbeef") | ||
| start = time.monotonic() | ||
| with pytest.raises(TimeoutError, match="exceeded total deadline"): | ||
| conn.send_command("get_editor_state", {}) | ||
| assert time.monotonic() - start < 4.0 | ||
|
|
||
|
|
||
| def test_send_command_with_retry_is_bounded_by_deadline(silent_bridge, tmp_path, monkeypatch): | ||
| """The reload-wait loop honours command_total_timeout, not just max_wait_s.""" | ||
| conn = UnityConnection(port=silent_bridge, instance_id="Repro@deadbeef") | ||
| monkeypatch.setattr(uc, "get_unity_connection", lambda instance_id=None: conn) | ||
| _write_reloading_status(tmp_path) | ||
|
|
||
| start = time.monotonic() | ||
| resp = uc.send_command_with_retry("get_editor_state", {}, instance_id="Repro@deadbeef") | ||
| assert time.monotonic() - start < 6.0 | ||
| assert uc._extract_response_reason(resp) == "reloading" | ||
|
|
||
|
|
||
| def test_reload_signal_drops_socket_for_fresh_reconnect(silent_bridge, tmp_path): | ||
| """A 'reloading' status drops the socket so the next command reconnects.""" | ||
| conn = UnityConnection(port=silent_bridge, instance_id="Repro@deadbeef") | ||
| assert conn.connect() is True | ||
| assert conn.sock is not None | ||
| _write_reloading_status(tmp_path) | ||
|
|
||
| response = conn.send_command("get_editor_state", {}) | ||
| assert conn.sock is None | ||
| assert uc._extract_response_reason(response) == "reloading" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.