Skip to content

fix: guard MigrateFrom1To2 against DirectoryNotFoundException on init - #216

Merged
kaan-barmore-genc-mixpanel merged 1 commit into
masterfrom
kaanbarmore-genc-sdk-133-unity-sdk-init-fails-with-directorynotfoundexception
Aug 4, 2026
Merged

kaan-barmore-genc-mixpanel merged 1 commit into
masterfrom
kaanbarmore-genc-sdk-133-unity-sdk-init-fails-with-directorynotfoundexception

Conversation

@kaan-barmore-genc-mixpanel

Copy link
Copy Markdown
Contributor

Summary

Fixes SDK-133. Customers hit a recurring init failure in the Unity SDK: Mixpanel fallback initialization failed: System.IO.DirectoryNotFoundException ... /mp_state.json.

Root cause

MigrateFrom1To2() in Controller.cs deleted legacy v1 files (mp_state.json, mp_super_properties.json) with File.Delete inside finally blocks — no File.Exists guard, and outside the protecting catch. File.Delete only tolerates a missing file when the parent directory exists, so when Application.persistentDataPath is empty or its directory is unavailable, the delete throws DirectoryNotFoundException. Running in finally meant the surrounding catch did not swallow it, so the exception escaped Initialize(), left _fullyInitialized == false, and Start() took the fallback path and logged the customer-visible failure.

persistentDataPath can be empty when read off the Unity main thread, on tvOS, or with a malformed ProductName/CompanyName.

Fix

  • Route both deletes through a DeleteLegacyFileBestEffort helper that guards File.Exists and swallows exceptions — legacy cleanup is best-effort and must never abort init.
  • Skip migration cleanly when persistentDataPath is empty, without marking migration done, so a later init can still run it.

Testing

No automated coverage — the repo has a single EditMode test (Identify_Test) and CI does not run it (Unity license required). Nothing exercises the migration path. Verified by inspection of the throw semantics.

The v1-to-v2 migration deleted legacy files (mp_state.json,
mp_super_properties.json) via File.Delete inside finally blocks with no
File.Exists guard and outside the protecting catch. File.Delete only
tolerates a missing file when the parent directory exists, so when
Application.persistentDataPath is empty or its directory is unavailable
the delete throws DirectoryNotFoundException. Because it ran in finally,
the surrounding catch did not swallow it, so the exception escaped
Initialize(), left the SDK uninitialized, and made Start() take the
fallback path and log 'Mixpanel fallback initialization failed'.

Route both deletes through a best-effort helper that guards File.Exists
and swallows exceptions, and skip migration when persistentDataPath is
empty (without marking it done, so a later init can still run it).

Fixes SDK-133
@linear-code

linear-code Bot commented Jul 28, 2026

Copy link
Copy Markdown

SDK-133

Comment thread Mixpanel/Controller.cs
@greptile-apps

greptile-apps Bot commented Jul 28, 2026

Copy link
Copy Markdown

Confidence Score: 3/5

The retry behavior should be fixed before merging because a transiently empty storage path can permanently skip migration of identity and consent state.

The new early return preserves the migration flag but both callers subsequently set _fullyInitialized, preventing normal later initialization attempts from retrying the migration.

Files Needing Attention: Mixpanel/Controller.cs

Important Files Changed

Filename Overview
Mixpanel/Controller.cs Safely handles legacy-file cleanup, but an empty-path skip is not retried after initialization becomes complete.

Reviews (1): Last reviewed commit: "fix: guard MigrateFrom1To2 against Direc..." | Re-trigger Greptile

@jaredmixpanel
jaredmixpanel removed their request for review July 28, 2026 21:43
@kaan-barmore-genc-mixpanel
kaan-barmore-genc-mixpanel merged commit acf29f5 into master Aug 4, 2026
2 checks passed
@kaan-barmore-genc-mixpanel
kaan-barmore-genc-mixpanel deleted the kaanbarmore-genc-sdk-133-unity-sdk-init-fails-with-directorynotfoundexception branch August 4, 2026 20:51
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.

2 participants