Skip to content

Persist the printer token on connect (survive unclean restarts) - #36

Open
VGottselig wants to merge 1 commit into
macdylan:masterfrom
VGottselig:fix-persist-token-on-connect
Open

Persist the printer token on connect (survive unclean restarts)#36
VGottselig wants to merge 1 commit into
macdylan:masterfrom
VGottselig:fix-persist-token-on-connect

Conversation

@VGottselig

Copy link
Copy Markdown

Problem

The printer token is only written to the known-hosts file by the shutdown defer in main(), which runs only on a graceful exit. On an unclean exit — SIGKILL, container restart, OOM, power loss — a freshly authorized token is lost, so the next start needs another touchscreen authorization.

There is also a second gap: the IP-only fallback printer (-host <ip> without discovery) is created without an ID:

printer = &Printer{IP: Host}

but LocalStorage.Add skips ID-less printers:

func (ls *LocalStorage) Add(printers ...*Printer) {
    for _, p := range printers {
        if p.ID == "" {
            continue   // <- fallback printer never stored
        }
        ...

so that printer's token is never persisted, even on a clean exit.

Fix

  • Add an OnPrinterUpdate hook, invoked right after a successful connect, that main() wires to ls.Add(printer) + ls.Save() — persisting the (refreshed) token immediately instead of only at shutdown.
  • Give the IP-only fallback printer an ID (its host) so ls.Add actually persists it.

No change to the normal shutdown path; this just makes persistence happen eagerly and closes the ID-less-fallback gap.

Testing

  • go build ./... clean.
  • Token now survives docker kill / container restarts and unclean power cycles — no re-authorization on the touchscreen on the next upload.

The token obtained from the printer was only written to the known-hosts
file by the shutdown defer in main(), which runs only on a graceful exit.
On an unclean exit (SIGKILL, container restart, OOM, power loss) a freshly
authorized token was therefore lost, forcing another touchscreen
authorization on the next start.

Additionally, the IP-only fallback printer (-host <ip> without discovery)
was created without an ID, and LocalStorage.Add skips ID-less printers, so
its token was never persisted even on a clean exit.

Fix:
- Add an OnPrinterUpdate hook, invoked right after a successful connect,
  which main() wires to ls.Add(printer) + ls.Save() -- persisting the
  (refreshed) token immediately instead of only at shutdown.
- Give the IP-only fallback printer an ID (its host) so ls.Add persists it.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018zWUoJhzouLTnkrpZE7rkJ
VGottselig added a commit to VGottselig/sm2uploader that referenced this pull request Jul 30, 2026
The README still only described upstream, so nothing in this fork was
documented: the web GUI, starting prints after upload, the token that
survives restarts, and the Spoolman filament bookings.

Everything upstream wrote stays as it was; the fork material sits below a
divider, with a pointer at the top so a visitor knows which repo they are
in and which two changes go upstream (macdylan#35, macdylan#36).

Documents the parts that are easy to get wrong when using it: bookings are
deltas so a correction is not a double booking, remaining weight is
computed instead of read because Spoolman clamps it at 0, and uploads.yaml
is the only record of what was booked -- deleting it orphans bookings
Spoolman still holds. Flags are tabled with their env fallbacks, plus the
compose snippet, because the container needs to share a network with
Spoolman before the hostname resolves.

Claims were checked against the code (poll intervals, row limits, net
weight, tail windows, make targets). The G-code section says what was
actually verified -- Snapmaker Orca 2.3.4 -- rather than implying
PrusaSlicer was tested too.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QHt1eWHFiSbJaNjKVhU38N
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