[Bug]: Orphaned test job after domain reload permanently blocks run_tests; clear_stuck is not exposed
What happened?
A test job can remain recorded as running after a Unity domain reload even though Unity reports that no tests are running.
The resulting state is internally inconsistent:
editor_state.tests.is_running == false- Unity is idle, connected, and ready for tools
TestJobManager.CurrentJobIdstill references a persisted jobget_test_jobcontinues returningstatus: running- Every subsequent
run_testscall returnstests_running
Waiting does not resolve the condition. Recovery requires restarting Unity or bypassing the public MCP API to invoke TestJobManager.ClearStuckJob().
This appears to be an incomplete edge case in the recovery added by PR #554. TryRestoreFromSessionState() checks whether the restored job is more than five minutes stale only once, during static initialization. If the domain reload occurs while the job is younger than five minutes, the job is retained. Nothing reevaluates it when it later becomes stale.
GetJob() does not resolve this case when TotalTests is already populated because its initialization-timeout path applies only when TotalTests == null.
There is also an implemented recovery path in the Unity handler:
RunTests.HandleCommand()accepts the undocumentedclear_stuckparameter.TestJobManager.ClearStuckJob()clears the orphaned job.
However, the Python run_tests MCP tool does not expose or forward clear_stuck, so MCP clients and agents cannot use the existing recovery mechanism.
Relevant sources:
- TestJobManager.cs
- Unity RunTests.cs handler
- Python run_tests wrapper
- PR #554: domain-reload stale-job recovery
- PR #1067: related test-state lifecycle leak
Expected behavior:
Once the editor reports that no test run is active and a persisted job exceeds the stale threshold, the orphaned job should be marked failed/cancelled and _currentJobId should be cleared. Alternatively, a safe public recovery operation should be available.
Actual behavior:
The job remains running indefinitely and blocks all future MCP test runs.
Reproduction steps
- Start an MCP test job and allow
RunStartedto populateTotalTests. - Trigger a Unity assembly/domain reload before the job receives normal
RunFinishedfinalization. - Allow
TestJobManagerto restore while the persisted job'sLastUpdateUnixMsis less than five minutes old. - After Unity reconnects, observe that
editor_state.tests.is_runningisfalse, while the persisted job andCurrentJobIdremainrunning. - Wait more than five minutes without another domain reload.
- Poll the old job with
get_test_job; it still reportsrunning. - Call
run_testsagain; it returnstests_running. - Observe that no public MCP parameter or tool can invoke the Unity-side
clear_stuckrecovery.
A restart or a direct reflective call to TestJobManager.ClearStuckJob() clears the condition, confirming that the retained coordinator state is the blocker.
Unity version
6000.4.0f1
MCP for Unity package version
10.1.1-beta.1
Python server version
10.1.0
MCP client
Codex
Transport
HTTP (default)
OS
Windows
Relevant logs / console output
editor_state:
tests.is_running: false
editor idle/ready: true
current_job_id: <job-id>
get_test_job(<job-id>):
status: running
completed: 0
total: 1
last_update_unix_ms: <stale timestamp>
run_tests(...):
error: tests_running
reason: tests_running
Checks
- [x] I searched existing issues and did not find a duplicate
- [x] I included logs / steps to reproduce
Suggested fix:
- Reconcile persisted test-job state again when starting or querying jobs, rather than only during static restoration.
- Clear an orphan only when:
- Unity reports no active test run,
- the editor is not compiling/updating,
- the current job ID matches the expected job,
- and its last update exceeds the configured stale threshold.
- Expose a dedicated recovery operation such as
clear_test_job(expected_job_id). - Ensure recovery bypasses the normal
requires_no_testspreflight. - Add regression tests for a job restored before the stale threshold and queried after it passes, plus protection against clearing a newer active job.