fix: NetworkTime.start() runs twice for clients#326
Merged
elementbound merged 4 commits intofoxssake:mainfrom Nov 19, 2024
Merged
fix: NetworkTime.start() runs twice for clients#326elementbound merged 4 commits intofoxssake:mainfrom
elementbound merged 4 commits intofoxssake:mainfrom
Conversation
…ved by adding another boolean flag to prevent the race condition. I made a new boolean flag instead of using _active because _active is used in multiple conditions with tick loops, and so would need MUCH testing to confirm no regressions are introduced. As a result, to avoid regressions, I just made a new boolean variable to be safe.
Contributor
|
Really nice catch! Since NetworkTime can now have more than two states ( inactive, activating, active ), I've refactored this to use an enum-like solution. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Supersedes #310 and explained the bug there.
Basically,
if _active: returndoes not work for clients because of the await, hence introducing a race condition.I could have just made
_active = trueabove the await, but this will likely introduce regression bugs, becauseif _activeis checked in many places in the code to process tick loops. So if I pushed_active = truebefore the await/sync finishes, it all these these codeblocks would enter tick processing while still awaiting for timesync. Red flags so, let's play safe with just a boolean flag.As a result, I just made a boolean variable to ensure
NetworkTime.start()doesn't get in the codeblock twice. I could have put it inside the server await codeblock instead of applying it to everyone, but this improves readability.The other alternative I see for not introducing this variable, is making the
_tickvariables by default -1 instead of 0, and so once it entersNetworkTime.start(), it checksif _active or _tick > -1instead ofif _active or _is_activating. But anyway, this new variable adds readability imo.Minor improvements which could be done in this PR is replacing
if _active or _is_activatingexit condition with exclusively_is_activating, or toggling_is_activating = falseright after the await loop, since it is no longer activating but activated :PAnyway, this PR is as simple as can be and works, all the possible variants are mentioned above, choose any you wish.