Skip to content

camera: Fix stop()/exec() hanging or losing the script after a soft reboot. - #28

Open
kwagyeman wants to merge 2 commits into
openmv:masterfrom
kwagyeman:fix/soft-reboot-hang
Open

kwagyeman wants to merge 2 commits into
openmv:masterfrom
kwagyeman:fix/soft-reboot-hang

Conversation

@kwagyeman

Copy link
Copy Markdown
Member

Fixes the "starting the on-camera script fails every other time" report (forum 11906): a host doing stop() then exec() hung forever on alternate runs.

Stopping a running script soft-reboots the camera. Its protocol sequence restarts at 0, the negotiated capabilities go back to defaults and dynamic channels are dropped. Commands sent before the host noticed were ACKed by the context being torn down (and their effect lost) or NAKed with a sequence error afterwards. The package then hung because of two transport bugs:

  • _check_seq dropped a NAK whose sequence number did not match the expected one at the header stage, so the sequence error was never seen and no resync was triggered.
  • Every event reset the receive deadline. With the KeyboardInterrupt traceback and "MPY: soft reboot" sitting unread in stdout, the camera sends a stdout notification every 50 ms, which kept the pending command waiting forever (the Unknown Event: channel=2, event=0xFFFF in the report).

Changes:

  • transport: accept ACK/NAK packets with any sequence number, so an out-of-sequence NAK raises and triggers a resync; let events pass without extending the command timeout.
  • camera: on the SOFT_REBOOT event mark a resync as pending, reset the sequence for in-flight packets and schedule a channel list refresh; the next command resyncs first. Without events there is no notification, so stop() marks the resync itself. This is what the IDE's client does.

Tested on OPENMV_N6 with the reporter's host and camera scripts: 10/10 stop+exec cycles against the released firmware (was 50%), 8/8 against the firmware with the matching protocol fix (openmv PR "protocol: Keep the session across a soft reboot").

A NAK whose sequence number did not match the expected one was dropped
by the header check, so a device that reset its protocol state (soft
reboot) could never trigger a resync. Events also reset the receive
deadline, so a device emitting stdout notifications every 50 ms kept the
pending command waiting forever. Accept ACK/NAK packets with any sequence
number and let events pass without extending the timeout.
Stopping a running script soft-reboots the device, which restarts its
protocol sequence at 0, resets the negotiated capabilities and drops the
dynamic channels. Commands sent before the host noticed were ACKed by the
context being torn down (and their effect lost) or NAKed afterwards.

On the SOFT_REBOOT event mark a resync as pending, reset the sequence for
in-flight packets and schedule a channel list refresh; the next command
resyncs first. Without events there is no notification, so stop() marks
the resync itself. This matches the IDE's client.
@kwagyeman kwagyeman changed the title Fix stop()/exec() hanging or losing the script after a soft reboot camera: Fix stop()/exec() hanging or losing the script after a soft reboot. Oct 26, 2026
@kwagyeman
kwagyeman requested a review from iabdalkader October 26, 2026 20:05
@kwagyeman kwagyeman added this to the v1.0.8 milestone Oct 26, 2026
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