From 5d8192008201ea794800cde3f234082aecd9b489 Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Sat, 4 May 2024 21:58:10 +0100 Subject: [PATCH] Remove blob of zlib data from state machine errors 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. --- lib/nostrum/shard/session.ex | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/nostrum/shard/session.ex b/lib/nostrum/shard/session.ex index 2b8cc1e72..ce9300701 100644 --- a/lib/nostrum/shard/session.ex +++ b/lib/nostrum/shard/session.ex @@ -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