Fix 6: _fetch_state race — _syncing guard and session-id validation
- Add _syncing: bool flag; _fetch_state() returns immediately if a game.get_state request is already in-flight — prevents multiple concurrent full-sync requests on poor connections or reconnect bursts - _syncing cleared in both success and error callbacks, and in _clear_state() so it resets cleanly when the session ends - _apply_state_from_payload: reject responses whose sessionId doesn't match the current session_id, discarding stale full-state responses that were in-flight when the player left or started a new game Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -102,6 +102,7 @@ var opponent_name : String = ""
|
||||
var is_spectating : bool = false
|
||||
var debug_output : bool = false
|
||||
var _state_version : int = -1
|
||||
var _syncing : bool = false # true while a game.get_state request is in-flight
|
||||
|
||||
# ── Lifecycle ─────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -117,6 +118,7 @@ func _clear_state() -> void:
|
||||
opponent_name = ""
|
||||
is_spectating = false
|
||||
_state_version = -1
|
||||
_syncing = false
|
||||
|
||||
|
||||
## Called on every auth_success (initial login AND reconnects).
|
||||
@@ -312,10 +314,16 @@ func send_ready() -> void:
|
||||
|
||||
|
||||
func _fetch_state() -> void:
|
||||
if _syncing:
|
||||
return
|
||||
_syncing = true
|
||||
Network.request("game.get_state",
|
||||
{ "sessionId": session_id },
|
||||
func(_t: String, p: Variant) -> void: _apply_state_from_payload(p),
|
||||
func(_c: String, _m: String) -> void: pass)
|
||||
func(_t: String, p: Variant) -> void:
|
||||
_syncing = false
|
||||
_apply_state_from_payload(p),
|
||||
func(_c: String, _m: String) -> void:
|
||||
_syncing = false)
|
||||
|
||||
|
||||
func send_draw() -> void:
|
||||
@@ -482,6 +490,13 @@ func _apply_state_from_payload(p: Variant) -> void:
|
||||
if debug_output:
|
||||
print("[Game] _apply_state_from_payload: no 'state' key in payload ", JSON.stringify(p))
|
||||
return
|
||||
# Discard stale responses that belong to a session we have already left.
|
||||
var response_sid: String = str(s.get("sessionId", ""))
|
||||
if not session_id.is_empty() and not response_sid.is_empty() \
|
||||
and response_sid != session_id:
|
||||
if debug_output:
|
||||
print("[Game] _apply_state_from_payload: ignored stale response for session ", response_sid)
|
||||
return
|
||||
_state_version = int(s.get("stateVersion", -1))
|
||||
if debug_output:
|
||||
print("[Game] state applied phase=", s.get("phase","?"),
|
||||
|
||||
Reference in New Issue
Block a user