Skip to content

Tear the menu down in the menu task when REST resets the machine - #1137

Open
JC-000 wants to merge 17 commits into
GideonZ:masterfrom
JC-000:fix/rest-reset-ui-teardown
Open

JC-000 wants to merge 17 commits into
GideonZ:masterfrom
JC-000:fix/rest-reset-ui-teardown

Conversation

@JC-000

@JC-000 JC-000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Fixes #1111.

A REST machine:reset that arrived while the menu was opening deleted the menu's windows from the HTTP task while the menu task was still drawing them, and the device stopped answering.

What changes

UserInterface::release_host() now checks which task calls it.

  • From the menu task itself, nothing changes.
  • From another task (REST, UCI, socket DMA), it only asks for the teardown and waits up to 2 s. The menu task serves the request between draws, in run_once(), and runs the deinit() loop itself. The request is a single state word (none, requested, serving) changed in a critical section, so a request that times out is withdrawn cleanly and cannot overlap a serve in progress.
  • On timeout release_host() returns false and its callers fail without touching the machine: C64_Subsys unfreeze, reset, clear memory, reboot and start cartridge answer SSRET_INTERNAL_ERROR (REST reset answers HTTP 500); the DMA load and raw DMA commands do the same; the socket DMA reset closes the connection; the control target reboot answers 8A,MENU NOT RELEASED.
  • The read-only text viewer (help, logs) closes on the request, so a REST reset with the help screen open resets as before.
  • Question and input dialogs (popup, choice, string box, forms, hex editor, monitor) are not ended. Ending them would hand their callers a result they read as an answer: a No on "Disk could not be saved. Continue?", or a -1 from string_box that config_menu.cc treats as true. So while one of these is open, a REST reset is refused with HTTP 500 and the machine and the dialog are untouched. This is a known limit.

Relation to #1109

#1109 is merged, and this branch now contains it. Both touch dma_load_raw_buffer. #1109 takes the ownership lock at the top of that function, so when release_host() is refused there, the function now calls c64->unlock_ownership() before return -1. Without that line the lock would stay held. #1109 alone does not fix this crash: on a build with only #1109, the same load test crashed the device in the menu task.

Tests

New suite menu-reset-race (tests/e2e/api/menu_reset_race_test.py, registered in run-tests, row added to the matrix in tests/README.md):

  • 60 rounds of menu button, delay, machine:reset; the device must keep answering. It fails if more than 3 of the 60 resets are refused.
  • Three resets with the help screen open; each must reset, close the menu and reach READY. This row also passes on unfixed master, so it guards the viewer path and is not a reproducer.

Hardware results, on an Ultimate 64 Elite over wired Ethernet, firmware loaded volatile over JTAG. All of these rows ran on builds made before master was merged into this branch:

Firmware Runs Result
master before #1109, no fix (fe40f0e5f) 3 the 60-round test killed the device 3 of 3 times
the fix on master before #1109 (dc301f291) 10 all pass, no refused resets
the same, under load (menu button presses, REST memory reads and writes, UCI command loop) 3 device stays up
the fix plus #1109 (5e3614c95) 3 all pass
#1109 only, same load 1 device crashed in the menu task

After merging master (head e1dfbaaaf): make host_tests passes in the Linux container, and the u64 and u64ii builds pass the size report. The merge left the fix's code unchanged, apart from the unlock_ownership() line described above. The tested fix-plus-#1109 build 5e3614c95 has identical software/io/c64 sources. The only differences in the fix's own files are unrelated upstream edits in userinterface.cc (null checks on the progress box and a freed message when the queue is full). I did not re-run the hardware rows on the merged head.

The refusal with a question dialog open is untested on hardware. The only dialogs I could reach with keys are in the C64 Machine submenu, next to Power Off and the reboot rows, and I did not want an automated test navigating there.

The U2+ (Nios) and U2 (RISC-V) applications were compiled and linked before the merge, and not rebuilt since. I could not build their images here. U2+L is not built.

Written with Claude Fable 5.2 and Claude Sonnet 5.5 in Claude Code.

🤖 Generated with Claude Code

JC-000 and others added 17 commits November 19, 2026 07:00
Presses menu_button, waits a swept delay, sends machine:reset and requires
the device to keep answering. It reproduces the use-after-free of the menu's
windows (REST reset releases the UI from the HTTP task while the menu task
draws) that stops the device on a fast host link.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
UserInterface::release_host() is called by C64_Subsys::executeCommand from
whichever task runs the command; REST machine:reset runs it on the HTTP task.
That deinit()ed the menu's windows while the menu task was still drawing
them (appear() / TreeBrowserState::draw_item), a use-after-free that ends in
a Nios exception and a dead device.

A caller on another task now only requests the teardown; run_once() performs
it between draws. The caller waits up to 2 s and carries on if the menu task
is busy in a blocking dialog, the request then being served when it returns.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A REST caller that holds the C64 ownership lock waits for the menu task to
tear down; the menu task can then block on that same lock before it reaches
the loop again. Clearing the request once the windows are gone lets the
caller go at once instead of timing out after 2 s.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… going on

A request from another task that the menu task does not serve within 2 s is
withdrawn and release_host() returns false. Every C64_Subsys caller (reset,
unfreeze, clear memory, reboot, start cartridge, DMA loads and raw DMA) then
returns an error and leaves the machine alone, so a REST reset with a menu
popup open answers an error instead of resetting under a live menu. The
destructor clears a client that points at it and the owner task.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
popup, string_box, choice and the other blocking loops already end on a menu
button push. A pending request from release_host() now ends them the same
way, so run_once() can serve it and a REST reset with a popup open resets
instead of failing after the 2 s wait.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Opens the Save REU Memory string box in the menu task and resets over REST;
the device must reset and answer. RETURN is pressed only after the highlighted
row has been read.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The client pointer is private to C64; the helper and the UserInterface
destructor need friend access or a public accessor to reach it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Navigating to a string box passed rows next to Power OFF and the highlight
could not be read reliably; F3 opens the help screen, which runs in the same
kind of nested loop, with a single key and no row activated.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Ending a blocking popup, choice or string_box on a foreign request returns 0,
which several callers read as consent: filetype_bin.cc (BUTTON_NO: an
unverified ROM is copied or set), network_esp32.cc (choice 0 is "Open", so a
connect without a password, or a half-typed one, goes ahead) and c1541.cc
(continue without saving). While a blocking dialog is open, a release request
from another task therefore times out and its caller fails cleanly (REST reset
answers an error; machine and menu are untouched). That is the known limit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…he clients

- The release request is one state word (none, requested, serving) changed in
  a critical section, so the 2 s withdrawal cannot overlap a serve; a caller
  returns true if the menu closed meanwhile. The wait runs to a tick deadline.
- run_once() leaves the loop after a served request, so MENU_EXIT/HIDE in the
  same pass does not tear the windows down a second time.
- A refused release now fails the command: the DMA load and raw DMA commands
  set SSRET_INTERNAL_ERROR, the socket DMA reset closes the connection and the
  control target reboot answers "8A,MENU NOT RELEASED".
- Drop C64::drop_client and the destructor block; the case cannot occur (menus
  that are deleted run on a stream and never use run_once or the C64 client).
- Known limit, in the code comment: while a blocking dialog is open the menu
  task cannot serve the request, so the caller fails after the wait.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…changes nothing

With the help screen open, machine:reset must answer an error within a few
seconds, leave the help screen up, keep the device answering, and a reset
after closing the menu must succeed. A refused reset in the race rounds is
counted, not a failure. Add the suite to the tests/README.md matrix (only its
own row; the matrix has other stale rows at the base).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
SocketDMA now reads the status of the reset command it runs.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Restores the nested-loop abort (reverting the revert): a REST reset with the
help screen open resets, as on master, instead of failing. Each loop that
ends because another task asked for the teardown returns what Escape returns
in it, never 0 and never a yes: string_box and string_edit -1, choice -1,
forms MENU_CLOSE; a popup has no Escape, so it returns its Cancel, else No,
else Ok button (the custom-button popup its last button). The editor/help and
monitor loops are void and just return. The test row expects a clean reset
again and guards this unwind path; it is not a RED reproducer.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Ending a question or input dialog on a release request hands its caller a
result it can read as an answer: a No on c1541.cc "Save?" drops the user's
disk writes, and a -1 from string_box is truthy in config_menu.cc and its
sibling callers, whose half-edited buffer is then committed. So popup,
choice, string_box, string_edit, forms, the hex editor and the monitor are
not ended. Known limit: a REST reset is refused (HTTP 500, machine and
dialog untouched) while a question or input dialog is open on the menu.
Only run_editor(const char *, int), the read-only text viewer (help, logs),
closes on the request; a REST reset with the help screen open resets.
This replaces the wording of the nested-loop commits before it, where every
nested loop returned its Escape value.

The race test fails if more than 3 of 60 resets are refused.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Only the read-only viewer (the help screen) closes on the release request;
question and input dialogs do not, and a REST reset gets HTTP 500 while one
is open.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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.

Ultimate 64 Elite stops answering when REST PUT /v1/machine:reset arrives while the menu is opening

1 participant