fix: allow session recovery even if no event was received - #5541
Open
marwan562 wants to merge 1 commit into
Open
fix: allow session recovery even if no event was received#5541marwan562 wants to merge 1 commit into
marwan562 wants to merge 1 commit into
Conversation
Before that fix, the session could only be recovered if the client provided the offset of the last event it had received. But when no event was received before the disconnection, the offset was undefined, which prevented the restoration of the session (id, rooms and data). The offset is now optional in Adapter#restoreSession: when it is not provided, the session is restored without replaying any event. Fixes socketio#5538
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.
The kind of change this PR does introduce
Current behavior
When
connectionStateRecoveryis enabled, a client that connects, joins rooms, and disconnects before the server emits any events cannot recover its session on reconnection (socket.recoveredisfalse, a newsocket.idis generated, room memberships andsocket.dataare lost).This happens because
_lastOffsetis only set on the client when an EVENT packet is received. In the "zero-event" case, the reconnect CONNECT packet contains no offset at all ({"pid":"..."}), and the server-side check innamespace.ts:skips
restoreSession()entirely.New behavior
The offset is now optional in
Adapter#restoreSession():namespace.ts, a non-string offset is normalized toundefined, and recovery is attempted as long as a valid private session id (pid) is providedSessionAwareAdapter#restoreSession(), an undefined offset means the client did not receive any event yet, so the session (id, rooms, data) is restored without replaying any eventNote for external adapters (Redis adapter, Postgres adapter, cluster adapters...):
restoreSession()may now be called withoffset === undefined. Implementations that don't handle it can keep their current signature; returningnullpreserves the previous behavior.Other information (e.g. related issues)
Fixes #5538
Testing:
test/connection-state-recovery.ts): zero-event recovery (id/rooms/data restored, no replayed events) and recovery with a non-string offsetpackages/socket.io-adapter/test/index.ts: restore with an undefined offset returns the session with an emptymissedPacketsarraypackages/socket.io-client/test/connection-state-recovery.ts: real client reconnects withrecovered === truewithout any prior eventnpm test -w packages/socket.io(217 passing), adapter suite (22 passing), client node suite (112 passing; one pre-existing flaky failure in the unrelatedautoUnrefchild-process test, which also fails on a clean checkout)