Skip to content

Unity Editor crashing #238

@gravitydeepimpactsun

Description

@gravitydeepimpactsun

Getting random crashes when using Unity (6000.2.0f1), see attached editor crash log:

mcp crashes.txt

Here's what RooCode (AI) said:

Based on the crash logs and examining the MCPForUnityBridge.cs file, I can identify several potential causes for the Unity crashes you're experiencing:

Root Cause Analysis

The crash is occurring during Unity's assembly reload process, specifically when the MCP bridge is shutting down. Here are the key issues:

1. Race Condition in Stop Method

The Stop() method (lines 328-353) has a potential race condition:

private static void Stop()
{
    lock (startStopLock)  // Good, has locking
    {
        if (!isRunning) return;
        
        // This sets isRunning = false immediately
        isRunning = false;
        
        // But background tasks might still be running...
        WriteHeartbeat(false);  // File I/O during shutdown
        listener?.Stop();       // TCP cleanup
        listener = null;
        
        // This could be called while ProcessCommands is executing
        EditorApplication.update -= ProcessCommands;
        
        Debug.Log("...stopped.");  // Line 346 where crash occurs
    }
}

2. Background Task Management

The code starts a background task with Task.Run(ListenerLoop) (line 314) but never properly cancels or awaits it during shutdown. This can cause:

  • Lingering TCP connections
  • Background threads continuing to execute during domain reload
  • Memory access violations when the domain is being unloaded

3. File I/O During Shutdown

The WriteHeartbeat() calls during shutdown (lines 342, 693) perform file operations that can conflict with Unity's domain reload process.

4. Event Handler Cleanup

The EditorApplication.update -= ProcessCommands (line 345) can be problematic if ProcessCommands is currently executing when the delegate is removed.

Specific Crash Pattern

Your logs show this sequence:

  1. StopAssetImportingV2 triggers asset pipeline refresh
  2. MCP bridge receives OnBeforeAssemblyReload() callback
  3. Stop() method executes and logs at line 346
  4. Native crash occurs during the shutdown sequence

Recommendations to Fix

The script is likely contributing to crashes due to improper cleanup during assembly reloads. Here are the main fixes needed:

  1. Add proper task cancellation - Use CancellationTokenSource to cancel background tasks
  2. Ensure thread safety - The background ListenerLoop task needs proper cancellation handling
  3. Remove file I/O during shutdown - Move heartbeat writes to a safer location
  4. Add exception handling - Wrap cleanup operations in try-catch blocks
  5. Use proper async/await patterns - Replace Task.Run with more controlled task management

The native crash indicates memory corruption or access violations, likely caused by the background task continuing to run while the Unity domain is being unloaded. This is a classic issue with Unity editor scripts that don't properly handle domain reload events.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions