Skip to content

Drop a stale active-instance pin instead of failing forever#1266

Open
lgarczyn wants to merge 1 commit into
CoplayDev:betafrom
lgarczyn:fix/stale-instance-pin
Open

Drop a stale active-instance pin instead of failing forever#1266
lgarczyn wants to merge 1 commit into
CoplayDev:betafrom
lgarczyn:fix/stale-instance-pin

Conversation

@lgarczyn

@lgarczyn lgarczyn commented Jul 13, 2026

Copy link
Copy Markdown

A pin outlives the editor it names.
While it stayed pinned it also suppressed auto-select. Every later call then failed with no_unity_session. Neither waiting nor relaunching the editor recovered. The editor re-registers under its own name, not the name the pin holds. The pin is now dropped once another instance is registered. An empty registry means a domain reload is in flight, so the pin is kept.

Description

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Refactoring (no functional changes)
  • Test update

Changes Made

Compatibility / Package Source

  • Unity version(s) tested:
  • Package source used (#beta, #main, tag, branch, or file:):
  • Resolved commit hash from Packages/packages-lock.json (if using a Git package URL):

Testing/Screenshots/Recordings

  • Python tests (cd Server && uv run pytest tests/ -v)
  • Unity EditMode tests
  • Unity PlayMode tests
  • Package import/compile check
  • Not applicable (explain why in Additional Notes)

Documentation Updates

  • I have added/removed/modified tools or resources
  • If yes, I have updated all documentation files using:
    • The LLM prompt at tools/UPDATE_DOCS_PROMPT.md (recommended)
    • Manual review of the generated changes

Related Issues

Additional Notes

Summary by CodeRabbit

  • Bug Fixes
    • Automatically clears session selections for Unity instances that are no longer available.
    • Preserves selections during temporary periods when no instances are registered.
    • Allows automatic selection of an available instance after a stale selection is removed.

A pin outlives the editor it names.
While it stayed pinned it also suppressed auto-select.
Every later call then failed with no_unity_session.
Neither waiting nor relaunching the editor recovered.
The editor re-registers under its own name, not the name the pin holds.
The pin is now dropped once another instance is registered.
An empty registry means a domain reload is in flight, so the pin is kept.
@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

UnityInstanceMiddleware now removes stale HTTP session pins when their Unity instance is no longer registered, while preserving valid pins and allowing auto-selection to continue. New async tests cover stale, valid, empty-registry, and fallback scenarios.

Changes

Stale pin cleanup

Layer / File(s) Summary
Detect and clear stale pins
Server/src/transport/unity_instance_middleware.py
The middleware validates active instance pins against discovered instances and clears stale state before auto-selection.
Validate pin lifecycle and fallback
Server/tests/test_stale_instance_pin.py
Tests cover stale-pin removal, valid-pin retention, empty-registry retention, and fallback to the sole live instance.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested labels: bug

Suggested reviewers: scriptwonder, dsarno, whatevertogo, msanatan

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: dropping stale active-instance pins to avoid persistent failures.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
Server/src/transport/unity_instance_middleware.py (1)

215-243: 🚀 Performance & Scalability | 🔵 Trivial

Stale-pin detection adds a PluginHub.get_sessions() round-trip to every HTTP request with a pinned instance.

The logic is correct — empty registry preserves the pin, a registered pin is kept, and a stale pin is dropped and cleared. However, _drop_stale_pin calls _discover_instances (which calls PluginHub.get_sessions) on every request that has a non-null active_instance, even when the pin is valid. This adds a network round-trip to the hot path alongside the existing PluginHub._resolve_session_id call at line 403.

Consider caching the discovery result for a short TTL (similar to the _tool_visibility_refresh_interval_seconds pattern already used in this class) or reusing the session data fetched here to avoid the redundant _resolve_session_id call when the pin is confirmed valid.
[medium_effort_and_high_reward]

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Server/src/transport/unity_instance_middleware.py` around lines 215 - 243,
The _drop_stale_pin path performs an extra PluginHub.get_sessions round-trip on
every request with an active pin. Add short-TTL caching for _discover_instances
using the class’s existing _tool_visibility_refresh_interval_seconds pattern, or
reuse the fetched session data through the subsequent request flow to avoid
redundant discovery/resolution when the pin is valid; preserve empty-registry
and stale-pin clearing behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@Server/src/transport/unity_instance_middleware.py`:
- Around line 215-243: The _drop_stale_pin path performs an extra
PluginHub.get_sessions round-trip on every request with an active pin. Add
short-TTL caching for _discover_instances using the class’s existing
_tool_visibility_refresh_interval_seconds pattern, or reuse the fetched session
data through the subsequent request flow to avoid redundant discovery/resolution
when the pin is valid; preserve empty-registry and stale-pin clearing behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5adc114b-949f-4895-b72a-f095dd01dd9d

📥 Commits

Reviewing files that changed from the base of the PR and between dcf74e1 and a8cd30a.

📒 Files selected for processing (2)
  • Server/src/transport/unity_instance_middleware.py
  • Server/tests/test_stale_instance_pin.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant