Skip to content

Commit

Permalink
Only attempt resume if the gateway says we can
Browse files Browse the repository at this point in the history
Per [1] and [2], if the `d` field is set to `true` we may attempt to
resume again with the same session, otherwise we should not. nostrum
would previously loop on trying to resume on the same session after an
invalid session was sent, and the behaviour of simply sending an
identify payload did not work as expected. When the session that we try
to resume with is invalid, perform a reconnect and do the whole
handshake, greeting and meeting the parents thing all over again.

[1]: https://discord.com/developers/docs/topics/gateway-events#invalid-session
[2]: https://discord.com/developers/docs/topics/gateway#resuming
  • Loading branch information
jchristgit committed Apr 29, 2024
1 parent c34a563 commit 035839f
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/nostrum/shard/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,14 @@ defmodule Nostrum.Shard.Event do
end
end

def handle(:invalid_session, _payload, state) do
Logger.info("INVALID_SESSION")
{{state, Payload.identity_payload(state)}, []}
def handle(:invalid_session, %{d: can_resume? = true}, state) do

Check warning on line 59 in lib/nostrum/shard/event.ex

View workflow job for this annotation

GitHub Actions / Run Unit tests (1.14, 25)

variable "can_resume?" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 59 in lib/nostrum/shard/event.ex

View workflow job for this annotation

GitHub Actions / Run Unit tests (1.14, 26)

variable "can_resume?" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 59 in lib/nostrum/shard/event.ex

View workflow job for this annotation

GitHub Actions / Run Unit tests (1.15, 25)

variable "can_resume?" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 59 in lib/nostrum/shard/event.ex

View workflow job for this annotation

GitHub Actions / Run Unit tests (1.15, 26)

variable "can_resume?" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 59 in lib/nostrum/shard/event.ex

View workflow job for this annotation

GitHub Actions / Run Unit tests (1.15, 27.0-rc3)

variable "can_resume?" is unused (if the variable is not meant to be used, prefix it with an underscore)
Logger.info("Invalid but resumable session. Attempting to resume at #{state.session}")
{{state, Payload.resume_payload(state)}, []}
end

def handle(:invalid_session, %{d: can_resume? = false}, state) do

Check warning on line 64 in lib/nostrum/shard/event.ex

View workflow job for this annotation

GitHub Actions / Run Unit tests (1.14, 25)

variable "can_resume?" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 64 in lib/nostrum/shard/event.ex

View workflow job for this annotation

GitHub Actions / Run Unit tests (1.14, 26)

variable "can_resume?" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 64 in lib/nostrum/shard/event.ex

View workflow job for this annotation

GitHub Actions / Run Unit tests (1.15, 25)

variable "can_resume?" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 64 in lib/nostrum/shard/event.ex

View workflow job for this annotation

GitHub Actions / Run Unit tests (1.15, 26)

variable "can_resume?" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 64 in lib/nostrum/shard/event.ex

View workflow job for this annotation

GitHub Actions / Run Unit tests (1.15, 27.0-rc3)

variable "can_resume?" is unused (if the variable is not meant to be used, prefix it with an underscore)
Logger.info("Invalid and un-resumable session at #{state.session}. Events may be lost.")
{{state, :reconnect}, []}
end

def handle(:reconnect, _payload, state) do
Expand Down

0 comments on commit 035839f

Please # to comment.