From 1be4206533e27b1f45003625159860a8cb14d186 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Sun, 22 Nov 2026 07:32:31 -0700 Subject: [PATCH] fix(system): the System Console releases the device before it resets into the bootloader A host that keeps a USB device open and claimed while the chip resets into the ROM bootloader (a WebUSB interface with an IN transfer pending) can leave the host -- macOS in particular -- holding a stale record of it, so the ROM's USB device never enumerates until the board is replugged: the reboot looks like a hang. Seen with a WebUSB console that asked for the reset 250 ms out and kept the device open. - system_console.html: a bootloader reboot asks for at least 1000 ms and closes the device (monitor stream stopped, interface released) as soon as the OK arrives. - README ("Host side: release the device before it resets"), system.rst and the protocol header document the rule for anyone implementing a host. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU --- components/system/README.md | 28 +++++++++++++++++++ .../system/include/detail/system_protocol.hpp | 5 ++++ components/system/web/system_console.html | 15 +++++++++- doc/en/system/system.rst | 13 +++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/components/system/README.md b/components/system/README.md index cab0570b23..2c5ed5d528 100644 --- a/components/system/README.md +++ b/components/system/README.md @@ -63,6 +63,7 @@ over HTTPS, or HTTP on localhost; a `file://` copy loses the grant on reload. - [System Component](#system-component) - [Protocol (module 7, `espp.system` v1)](#protocol-module-7-esppsystem-v1) + - [Host side: release the device before it resets](#host-side-release-the-device-before-it-resets) - [Example](#example) @@ -96,6 +97,33 @@ u32), 22 capabilities (u32: bit0 reboot allowed, bit1 bootloader reboot allowed and supported). The delay is clamped to at least `Config::min_restart_delay` so the OK reply leaves the transport before the restart. +## Host side: release the device before it resets + +A host that sends **REBOOT_TO_BOOTLOADER** over USB must have let go of the +device before the reset fires. A device that is still open and claimed at that +moment (a WebUSB interface with an IN transfer pending, a pyusb handle, an open +serial port) can leave the host holding a stale record of it; macOS in +particular. Then the ROM's USB device, which has other ids, does not enumerate +until the board is unplugged and plugged back in. To the user it looks as if the +device hung on its way into the bootloader. + +So, in a host (a web page, a script, a tool): + +1. Ask for a delay long enough to close the connection first: at least + **1000 ms**. The System Console sends `max(1000, the delay box)` for a + bootloader reboot. +2. As soon as the OK reply arrives, stop anything that keeps transfers in + flight (e.g. a monitor stream), release the interface and close the device + (WebUSB: `releaseInterface()` then `close()`; Web Serial: cancel the reader + and `close()` the port). If the link drops before the OK, the device is + already on its way; close your side the same way. +3. Only then wait for the ROM's device: it enumerates with different ids + (USB-Serial-JTAG `303a:1001` or the ROM's USB CDC `303a:0009` on the + ESP32-S3), so look for it rather than reconnecting to the old one. + +A plain **REBOOT** comes back with the same ids and is reconnected the usual +way, but the same order (close, then let it reset) never hurts. + ## Example The [example](./example) exposes `SystemService` and `MonitorService` on the diff --git a/components/system/include/detail/system_protocol.hpp b/components/system/include/detail/system_protocol.hpp index 717f81ac82..1d4a328194 100644 --- a/components/system/include/detail/system_protocol.hpp +++ b/components/system/include/detail/system_protocol.hpp @@ -13,6 +13,11 @@ // 0x01 GET_INFO (no payload) // 0x02 REBOOT [delay_ms u16] // 0x03 REBOOT_TO_BOOTLOADER [delay_ms u16] +// A host should ask for a delay long enough to +// close its connection first (>= 1000 ms) and +// close it as soon as the OK arrives: see the +// component README, "Host side: release the +// device before it resets". // Replies (device -> host, high bit set = frame reply flag): // 0x81 INFO a list of tagged records [tag u8][len u8][value...]; a host // skips tags it does not know, so fields can be added without a diff --git a/components/system/web/system_console.html b/components/system/web/system_console.html index f8e69fdece..56b3e0f972 100644 --- a/components/system/web/system_console.html +++ b/components/system/web/system_console.html @@ -1659,8 +1659,17 @@

Log

const v = Number.parseInt(els.rebootDelay.value, 10); return Number.isInteger(v) && v >= 0 && v <= 65535 ? v : 500; } + // Release the device before it restarts into the ROM bootloader: a WebUSB + // device that is still claimed (an interface and a pending IN transfer) + // while the chip resets can leave the host -- macOS in particular -- + // holding a stale record of it, so the ROM's USB device (other ids) does + // not enumerate until the board is replugged. The reset is asked for at + // least this far ahead, and the page closes the device as soon as the + // request is acknowledged, well before it fires. (A plain reboot comes back + // with the same ids and is reconnected as usual.) + const BOOTLOADER_REBOOT_MIN_DELAY_MS = 1000; async function requestReboot(bootloader) { - const delay = rebootDelayMs(); + const delay = bootloader ? Math.max(BOOTLOADER_REBOOT_MIN_DELAY_MS, rebootDelayMs()) : rebootDelayMs(); const type = bootloader ? SYS.REBOOT_TO_BOOTLOADER : SYS.REBOOT; logLine("tx", "> " + (bootloader ? "reboot into bootloader" : "reboot") + " in " + delay + " ms"); // A plain reboot keeps the device's USB ids: auto-reconnect picks it up @@ -1669,6 +1678,10 @@

Log

if (bootloader) reconnect.suppress(); else reconnect.expectReboot(); try { await transact("sys", type, u16(delay), [SYS.OK]); + if (bootloader && transport) { + await disconnect(); // let go of it before it resets (see above) + logLine("sys", "Released the device so it can restart into its ROM download mode."); + } logLine("sys", "Acknowledged: the device " + (bootloader ? "restarts into the ROM download mode (it re-enumerates as the ROM's USB CDC / DFU or USB-Serial-JTAG port; use esptool / idf.py flash, then reset it)." : (els.autoReconnect.checked ? "restarts shortly; reconnecting automatically once it is back." : "restarts shortly. Reconnect once it is back."))); diff --git a/doc/en/system/system.rst b/doc/en/system/system.rst index c8dfcee802..e1ceafbd4c 100644 --- a/doc/en/system/system.rst +++ b/doc/en/system/system.rst @@ -60,6 +60,19 @@ it doubles as a serial monitor): a device-info panel, the two restart buttons :doc:`monitor <../core/monitor>` component's ``MonitorService`` — heap-region gauges and a live, sortable task table with a stream toggle. +.. note:: + + **A host must release the device before it resets into the bootloader.** A + device that is still open and claimed when the chip resets (a WebUSB + interface with an IN transfer pending, a pyusb handle, an open serial port) + can leave the host -- macOS in particular -- holding a stale record of it, + so the ROM's USB device (other ids) does not enumerate until the board is + replugged. Ask for a delay of at least 1000 ms with ``REBOOT_TO_BOOTLOADER``, + and as soon as the OK reply arrives stop any stream, release the interface + and close the device; then wait for the ROM's device. The System Console + does exactly this. See the component README's *Host side: release the device + before it resets* for the details. + .. image:: https://github.com/user-attachments/assets/bc5fa520-f3a0-40de-9b53-889a887c0322 :alt: espp System Console: device info, reboot / bootloader controls and heap gauges :width: 100%