Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks to how Bandit handles unexpected messages #259

Merged
merged 2 commits into from
Nov 2, 2023
Merged

Conversation

mtrudel
Copy link
Owner

@mtrudel mtrudel commented Nov 2, 2023

Previously, unexpected messages received by an HTTP/1 handler would crash the server; this PR updates this behaviour to log messages in the same way that a stock GenServer which does not implement any handle_info/2 callbacks does, and thence does not crash.

Note there's no need to add a similar mechanism on HTTP/2 handlers, since HTTP/2 requests are backed (at least for now) by Task

Also note that there's no need to add a similar mechanism to WebSockets, since WebSock already provides a way to forward handle_info calls to the websock implementor. In fact, this PR also stops explicitly catching normal monitored process termination calls in the websocket handler for this reason.

@@ -197,4 +197,31 @@ defmodule Bandit.HTTP1.Handler do

def handle_info({:EXIT, _pid, :normal}, {socket, state}),
do: {:noreply, {socket, state}, socket.read_timeout}

def handle_info(msg, state) do
# Copied verbatim from lib/elixir/lib/gen_server.ex
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we'd just call super(msg, state) here but calling super on (most) GenServer callbacks was deprecated a while ago

case Process.info(self(), :registered_name) do
{_, []} -> self()
{_, name} -> name
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you allow the process to be registered? If not you can skip this, otherwise it looks good to me :)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, handler processes themselves are able to accept any valid gen_server option via the genserver_options flag.

@mtrudel mtrudel merged commit 34caf6b into main Nov 2, 2023
27 checks passed
@mtrudel mtrudel deleted the handle_info_tweaks branch November 2, 2023 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants