unity-mcp icon indicating copy to clipboard operation
unity-mcp copied to clipboard

[Bug]: Orphaned test job after domain reload permanently blocks run_tests; clear_stuck is not exposed

Open ascriven opened this issue 4 months ago • 1 comments

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.CurrentJobId still references a persisted job
  • get_test_job continues returning status: running
  • Every subsequent run_tests call returns tests_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 undocumented clear_stuck parameter.
  • 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:

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

  1. Start an MCP test job and allow RunStarted to populate TotalTests.
  2. Trigger a Unity assembly/domain reload before the job receives normal RunFinished finalization.
  3. Allow TestJobManager to restore while the persisted job's LastUpdateUnixMs is less than five minutes old.
  4. After Unity reconnects, observe that editor_state.tests.is_running is false, while the persisted job and CurrentJobId remain running.
  5. Wait more than five minutes without another domain reload.
  6. Poll the old job with get_test_job; it still reports running.
  7. Call run_tests again; it returns tests_running.
  8. Observe that no public MCP parameter or tool can invoke the Unity-side clear_stuck recovery.

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

ascriven avatar Jul 14 '26 19:07 ascriven

Suggested fix:

  1. Reconcile persisted test-job state again when starting or querying jobs, rather than only during static restoration.
  2. 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.
  3. Expose a dedicated recovery operation such as clear_test_job(expected_job_id).
  4. Ensure recovery bypasses the normal requires_no_tests preflight.
  5. Add regression tests for a job restored before the stale threshold and queried after it passes, plus protection against clearing a newer active job.

ascriven avatar Jul 14 '26 19:07 ascriven