Soundcheck.Check.Warning.BlockingCallInHandler (soundcheck v0.1.0)

Copy Markdown View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

This check has a base priority of normal and works with any version of Elixir.

Explanation

Flags calls that block a LiveView process inside handle_event/3, handle_info/2, and handle_params/3: sleeps, Task.await/2, HTTP requests, and receive without after. A LiveView handles one message at a time, so while the handler waits, the page ignores every click, keystroke, and broadcast.

Before:

def handle_event("refresh", _params, socket) do
  prices = Req.get!("https://api.example.com/prices").body
  {:noreply, assign(socket, :prices, prices)}
end

After:

def handle_event("refresh", _params, socket) do
  {:noreply,
   start_async(socket, :prices, fn -> Req.get!("https://api.example.com/prices").body end)}
end

Calls inside closures (start_async/3, Task.async/1) do not count. blocking_functions lists {Module, :function} pairs and blocking_modules lists modules (atoms or regexes on the alias-expanded name) whose every call blocks.

Confidence: medium. Tier: syntactic (works without compiling).

Check-Specific Parameters

Use the following parameters to configure this check:

:excluded_paths

Paths to skip (atoms, binaries as path segments, or regexes).

This parameter defaults to [~r/\/test\//, "test"].

:strict

Unused.

This parameter defaults to false.

:blocking_functions

{Module, :function} pairs that block.

This parameter defaults to [{Process, :sleep}, {:timer, :sleep}, {Task, :await}, {Task, :await_many}, {Task, :yield}, {Task, :yield_many}].

:blocking_modules

Modules whose calls block: atoms or regexes.

This parameter defaults to [~r/^(Req|Finch|HTTPoison|Tesla)$/].

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.