Skip to content

Commit 0a8591f

Browse files
sakurachanclaude
andcommitted
Fix instance sorting and logging issues
- Fix sorting logic for instances without heartbeat data: use epoch timestamp instead of current time to properly deprioritize instances with None last_heartbeat - Use logger.exception() instead of logger.error() in disconnect_all() to include stack traces for better debugging 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 13b8fb2 commit 0a8591f

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

MCPForUnity/UnityMcpServer~/src/unity_connection.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,9 @@ def _resolve_instance_id(self, instance_identifier: Optional[str], instances: Li
468468
logger.debug(f"Using default instance: {instance_identifier}")
469469
else:
470470
# Use the most recently active instance
471-
sorted_instances = sorted(instances, key=lambda i: i.last_heartbeat or time.time(), reverse=True)
471+
# Instances with no heartbeat (None) should be sorted last (use epoch as sentinel)
472+
from datetime import datetime
473+
sorted_instances = sorted(instances, key=lambda i: i.last_heartbeat or datetime.fromtimestamp(0), reverse=True)
472474
logger.info(f"No instance specified, using most recent: {sorted_instances[0].id}")
473475
return sorted_instances[0]
474476

@@ -592,8 +594,8 @@ def disconnect_all(self):
592594
try:
593595
logger.info(f"Disconnecting from Unity instance: {instance_id}")
594596
conn.disconnect()
595-
except Exception as e:
596-
logger.error(f"Error disconnecting from {instance_id}: {e}")
597+
except Exception:
598+
logger.exception(f"Error disconnecting from {instance_id}")
597599
self._connections.clear()
598600

599601

Server/unity_connection.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,9 @@ def _resolve_instance_id(self, instance_identifier: Optional[str], instances: Li
468468
logger.debug(f"Using default instance: {instance_identifier}")
469469
else:
470470
# Use the most recently active instance
471-
sorted_instances = sorted(instances, key=lambda i: i.last_heartbeat or time.time(), reverse=True)
471+
# Instances with no heartbeat (None) should be sorted last (use epoch as sentinel)
472+
from datetime import datetime
473+
sorted_instances = sorted(instances, key=lambda i: i.last_heartbeat or datetime.fromtimestamp(0), reverse=True)
472474
logger.info(f"No instance specified, using most recent: {sorted_instances[0].id}")
473475
return sorted_instances[0]
474476

@@ -592,8 +594,8 @@ def disconnect_all(self):
592594
try:
593595
logger.info(f"Disconnecting from Unity instance: {instance_id}")
594596
conn.disconnect()
595-
except Exception as e:
596-
logger.error(f"Error disconnecting from {instance_id}: {e}")
597+
except Exception:
598+
logger.exception(f"Error disconnecting from {instance_id}")
597599
self._connections.clear()
598600

599601

0 commit comments

Comments
 (0)