Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up native resources on Unity application exit #2467

Merged
merged 6 commits into from
Jun 29, 2021

Conversation

nirinchev
Copy link
Member

@nirinchev nirinchev commented Jun 23, 2021

Description

When Unity closes the application, it will suspend garbage collection and wait for all native threads to exit (or at least stop doing work - threads that are waiting on conditional variables are not an issue). This is problematic because the managed AppHandle holds a shared_ptr to the native App, which in turn holds a shared_ptr to the SyncManger. This prevents the destruction of the SyncManager and this is where we actually stop the sync client. Not stopping the sync client means that it'll keep polling the socket in a loop, forcing Unity to wait forever.

The fix is to keep a collection of weak references to each AppHandle we create and force close all of them when the application exits.

TODO

  • Changelog entry

@nirinchev nirinchev self-assigned this Jun 24, 2021
@nirinchev nirinchev requested review from LaPeste and papafe June 24, 2021 18:54
@nirinchev nirinchev marked this pull request as ready for review June 24, 2021 19:08
Copy link
Contributor

@papafe papafe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

{
foreach (var weakHandle in _appHandles)
{
if (weakHandle.Target is AppHandle handle)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we only being overly cautious here with the is operator and then then the null conditional operator? It seems it can't be anything else than AppHandle

Copy link
Contributor

@LaPeste LaPeste left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beside the part that Ferdinando pointed out, the solution looks good. Most importantly, good stuff in discovering something that looks tedious to figure out.

Base automatically changed from ni/unity-package-cleanup to master June 25, 2021 21:22
@nirinchev nirinchev force-pushed the ni/close-apphandles branch from 541e285 to e2ba2f0 Compare June 25, 2021 21:25
@nirinchev nirinchev force-pushed the ni/close-apphandles branch from 45050cd to 518fbcd Compare June 29, 2021 22:00
@nirinchev nirinchev merged commit e2b7c27 into master Jun 29, 2021
@nirinchev nirinchev deleted the ni/close-apphandles branch June 29, 2021 23:05
nirinchev added a commit that referenced this pull request Jul 6, 2021
* master:
  Update the AppConfiguration.Logger obsolete message (#2495)
  Add more serializer types to the preserved ones (#2489)
  Update changelog from Core (#2488)
  Build an xcframework for iOS (#2475)
  Prepare for vNext (#2487)
  Prepare for 10.2.1 (#2484)
  Fix TableKey / ObjKey conversion on Android x86 (#2477)
  Update the build manager locations (#2474)
  Clean up native resources on Unity application exit (#2467)
  Fix some native warnings (#2483)
  Don't try to get the app from a removed user (#2480)
  Use delete_files from core when deleting a Realm. (#2401)
  Fixes for the sync datatype tests (#2461)
  Clean up the package.json and add some docs (#2452)
  Run iOS tests on CI (#2405)
  Verify that objects do not belong to different realm when added to collection (#2465)
  Updated README instructions (#2459)
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants