Skip to content

fix: skip unsafe Fusion serialization types#1127

Open
cyanxwh wants to merge 1 commit into
CoplayDev:betafrom
StarRiverVR:codex/fusion-unsafe-type-serialization
Open

fix: skip unsafe Fusion serialization types#1127
cyanxwh wants to merge 1 commit into
CoplayDev:betafrom
StarRiverVR:codex/fusion-unsafe-type-serialization

Conversation

@cyanxwh
Copy link
Copy Markdown
Contributor

@cyanxwh cyanxwh commented May 15, 2026

Description

Fixes component serialization crashes/disconnects caused by Photon Fusion IL-weaved unsafe types being accessed through reflection.

Type of Change

Save your change type

  • Bug fix (non-breaking change that fixes an issue)
  • Test update

Changes Made

  • Skip known Photon Fusion unsafe IL-weaved types before reading component fields/properties via reflection.
  • Recursively skip arrays/generic containers that wrap unsafe Fusion types.
  • Use reflective Type.IsByRefLike detection so older supported Unity runtimes can still compile.
  • Add an EditMode regression test covering direct and nested Fusion unsafe container types.

Testing/Screenshots/Recordings

  • git diff --check
  • Unity 2022.3.62f1 EditMode: FusionUnsafeTypeSerializationTests.GetComponentData_SkipsFusionUnsafeTypesInsideContainers passed (total=1 passed=1 failed=0)
  • Unity 6000.0.41f1 EditMode: FusionUnsafeTypeSerializationTests.GetComponentData_SkipsFusionUnsafeTypesInsideContainers passed (total=1 passed=1 failed=0)
  • Manual UnityMCP validation in D:\Source\UnityProjects\Gorimon with local package deployment:
    • Read scene-instantiated Fusion components from NetworkBall / NetworkRig_SharedMode successfully.
    • Read prefab-stage properties for NetworkBall.prefab and NetworkRig_SharedMode.prefab successfully.
    • Verified Fusion.NetworkObject, Fusion.NetworkTransform, Fusion.Addons.Physics.NetworkRigidbody3D, Fusion.XR.Host.Rig.NetworkRig, and Gorimon custom NetworkBehaviour components without Editor crash.

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 updates following the guide at tools/UPDATE_DOCS.md

Related Issues

Fixes #1128

Additional Notes

The project declares Unity 2021.3 support, but Unity 2021.3.45f2 is not installed on this machine. I avoided a direct dependency on Type.IsByRefLike by using reflection and validated on the installed 2022.3.62f1 and 6000.0.41f1 editors.

Summary by CodeRabbit

  • Bug Fixes

    • Prevented Editor crashes during component serialization by detecting and safely skipping problematic types.
  • Tests

    • Added test coverage for serialization safety across direct and nested type scenarios.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 15, 2026

📝 Walkthrough

Walkthrough

This PR adds a safety mechanism to GameObjectSerializer that detects and skips reflection-unsafe types—including Fusion IL-weaved types, pointers, by-ref types, and ref-like value types—during component serialization. The check recursively inspects generic arguments and array elements to catch unsafe types nested in containers, then integrates into property and field metadata caching. A new NUnit test verifies the mechanism correctly excludes direct and nested Fusion types from serialized output.

Changes

Unsafe Type Detection and Filtering

Layer / File(s) Summary
Unsafe Type Detection Mechanism
MCPForUnity/Editor/Helpers/GameObjectSerializer.cs
Introduces IsUnsafeType(...) helper and supporting caches (_crashingTypeNames, _isByRefLikeProperty) to classify pointer types, by-ref types, ref-like value types (via Type.IsByRefLike), known Fusion IL-weaved types, and recursively checks generic arguments and element types (arrays, generic containers) for unsafe inner types.
Property and Field Metadata Caching
MCPForUnity/Editor/Helpers/GameObjectSerializer.cs
During GetComponentData metadata caching, property and field reflection collection now skip members whose type is detected as unsafe by IsUnsafeType, preventing subsequent reflection access and JSON conversion on problematic members.
Test Coverage and Test Components
TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/FusionUnsafeTypeSerializationTests.cs, .meta
Defines placeholder Fusion types (NetworkBehaviourBuffer, Changed<T>), a FusionUnsafeTypeComponent MonoBehaviour with both safe and Fusion-typed fields (including nested containers), and an NUnit test that verifies GameObjectSerializer.GetComponentData excludes direct and nested Fusion unsafe types while preserving safe fields. Includes cleanup via DestroyImmediate in a finally block.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • CoplayDev/unity-mcp#762: Both PRs modify GameObjectSerializer.cs's GetComponentData property and field iteration to skip specific problematic component fields (unsafe Fusion types in the main PR vs Collider's GeometryHolder in the related PR).

Suggested labels

safe-to-test

Suggested reviewers

  • dsarno
  • msanatan

Poem

🐰 A rabbit hops through the serialization maze,
Unsafe types detected in a safety craze!
Fusion pointers banished, containers purified,
Reflection crashes halted, now safely beautified!
Tests verify the bounty—not a crash in sight! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely summarizes the main change: skipping unsafe Fusion serialization types to fix crashes.
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.
Description check ✅ Passed The PR description covers all required template sections: description, type of change, changes made, testing details, documentation updates, related issues, and additional notes.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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 and usage tips.

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.

Component serialization can crash or disconnect UnityMCP when reading Photon Fusion components

1 participant