Skip to content

Commit

Permalink
Ignore unhandled messages in DeviceLink
Browse files Browse the repository at this point in the history
Crashing DeviceLink could cause cascading problems. To fix that, this
change ignores unhandled messages in `handle_info` and logs it
  • Loading branch information
jjcarstens committed Dec 22, 2023
1 parent b315201 commit 9d96a81
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/nerves_hub/devices/device_link.ex
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,14 @@ defmodule NervesHub.Devices.DeviceLink do
{:stop, :normal, state}
end

def handle_info(msg, state) do
# Ignore unhandled messages so that it doesn't crash the link process
# preventing cascading problems. There is probably a better way to
# report this but just log the warning for now
Logger.warning("[DeviceLink] Unhandled message! - #{inspect(msg)}")
{:noreply, state}
end

defp subscribe(topic) do
Phoenix.PubSub.subscribe(NervesHub.PubSub, topic)
end
Expand Down
13 changes: 13 additions & 0 deletions test/nerves_hub/devices/device_link_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,19 @@ defmodule NervesHub.DeviceLinkTest do
assert {:stop, :normal, %{}} = DeviceLink.handle_info(:timeout_reconnect, %{})
end

test "ignores unknown messages" do
log =
ExUnit.CaptureLog.capture_log(fn ->
# Prob good spot for prop test
DeviceLink.handle_info(12355, %{})
DeviceLink.handle_info("12355", %{})
DeviceLink.handle_info(:wat, %{})
DeviceLink.handle_info(nil, %{})
end)

assert log =~ ~r/Unhandled message!/
end

defp create_device(context) do
user = context[:user] || Fixtures.user_fixture()
org = context[:org] || Fixtures.org_fixture(user)
Expand Down

0 comments on commit 9d96a81

Please sign in to comment.