Skip to content

Fix device console join errors, logs --follow spinning, and list filter keys - #31

Merged
joshk merged 4 commits into
mainfrom
fix/device-command-bugs
Nov 24, 2026
Merged

joshk merged 4 commits into
mainfrom
fix/device-command-bugs

Conversation

@joshk

@joshk joshk commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes three bugs in the device commands. Server references are to nerves_hub_web at upstream/main (a7c5c5e44).

nh device console / nh device shell hung when the join was refused

internal/phoenix sent phx_join and returned at once. The command then printed "Connected to X", put the terminal into raw mode and waited for output that would never come.

The server refuses the join with {:error, %{reason: "unauthorized"}} when the device isn't found, the user's role lacks the permission, or the org's 2FA requirement isn't met:

  • lib/nerves_hub_web/channels/user_console_channel.ex:15-26, with the checks in authorized?/2 at :136-148 (:"device:console", TwoFactor.meets_org_requirement?)
  • lib/nerves_hub_web/channels/user_local_shell_channel.ex:14-25, with authorized?/2 at :66 (:"device:extensions:local_shell")

Changes

  • New phoenix.Conn.JoinAndWait waits up to a timeout (10s, the Phoenix JS default) for the phx_reply whose ref matches the join. It returns a *JoinError with the server's reason, or ErrJoinTimeout.
  • Frames that arrive before the reply, such as output or replies to other refs, are kept. Receive returns them in order afterwards, so nothing is lost.
  • runDeviceTerminalSession dials and joins through openTerminalChannel, before it prints "Connected" or enters raw mode. An unauthorized refusal lists the three possible causes, since the server gives the same reason for all of them.
  • A websocket the server won't open now gives a clear error. NervesHubWeb.APISocket.connect/3 (lib/nerves_hub_web/channels/api_socket.ex:14-25) returns :error for any token that isn't a user API token, and Phoenix answers the upgrade with 403. phoenix.Dial returns a *DialError carrying the status. The command then says either that product API keys (nhpk_, lib/nerves_hub/products/api_key.ex:39) can't open a console or shell, or that the token is invalid, expired or revoked. The token itself is never echoed.

The output and heartbeat loops are unchanged.

nh device logs --follow hammered the server with a small --limit

since is inclusive (lib/nerves_hub/devices/log_lines.ex:41 and :111, l.timestamp >= ^since), so every poll returns the boundary line again. With --limit 1, that one line made a "full page", so the loop polled again with no sleep, forever, and never got past it. Any limit did the same once that many lines shared a timestamp.

This ports the fix nh device metrics --follow already has: polls ask for max(limit, 2) lines, and the loop only catches up without sleeping when a full page brought new lines.

nh device logs -o json with no lines printed nothing. It now prints [] when not following, as metrics does.

nh device list --filter tag:prod was silently ignored

The help example used tag, but the server's key is tags. The API controller converts filter keys with String.to_existing_atom/1 and passes them straight to DeviceFiltering.build_filters/2 (lib/nerves_hub_web/controllers/api/device_controller.ex:41, lib/nerves_hub/devices.ex:161). Unknown keys fall through to the catch-all filter/4 clause (lib/nerves_hub/devices/device_filtering.ex:362-366), so the list came back unfiltered. A key or --sort value that isn't already an atom on the server raises instead, which is a 500 (device_controller.ex:41 and :57).

nh now checks these locally, in the new internal/api/device_filters.go, and errors with the valid options (plus "did you mean tags?" for tag):

  • Filter keys: the filter/4 clauses in device_filtering.ex:284-360 that the API path can actually reach.
  • Values with a fixed set: for the multi-choice filters, every comma-separated choice is checked against value_dynamic/2 (:177-210; unknown choices become nil and are dropped). Also checked: state/reason (:355-356, @states/@reasons), display_deleted (:345-349) and metrics_operator. deployment_id must be numeric and metrics_value a plain decimal number (not NaN, Inf or hex).
  • has_no_tags: only true is accepted. The API passes the raw string and :333 treats any value as truthy.
  • Metric filter: filter_on_metric/2 (:403) only applies when metrics_key, metrics_operator and metrics_value are all present. nh requires the key and value together and fills in metrics_operator:gt, the server default, when it's missing.
  • Sort columns: @sortable_fields (:102): identifier, connection_established_at, connection_last_seen_at, tags.
  • tags: NervesHub.Types.Tag.cast/1 (lib/nerves_hub/types.ex:11-24) refuses tags with a space, and build_tag_filter then filters nothing. nh rejects a tag containing a space, and a value with no tags left after splitting.
  • Comma-only values: a multi-choice value made only of commas (connection:,) splits to no choices, so the server applies no filter. nh rejects it.
  • Repeated keys: repeating a --filter key used to keep only the last value without saying so. It's now an error that points to the comma-separated form.

The help text now lists every key and its values, and the example uses tags:prod. The tests that locked in tag:prod (cmd/device_list_test.go) now use tags, and the TestParseSort case that used last_communication (not a sortable column) uses connection_last_seen_at.

Decisions and limits

  • Left out healthy, is_pinned and only_updating. They're in @default_filters, but the API path never applies them: healthy and is_pinned have no clause, and only_updating only matches a boolean while the API passes strings. --filter updates:updating does what only_updating meant.
  • --sort is limited to @sortable_fields. The API would also accept any Device schema field that happens to be an atom (for example inserted_at), but the UI doesn't treat those as supported, so nh doesn't either.
  • --follow (logs and metrics alike) still can't page past more lines at one exact timestamp than fit in one page. It now waits for the interval instead of spinning.

Tests

  • internal/phoenix: covers a refused join (the reason comes back), early frames kept for Receive, a join timeout, and a 403 dial.
  • cmd/device_console_test.go: a fake Phoenix server covers console and shell refusals, the join happy path with output arriving before the reply, an unknown reason, a timeout, and a refused socket for both a user token and a product key.
  • cmd/device_logs_test.go: covers --limit 1 (poll limit 2, the new line printed, no spin), a full page of already-seen lines (no spin) and empty -o json. All three fail against the old code.
  • cmd/device_list_test.go: covers unknown keys, bad values, a metric filter missing its value, a non-numeric metric, bad sort columns (no request is sent for any of these) and the default metric operator.

go test ./..., go vet ./... and gofmt -l . are clean.

🤖 Generated with Claude Code

joshk and others added 4 commits November 24, 2026 13:15
nh device console and nh device shell sent phx_join and went straight
into raw mode, so a join the server refused (no such device, a role
without the permission, or unmet 2FA) left a silent, dead session.
Wait for the join reply, with a timeout, and report the refusal before
touching the terminal. Frames that arrive before the reply are kept for
the read loop.

A websocket the server refuses (403 for a bad token or a product API
key) now says so, too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
since is inclusive, so each poll returns the boundary line again. With
--limit 1, or a full page of lines at one timestamp, that page counted
as full and the loop polled again at once, forever. Ask for at least
two lines per poll and only catch up when the page brought new lines,
as nh device metrics --follow already does.

nh device logs -o json with no lines now prints [] rather than nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The help suggested --filter tag:prod, but the server's key is tags and
it ignores keys it doesn't know, so every device was listed. A key or
sort column that isn't an atom on the server is a 500.

Check filter keys, the values of the filters that take a fixed set,
and sort columns against NervesHub.Devices.DeviceFiltering, and name
what would work. Fill in metrics_operator, which the metric filter
needs, and correct the help.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Tags with a space, or no tags at all, fail the server's Tag.cast and
filter nothing; so does a multi-choice value made only of commas.
metrics_value must be a plain decimal rather than anything ParseFloat
accepts, and a repeated --filter key is an error instead of silently
keeping the last value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@joshk
joshk merged commit 54353e8 into main Nov 24, 2026
1 check passed
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.

1 participant