Skip to content

Fix/no explicit dispose - #68

Merged
NickKhalow merged 5 commits into
mainfrom
fix/no_explicit_dispose
May 15, 2026
Merged

Fix/no explicit dispose#68
NickKhalow merged 5 commits into
mainfrom
fix/no_explicit_dispose

Conversation

@NickKhalow

@NickKhalow NickKhalow commented May 14, 2026

Copy link
Copy Markdown

What does this PR change?

Prevents LiveKit FFI callback threads from blocking Unity's application exit by marking them as background threads, and standardizes internal logging through Utils.Debug with UTC timestamps.

Problem

When Unity's Application.Quit() is called, the runtime waits for all foreground threads to complete before the process can exit. The LiveKit native SDK spawns threads for FFI callbacks (FFICallback), and these threads remain as foreground threads by default. If a callback is in-flight or the native SDK keeps dispatching events during shutdown, the application hangs — contributing to the exit delay investigated in unity-explorer#8770.

Additionally, some log calls in FFIClient used Debug.Log directly instead of the SDK's own Utils.Debug wrapper, bypassing the LK_DEBUG conditional compilation and lacking consistent formatting.

Solution

Background thread marking (FFIClient.FFICallback):

  • At the entry point of FFICallback, checks Thread.CurrentThread.IsBackground and sets it to true if it isn't already.
  • This ensures that any thread the native LiveKit SDK uses to invoke the callback will not prevent the CLR/Mono runtime from exiting when Application.Quit() is called.
  • The check is idempotent — setting IsBackground on an already-background thread is a no-op.

Logging standardization:

  • Replaced three Debug.Log calls in FFIClient.cs with Utils.Debug to route them through the SDK's conditional (LK_DEBUG) logging path.
  • Added the captureLogs flag value to the initialization log for easier diagnostics.
  • Added UTC timestamps (HH:mm:ss.fff) to both Utils.Debug and Utils.Error output for better correlation with exit timing diagnostics.

Note

An initial approach (silent drop fix) was attempted and reverted in favor of the current, simpler background-thread solution.

DO NOT MERGE YET — pending validation with unity-explorer#8770.

Test Instructions

  1. Integrate this branch into a unity-explorer build (already referenced in unity-explorer#8770 via manifest.json).
  2. Launch the Explorer, enter a world, then quit the application.
  3. Verify the application exits promptly without hanging.
  4. Check logs for LiveKit [LK] prefixed messages — they should include UTC timestamps.
  5. Verify no regressions in LiveKit comms (voice/data channels) during normal gameplay.

Quality Checklist

  • Changes have been tested locally
  • No regressions in LiveKit connectivity
  • Exit timing validated with unity-explorer#8770

@NickKhalow NickKhalow self-assigned this May 14, 2026
@github-actions

Copy link
Copy Markdown

🔗 Merge Alignment Reminder

If this PR targets main, please make sure the corresponding changes in unity-explorer are also ready to merge.

Both repos should be merged in coordination to avoid breaking changes. Do not merge one without the other being ready.

@NickKhalow
NickKhalow merged commit dc6e479 into main May 15, 2026
@charly-bg
charly-bg deleted the fix/no_explicit_dispose branch June 3, 2026 14:34
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