fix player disconnect#3104
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes use-after-free / stale-pointer issues that occurred when a networked player disconnects while a duel mode still holds raw DuelPlayer* pointers into the users map entry that's about to be erased. A new virtual hook lets each duel mode null out its references before the bufferevent is freed, and server-side send helpers now skip writes to the bufferevent currently being torn down.
Changes:
- Add pure virtual
DuelMode::OnPlayerDisconnected()and implement it inSingleDuelandTagDuelto clearhost_player,players[],pplayer[],ready[], and (tag)surrender[]/cur_player[], plus erase fromobservers. - Track the currently-disconnecting
bufferevent*inNetServerand gate all send helpers behindCanWriteToPlayer()to prevent writing into a buffer that's about to be freed (while still preservinglast_sentsoReSendToPlayerworks for remaining players). - Invoke
OnPlayerDisconnected()fromDisconnectPlayer()beforebufferevent_free(), and resetdp->game/state/type/bevso any subsequent accidental access is benign.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| gframe/network.h | Adds pure virtual OnPlayerDisconnected(DuelPlayer*) to DuelMode. |
| gframe/single_duel.h | Declares OnPlayerDisconnected override. |
| gframe/single_duel.cpp | Implements cleanup of host_player, players[], ready[], pplayer[], and observers. |
| gframe/tag_duel.h | Declares OnPlayerDisconnected override. |
| gframe/tag_duel.cpp | Implements cleanup of host_player, players[], ready[], surrender[], pplayer[], cur_player[], and observers. |
| gframe/netserver.h | Adds disconnecting_bev and CanWriteToPlayer(); routes all send helpers through it. |
| gframe/netserver.cpp | Defines disconnecting_bev; sets it around LeaveGame/DisconnectPlayer in the EOF/error path; calls OnPlayerDisconnected and resets DuelPlayer fields before freeing the bufferevent. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
This change prevents the server from writing to a bufferevent that is already being disconnected, and clears stale duel-side player references when a player is removed.
It also marks single duel disconnect endings as
DUEL_STAGE_ENDso remaining clients closing afterward do not trigger a second duel-end notification for observers.