Skip to content

services/flash: defer erase instead of blocking the whole server - #703

Open
JesseMelon wants to merge 6 commits into
OpenPRoT:mainfrom
JesseMelon:flash-erase-nonblocking
Open

JesseMelon wants to merge 6 commits into
OpenPRoT:mainfrom
JesseMelon:flash-erase-nonblocking

Conversation

@JesseMelon

Copy link
Copy Markdown
Contributor

Summary

Addresses #691: a slow or stuck erase no longer blocks every other channel's requests.

  • FlashDriver::start_erase/is_busy on the AST1060 FMC backend are now genuinely non-blocking (SpiNorFlash gets a split start_erase_sector/write_in_progress), instead of is_busy hardcoding false.
  • services/flash/server reports Outcome::Pending for an erase that isn't done yet; services/flash/server-runtime uses ServiceLoop::defer/complete to take that channel out of the WaitGroup so other channels keep being served, polling at 1ms with a 5s timeout rather than waiting forever.
  • If a deferred erase is abandoned (timeout, or the defer bookkeeping itself fails), the channel is marked unsafe and rejects every request except GET_INFO until a background poll confirms the driver actually went idle — a client giving up does not let a new client race hardware that may still be executing a command.
  • BlockingFlash::erase/program's wait loop is fixed to actually check is_busy instead of assuming one wait always means done (a latent bug this change would otherwise have exposed).

Deliberately out of scope: program is not deferred — a multi-window program needs its own cross-poll state machine to defer safely, which is a larger, separate change. BlockingFlash::erase's own direct blocking path has no timeout bound (nothing calls it directly today; bounding it needs a way to construct a driver-specific error from generic code, a design question rather than a quick fix).

Tests added in services/flash/server/tests/server.rs cover the defer/poll/abandon/recover paths with a fake that reports busy for a fixed number of polls — this path had no coverage before.

@JesseMelon
JesseMelon force-pushed the flash-erase-nonblocking branch from 9f67041 to 7e8a29c Compare November 13, 2026 16:08
@JesseMelon
JesseMelon marked this pull request as ready for review November 13, 2026 16:09

@EnmanuelParache EnmanuelParache left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes look good and serve the purpose of deferring erase operation.

I just have a couple observations that could be discussed in a separate issue.

  • We could make use of embedded_hal_nb when implementing drivers that would block

  • Flakiness tests for flash_service:flash_evb_test failed 3 of 3 runs on the board. Every run ended identically:

flash server: ready
read: offset 0x00100000, size 0x1000, flash ptr 0x88100000
[ERR] flash server stopped
TEST_RESULT:FAIL

Comment thread target/ast10x0/backend/flash/src/lib.rs Resolved
Comment thread target/ast10x0/backend/flash/src/lib.rs Resolved
@EnmanuelParache EnmanuelParache self-assigned this Nov 16, 2026
@JesseMelon
JesseMelon force-pushed the flash-erase-nonblocking branch from 1275a4f to 9425efb Compare November 17, 2026 14:59
@JesseMelon
JesseMelon force-pushed the flash-erase-nonblocking branch from 9425efb to 9ad05b1 Compare November 17, 2026 15:05
@JesseMelon

Copy link
Copy Markdown
Contributor Author

This changes look good and serve the purpose of deferring erase operation.

I just have a couple observations that could be discussed in a separate issue.

  • We could make use of embedded_hal_nb when implementing drivers that would block
  • Flakiness tests for flash_service:flash_evb_test failed 3 of 3 runs on the board. Every run ended identically:
flash server: ready
read: offset 0x00100000, size 0x1000, flash ptr 0x88100000
[ERR] flash server stopped
TEST_RESULT:FAIL

Test flakiness resolved after rebasing onto main

@rusty1968

Copy link
Copy Markdown
Collaborator

Let's hold off merging this code until I analyze the earlgrey and veer cases with this code.

@rusty1968 rusty1968 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please hold-off merging until I carefully review this against earlgrey and veer

rusty1968 and others added 2 commits November 18, 2026 12:12
… DeferredErase

Proposal on top of OpenPRoT#703. BlockingFlash::erase goes back to start_erase,
wait_for_notification, complete_op, so correctness does not depend on
is_busy and the AST10x0 blocking path is bounded again.

Deferral becomes an explicit capability instead of a change to every
driver's semantics:
- hal_flash_driver::DeferredErase: a driver that can start an erase
  without waiting (contract: is_busy is accurate until complete_op).
- hal_flash::DeferredFlash<D: DeferredErase, B>: wraps BlockingFlash;
  erase/read/program delegate unchanged, start_erase uses
  start_erase_nowait and poll_erase consults is_busy/complete_op.
- BlockingFlash keeps the Flash defaults (blocking start_erase,
  poll_erase == Done). FlashDriver is untouched.

AST10x0: start_erase uses the blocking erase_sector again; the
non-blocking start_erase_sector is exposed through DeferredErase, and the
flash_service server uses DeferredFlash. Earlgrey and VeeR are unchanged
and stay on BlockingFlash until they opt in.

Tests: erase does not touch is_busy or the nowait start (also through
DeferredFlash); BlockingFlash::start_erase is synchronous; the deferred
path reports Busy then Done; complete_op errors surface from poll_erase.

Only //hal/blocking/flash:flash_test was run. The AST10x0 backend and
server changes were not built, and ./pw presubmit was not run.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
DeferredFlash::poll_erase never went through Blocking, so for a driver
with an interrupt-backed Blocking (Earlgrey) the completion signal of a
deferred erase would be left pending. The next blocking program/erase
would then return from wait_for_notification at once and fail in
complete_op with FLASH_GENERIC_BUSY.

On the Busy -> Done transition, call wait_for_notification once, before
complete_op. The operation has already finished, so this returns
promptly and consumes the notification. It is a no-op for NoWaitBlocking
(AST10x0) and NoWait (VeeR).

Contract: a driver implementing DeferredErase must only report !is_busy()
once its completion notification has been raised.

Test: a counting Blocking checks that start and Busy polls do not wait
and that the Done poll waits exactly once.

Only //hal/blocking/flash:flash_test was run. The AST10x0 backend and
server changes, the services/flash server tests and ./pw presubmit were
not run.

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

The docs still said a deferred erase never goes through Blocking, which
stopped being true when poll_erase began consuming the completion
notification on the Busy -> Done transition.

State the rule once, on DeferredErase: is_busy must not return false
until the completion notification has been raised, and DeferredFlash
consumes it exactly once on Done. DeferredFlash and poll_erase now refer
to it. Comment-only; no code change.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@rusty1968

Copy link
Copy Markdown
Collaborator

I opened a proposal against Jesse's branch: JesseMelon#26. It keeps BlockingFlash::erase as start / Blocking wait / complete_op, and makes deferral an opt-in DeferredErase capability with a DeferredFlash wrapper. The description explains the AST10x0 spin-and-hang concern, and two follow-ups for this PR (recover_abandoned_erase clearing on Err(_), and AST is_busy mapping errors to busy). I ran the hal_flash tests and the VeeR host and emulator flash tests; the AST10x0 build and the services/flash server tests are not yet run.

@rusty1968
rusty1968 requested a review from yvettewu1 November 19, 2026 18:16
@rusty1968

Copy link
Copy Markdown
Collaborator

@yvettewu1 , can you comment on the behavior of the Flash ?

@yvettewu1

yvettewu1 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

@rusty1968 yes. I think making deferral an opt-in DeferredErase capability with a DeferredFlash wrapper is a much safer approach. This preserves the existing blocking behavior while adding non-blocking erase support without affecting existing users of BlockingFlash.

@rusty1968
rusty1968 requested a review from swenson November 19, 2026 19:08
@EnmanuelParache
EnmanuelParache self-requested a review November 19, 2026 19:17

@swenson swenson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few small things, code overall looks good.


/// Polls an erase begun with [`start_erase`](Self::start_erase).
///
/// `Err` must mean the busy check already came back false, same as `Done`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is a bit confusing. What is the same as done? What does it mean for the busy check to come back false and why is that an error?

Also, do we want to have a default implementation for this?


/// Outcome of polling an operation started with [`Flash::start_erase`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OpPoll {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is only for erase polling and we don't anticipate supporting other uses for this enum, I might suggest changing the name to something like EraseStatus or OpErasePoll?

fn program(&mut self, start_addr: FlashAddress, data: &[u8]) -> Result<(), Self::Error> {
self.0.program(start_addr, data)
}
/// Starts the erase and returns without waiting; `poll_erase` reports its real status.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this comment is adding anything that the parent trait comment doesn't say.

loop {
if let Event::Request { channel, len } = service.next(Instant::MAX, buf)? {
serve(&mut service, servers, channel, buf, len);
let needs_poll = |s: &ChannelState| !matches!(s, ChannelState::Idle | ChannelState::Dead);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly confusing to have this as a !matches!. Why not just use a positive matches!, and why not just a method on the enum itself instead of an inline function here?

match self.handle_request(buf, len) {
Ok(Outcome::Pending) => Outcome::Pending,
Ok(Outcome::Done(reply_len)) => {
Outcome::Done(Self::write_status_frame(buf, 0, reply_len))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than using a magic value of 0 for the status to indicate no error, I think it would be more Rust idiomatic to use an enum that is either Done or a NonZero error code and have it implement something like Into<u32> or Into<[u8; 4]>

@yvettewu1 yvettewu1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these changes will affect the underlying flash erase operation. However, please validate the deferred erase functionality on actual AST10x0 hardware, including access to another chip select while an erase is in progress.

This branch has not been deployed

No deployments
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.

5 participants