Repository navigation
Fix device console join errors, logs --follow spinning, and list filter keys - #31
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes three bugs in the device commands. Server references are to
nerves_hub_webatupstream/main(a7c5c5e44).nh device console/nh device shellhung when the join was refusedinternal/phoenixsentphx_joinand 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 inauthorized?/2at:136-148(:"device:console",TwoFactor.meets_org_requirement?)lib/nerves_hub_web/channels/user_local_shell_channel.ex:14-25, withauthorized?/2at:66(:"device:extensions:local_shell")Changes
phoenix.Conn.JoinAndWaitwaits up to a timeout (10s, the Phoenix JS default) for thephx_replywhose ref matches the join. It returns a*JoinErrorwith the server's reason, orErrJoinTimeout.Receivereturns them in order afterwards, so nothing is lost.runDeviceTerminalSessiondials and joins throughopenTerminalChannel, before it prints "Connected" or enters raw mode. Anunauthorizedrefusal lists the three possible causes, since the server gives the same reason for all of them.NervesHubWeb.APISocket.connect/3(lib/nerves_hub_web/channels/api_socket.ex:14-25) returns:errorfor any token that isn't a user API token, and Phoenix answers the upgrade with 403.phoenix.Dialreturns a*DialErrorcarrying 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 --followhammered the server with a small--limitsinceis inclusive (lib/nerves_hub/devices/log_lines.ex:41and: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 --followalready has: polls ask formax(limit, 2)lines, and the loop only catches up without sleeping when a full page brought new lines.nh device logs -o jsonwith no lines printed nothing. It now prints[]when not following, as metrics does.nh device list --filter tag:prodwas silently ignoredThe help example used
tag, but the server's key istags. The API controller converts filter keys withString.to_existing_atom/1and passes them straight toDeviceFiltering.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-allfilter/4clause (lib/nerves_hub/devices/device_filtering.ex:362-366), so the list came back unfiltered. A key or--sortvalue that isn't already an atom on the server raises instead, which is a 500 (device_controller.ex:41and:57).nh now checks these locally, in the new
internal/api/device_filters.go, and errors with the valid options (plus "did you meantags?" fortag):filter/4clauses indevice_filtering.ex:284-360that the API path can actually reach.value_dynamic/2(:177-210; unknown choices becomeniland are dropped). Also checked:state/reason(:355-356,@states/@reasons),display_deleted(:345-349) andmetrics_operator.deployment_idmust be numeric andmetrics_valuea plain decimal number (notNaN,Infor hex).has_no_tags: onlytrueis accepted. The API passes the raw string and:333treats any value as truthy.filter_on_metric/2(:403) only applies whenmetrics_key,metrics_operatorandmetrics_valueare all present. nh requires the key and value together and fills inmetrics_operator:gt, the server default, when it's missing.@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, andbuild_tag_filterthen filters nothing. nh rejects a tag containing a space, and a value with no tags left after splitting.connection:,) splits to no choices, so the server applies no filter. nh rejects it.--filterkey 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 intag:prod(cmd/device_list_test.go) now usetags, and theTestParseSortcase that usedlast_communication(not a sortable column) usesconnection_last_seen_at.Decisions and limits
healthy,is_pinnedandonly_updating. They're in@default_filters, but the API path never applies them:healthyandis_pinnedhave no clause, andonly_updatingonly matches a boolean while the API passes strings.--filter updates:updatingdoes whatonly_updatingmeant.--sortis limited to@sortable_fields. The API would also accept anyDeviceschema field that happens to be an atom (for exampleinserted_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 forReceive, 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 ./...andgofmt -l .are clean.🤖 Generated with Claude Code