Repository navigation
Clean up after Ctrl-C, keep --deploy JSON clean, and match the branded update-tool error - #34
Merged
Merged
Conversation
nh had no process-wide signal handling, so SIGINT or SIGTERM ended it at once and no deferred cleanup ran. Interrupting `nh firmware upload` or `nh archive upload` while fwup was signing left the decrypted Ed25519 key (nh-key-*.priv) and the signed copy (nh-signed-*) in $TMPDIR. Execute now runs the command under internal/interrupt: the first signal cancels the command's context, which kills fwup, espsecure and mix eval and lets the defers run. Work that never sees the context, such as a prompt blocked on the terminal, is given up on after a second, or at once on a second Ctrl-C: the registered temporary files are removed, the terminal state is restored (term.ReadPassword leaves echo off), and nh exits with 130 or 143. A command that turns Ctrl-C into a clean finish, as `device logs --follow` does, still exits 0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
deployFirmware printed "Updated deployment group ..." to stdout ahead of the JSON, and a failed deployment returned before any JSON was written, so a script lost the UUID of firmware that had uploaded. In JSON mode the progress lines now go to stderr, and the JSON for the upload, with the groups that were updated, is written before the deployment error is returned. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The hint for an update tool the instance has turned off looked for "not enabled on this NervesHub instance:", but the server now says "not enabled on <friendly platform name>: <tools>", such as "not enabled on NervesCloud: rauc", so the hint never appeared. Match the "not enabled on " prefix and take the tools after the last ": ", since a configured instance name may itself contain one, and name the instance in the hint. The test fixtures now use the server's real strings, for both the default and a branded name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Archive.create_changeset does not require product, which reads as if nh's archive check were stricter than the server. It is not: Archives.create reads the file with NervesHub.Fwup.metadata/1 first, whose UpdateTool.Metadata struct enforces product, and a missing key is a 422. Say so where the required keys are listed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Don't abandon work that returned just as the grace period ran out: select picks among ready cases at random, so check done once more. - End the line after an abandoned prompt, since exiting with 130 rather than dying of SIGINT gets no newline from the shell. - Set WaitDelay on fwup and espsecure signing, so a child holding the output pipe after the kill doesn't delay removing the key file. - Word the disabled-tool hint "an administrator must enable the format on <instance> first", which reads right for NervesHub, NervesCloud and older servers' "this NervesHub instance". 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 firmware and product commands and adds process-wide signal handling. The fourth reported bug,
nh archive uploadrequiringmeta-product, turned out not to be a bug. Server references are to nerves_hub_webupstream/mainata7c5c5e44.1. Ctrl-C while signing left the decrypted key in
$TMPDIRsignFirmwarewrites the plaintext Ed25519 key tonh-key-*.privforfwup -sand removes it with adefer.Executedidn't handle signals, so SIGINT ended nh at once and the defer never ran. That left both the key and thenh-signed-*copy behind, forfirmware uploadandarchive uploadalike.The fix.
Executenow runs the command under a newinternal/interruptpackage:exec.CommandContext(cmd.Context()), so they are killed and the defers run.mix evalusedcontext.Background(), somix.SetContextnow gives it nh's context.term.ReadPasswordorReadStringis one example. nh gives up on it after 1s, or at once on a second Ctrl-C. Before exiting it removes the files registered withinterrupt.RemoveOnExit(the key file and the signed copies), and restores the terminal state saved at startup, becauseterm.ReadPasswordleaves echo off when interrupted. Before this change, Ctrl-C at the signing-key password prompt also left the terminal without echo.WaitDelay, so a child process holding the output pipe after the kill doesn't hold up the key file's removal.Known leftover: Ctrl-C during a
mix evallookup makesmix.Org()return "", so the command reports a missing org before exiting 130. Fixing the message means changingrequireOrg/requireProduct, which have about 90 callers across files other PRs own.I used
signal.NotifywithWithCancelCauserather than plainsignal.NotifyContext, so nh knows which signal arrived (for the exit code) and can tell a second signal from the first.What else this changes. Please check these:
device logs --followanddevice metrics --followkeep their ownsignal.NotifyContext(cmd.Context(), os.Interrupt). Both handlers get Ctrl-C, the loop returns nil, and nh exits 0 as before. Their own handler is now redundant but harmless, and I left those files alone because another PR owns them.device consoleanddevice iroh console, by reading the code only, not run against a device. In raw mode,term.MakeRawclearsISIG, so Ctrl-C arrives as byte 0x03 andconsoleWriteLoopdisconnects as before. No signal is raised. Two things differ:Error: … context canceledand exits 130.nh logincode entry and confirmations, read stdin without a context. Ctrl-C at a prompt now takes up to 1s to exit, or is instant on a second Ctrl-C. Makingcmd/prompt.gocontext-aware would make the first press instant too. That file is outside this PR.Tests:
TestFirmwareUploadInterruptedRemovesKeyruns the test binary asnh firmware uploadwith a fwup stub that hangs on--sign. It checks that the key is in$TMPDIR, sends a real SIGINT, and asserts exit 130 and nonh-*files left. Against the oldExecuteit fails: the exit issignal: interrupt, and bothnh-key-*.privandnh-signed-*.fware left behind.TestSignFirmwareCancelledRemovesTempFileschecks that a cancelled context kills fwup and removes both temporary files.internal/interrupthas tests for context cancellation, giving up on stuck work after the grace period, the second signal, the file registry and exit codes.internal/mixhas a test that a cancelled base context stopsmix evalwithout reporting a timeout.2.
firmware upload/publish --deploy <group> -o jsonwrote text into the JSONdeployFirmwareprintedUpdated deployment group …to stdout before the JSON. A failed deployment returned before any JSON was written, so a script lost the UUID of firmware that had uploaded.The fix. In JSON mode the progress lines go to stderr. On a deployment error, nh still writes the upload's JSON, with
deployedlisting the groups that were updated, and then returns the error, which exits 1. This is client-side only; the server behaviour doesn't change.Tests:
TestFirmwareUploadDeployJSONKeepsStdoutCleanandTestFirmwareUploadDeployFailureStillPrintsJSON. Both fail against the old code.3.
nh product update --allow-tool: the disabled-tool hint never appearedexplainUpdateToolErrorlooked fornot enabled on this NervesHub instance:. The server now says"not enabled on #{NervesHub.Branding.friendly_platform_name()}: …":lib/nerves_hub/products/product.ex:144-148produces that message.lib/nerves_hub/branding.ex:12-17returns the configured name, orNervesHubwhen none is set. NervesCloud is one such configured name.The fix. nh now matches the
not enabled onprefix and takes the tool list after the last": ". The brief said the first. The configured name is free text and could contain": ", but the tool names, joined with", ", never do. The hint also names the instance now: "an administrator must enable the format on NervesCloud first".Tests: the fixtures in
internal/api/product_update_test.goandcmd/product_update_test.gonow use the server's real strings, for bothNervesHubandNervesCloud. A new table test,TestNotEnabledTools, includes a name that contains": ".Not changed:
nh archive uploadrequiringmeta-productThe review read
Archive.create_changeset(lib/nerves_hub/archives/archive.ex:52-73), which neither casts nor requiresproduct, and concluded that nh was stricter than the server. It isn't. The API path never reaches that changeset withoutproduct:lib/nerves_hub_web/controllers/api/archive_controller.ex:58callsArchives.create/2.lib/nerves_hub/archives.ex:336reads the file withFwup.metadata/1before building the changeset.lib/nerves_hub/fwup.ex:48-53buildsstruct!(Metadata, …)and rescues to{:error, :invalid_metadata}.lib/nerves_hub/firmwares/update_tool.ex:39lists:productin that struct's@enforce_keys.lib/nerves_hub_web/controllers/api/fallback_controller.ex:97-102answers 422: "The file's metadata must include meta-product, …".So dropping
productfrom nh's check would only move the refusal to the server, after signing and uploading. I kept the check and its help text, and rewrote the comment onarchiveRequiredMetato cite this path.Checks
go test ./...passes.go vet ./...is clean, and so isGOOS=windows go vet ./....gofmt -l .lists nothing.go test -racepasses forinternal/interrupt,internal/mixand the newcmdtests.🤖 Generated with Claude Code