Skip to content

Commit

Permalink
Remove blob of zlib data from state machine errors
Browse files Browse the repository at this point in the history
Due to how zlib and the state machine work, when the state machine
crashed with an error, it would log the last blob that was received.

This blob could not be decoded from error traces because it depends on
the context that it was received by, additionally we cannot decode it as
an error logging step as once it has been decoded by a context it cannot
be inflated again.

Removing this blob is the best solution here as it shrinks the size of
state machine errors and makes them easier to read by users and Nostrum
team.

In state machine traces, the payload will now be shown as "PAYLOAD
REMOVED", if the last event was not a payload, the event will remain
unmodified.
  • Loading branch information
jb3 committed May 4, 2024
1 parent dfe11f1 commit 5d81920
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/nostrum/shard/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,27 @@ defmodule Nostrum.Shard.Session do
{:next_state, :disconnected, %{data | conn: nil, stream: nil}, connect}
end

# Internal helper that removes the huge zlib data blob from any errors
# experienced by the shard session state machine. This zlib blob is only
# decodable by the zlib context that the error ocurred in anyway so it's
# inclusion in bug reports only hinders. It has also already been decoded
# by the time it reaches this call so we cannot attempt to decode it here.
def format_status(%{queue: queue} = state) do
queue =
Enum.map(queue, fn queue_item ->
case queue_item do
# match gun websocket messages and remove the data blob
{:info, {:gun_ws, conn, stream, {:binary, _payload}}} ->
{:info, {:gun_ws, conn, stream, {:binary, "PAYLOAD REMOVED"}}}

_ ->
queue_item
end
end)

%{state | queue: queue}
end

# Internal helper. Wait for consumers to start up.
defp wait_for_consumer_boot(reference, timeout) do
receive do
Expand Down

0 comments on commit 5d81920

Please # to comment.