Stop player save snapshots sharing mutable state with the player - #1253
Open
HarleyGilpin wants to merge 1 commit into
Open
Stop player save snapshots sharing mutable state with the player#1253HarleyGilpin wants to merge 1 commit into
HarleyGilpin wants to merge 1 commit into
Conversation
Player.copy() defensively copies every field except friends, which was passed by reference, and offers, where copyOf() copies the array but not the ExchangeOffer elements behind it. The snapshot is serialized on Dispatchers.IO while the game thread can still write to both. FriendsList and ClanChat assign into player.friends, and an offer's state, completed and coins are var, so a save can observe a half-updated snapshot or throw ConcurrentModificationException - after Config.fileWriter has already truncated the target file. friends is copied with toMap() and offers elementwise. ExchangeHistory is entirely val, so history.toList() is already safe.
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.
Partially addresses #1252. The atomic write half of that issue is untouched, so it stays open.
Player.copy()builds the snapshot that gets serialized onDispatchers.IO. Every field is defensively copied except two:friendsisval friends: MutableMap<String, ClanRank> = mutableMapOf()(Player.kt:44), handed straight to the IO thread by reference. The game thread keeps writing to it:FriendsList.kt:67andClanChat.kt:106both doplayer.friends[account.accountName] = .... Adding a friend during a save can throwConcurrentModificationExceptionwhile the file is being written.offers.copyOf()copies the array but not theExchangeOfferelements, andstate,completedandcoinsarevar, so a GE update mid-save tears the snapshot.Neither fails loudly. The
friendscase throws on the IO thread afterConfig.fileWriterhas truncated the target, which is the part that turns it into a corrupt save rather than a retried one. That interaction is why #1252 covers both.ExchangeOfferis a data class whose remaining fields areval, so an elementwisecopy()fully detaches it.ExchangeHistoryis entirelyval, sohistory.toList()was already safe and is left as is.Both tests fail without the change:
Full suite green.