Fix Heartbeat gateway event handling #3105
Open
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.
Currently, serenity deserializes the Heartbeat gateway event (opcode 1) incorrectly (Discord->Bot heartbeats):
It attempts to deserialize a sequence number for it, however it is
null
in the payload and that matches Discord's documentation on gateway events: https://discord.com/developers/docs/events/gateway-events#payload-structure(Heartbeat is not opcode 0, so
s
andt
arenull
.)What serenity intends to happen after is forcing a heartbeat immediately. This is correct according to the documentation: https://discord.com/developers/docs/events/gateway#sending-heartbeats
However, the deserialization error leads to this code being unreachable in practice.
This patch leaves the Heartbeat event's sequence field in, as it's technically part of the crate's public API, but sets it to 0 and ignores it during handling.
Some cursory testing indicates that this leads to the correct behavior:
The current running theory is that this was never noticed because this gateway event very rarely happens.
In my testing, Discord only sends this event when a heartbeat is overdue. So to test this, I disabled periodic heartbeats, except once after the Hello event.