-
Notifications
You must be signed in to change notification settings - Fork 17
fix: delay on exit and exit stopwatch #8770
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
b9ea037
stopwatch
NickKhalow b590652
implement safety test
NickKhalow 81988cf
OnQuittingCleanUpCandidate and stopwatch
NickKhalow 5f65bdc
use new api
NickKhalow 7426274
Application.quitting handling
NickKhalow be99105
exclude DCLPrefs from test due a cycling issue of asmdefs
NickKhalow 68e933e
Apply suggestions from code review
NickKhalow 09e5482
time format
NickKhalow 02c8b05
Merge remote-tracking branch 'origin/fix/delay-on-exit' into fix/dela…
NickKhalow ffdd1c0
ensure reset isExiting in Editor
NickKhalow d6472b3
clear candidates on quit
NickKhalow 426d6be
patch application.quitting
NickKhalow 88374ee
fix pathing
NickKhalow e0099ce
log current handlers count
NickKhalow b3b57a6
link the fix branch
NickKhalow b7b48bb
report timestamp
NickKhalow 330169b
update hash
NickKhalow 4535e94
fix typo
NickKhalow c4cde1a
clear in editor
NickKhalow f83ede8
reference updated branch
NickKhalow c7384d8
address tests
NickKhalow 937e12e
exclude log check for DCLPlayerPrefs
NickKhalow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 110 additions & 3 deletions
113
Explorer/Assets/DCL/Infrastructure/Utility/ExitUtils.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,130 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using DCL.Diagnostics; | ||
| using UnityEngine; | ||
| using Utility.Multithreading; | ||
| #if UNITY_EDITOR | ||
| using UnityEditor; | ||
| #endif | ||
|
|
||
| namespace DCL.Utility | ||
| { | ||
| public class OnQuittingCleanUpCandidate | ||
| { | ||
| private readonly string name; | ||
| private readonly Action callback; | ||
|
|
||
| public string Name => name; | ||
|
|
||
| public OnQuittingCleanUpCandidate(string name, Action callback) | ||
| { | ||
| this.name = name; | ||
| this.callback = callback; | ||
| } | ||
|
|
||
| internal void Execute(Stopwatch stopwatch) | ||
| { | ||
| long startedAtMs = stopwatch.ElapsedMilliseconds; | ||
|
|
||
| try | ||
| { | ||
| callback.Invoke(); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| ReportHub.LogException(e, ReportCategory.UNSPECIFIED); | ||
| } | ||
|
|
||
| long elapsedMs = stopwatch.ElapsedMilliseconds - startedAtMs; | ||
| ReportHub.LogProductionInfo($"[ExitUtils] '{name}' cleanup took {elapsedMs}ms (total {stopwatch.ElapsedMilliseconds}ms)"); | ||
| } | ||
| } | ||
|
|
||
| public static class ExitUtils | ||
| { | ||
| public static event Action BeforeApplicationQuitting; | ||
| private static readonly Mutex<List<OnQuittingCleanUpCandidate>> candidates = new (new ()); | ||
| private static readonly Atomic<bool> isExiting = new (false); | ||
|
|
||
| [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] | ||
| private static void SubscribeToApplicationQuitting() | ||
| { | ||
| Application.quitting += OnApplicationQuitting; | ||
|
NickKhalow marked this conversation as resolved.
|
||
| } | ||
|
NickKhalow marked this conversation as resolved.
|
||
|
|
||
| private static void OnApplicationQuitting() | ||
| { | ||
| Application.quitting -= OnApplicationQuitting; | ||
| ReportHub.LogProductionInfo("[ExitUtils] triggered by Unity's Application.quitting"); | ||
| Exit(); | ||
| } | ||
|
|
||
| public static void RegisterCleanUpCandidate(OnQuittingCleanUpCandidate candidate) | ||
| { | ||
| if (isExiting) | ||
| { | ||
| ReportHub.LogProductionInfo($"[ExitUtils] Ignored RegisterCleanUpCandidate('{candidate.Name}') because Exit() is already in progress"); | ||
| return; | ||
| } | ||
|
|
||
| using var scope = candidates.Lock(); | ||
|
|
||
| for (var i = 0; i < scope.Value.Count; i++) | ||
| { | ||
| if (scope.Value[i].Name == candidate.Name) | ||
| throw new InvalidOperationException($"[ExitUtils] Cleanup candidate '{candidate.Name}' is already registered"); | ||
| } | ||
|
|
||
| scope.Value.Add(candidate); | ||
| } | ||
|
|
||
| public static void UnregisterCleanUpCandidate(string name) | ||
| { | ||
| if (isExiting) | ||
| { | ||
| ReportHub.LogProductionInfo($"[ExitUtils] Ignored UnregisterCleanUpCandidate('{name}') because Exit() is already in progress"); | ||
| return; | ||
| } | ||
|
|
||
| using var scope = candidates.Lock(); | ||
|
|
||
| for (var i = 0; i < scope.Value.Count; i++) | ||
| { | ||
| if (scope.Value[i].Name == name) | ||
| { | ||
| scope.Value.RemoveAt(i); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static void Exit() | ||
| { | ||
| BeforeApplicationQuitting?.Invoke(); | ||
| Stopwatch stopwatch = Stopwatch.StartNew(); | ||
| ReportHub.LogProductionInfo($"[ExitUtils] Exit requested at {stopwatch.ElapsedMilliseconds}ms"); | ||
|
NickKhalow marked this conversation as resolved.
Outdated
NickKhalow marked this conversation as resolved.
Outdated
NickKhalow marked this conversation as resolved.
Outdated
NickKhalow marked this conversation as resolved.
Outdated
|
||
|
|
||
| if (isExiting) | ||
| { | ||
| ReportHub.LogProductionInfo("[ExitUtils] Exit() ignored because it is already in progress"); | ||
| return; | ||
| } | ||
|
|
||
| isExiting.Set(true); | ||
|
|
||
| using (var scope = candidates.Lock()) | ||
| { | ||
| foreach (OnQuittingCleanUpCandidate candidate in scope.Value) | ||
| candidate.Execute(stopwatch); | ||
| } | ||
|
|
||
| ReportHub.LogProductionInfo($"[ExitUtils] CleanUpCandidates finished at {stopwatch.ElapsedMilliseconds}ms"); | ||
|
|
||
| #if UNITY_EDITOR | ||
| EditorApplication.isPlaying = false; | ||
| #else | ||
| UnityEngine.Application.Quit(); | ||
| #endif | ||
| ReportHub.LogProductionInfo($"[ExitUtils] Quit call dispatched at {stopwatch.ElapsedMilliseconds}ms"); | ||
| } | ||
|
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.