Skip to content

[nexus] unwind instance-start sagas if they race with the VMM going away - #11608

Merged
hawkw merged 14 commits into
mainfrom
eliza/fix-11606
Nov 3, 2026
Merged

hawkw merged 14 commits into
mainfrom
eliza/fix-11606

Conversation

@hawkw

@hawkw hawkw commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

This branch fixes #11560, a race condition which occurs when an instance
is requested to stop through the external API while an instance_start
saga for that instance is in progress.

The race occurs when the stop request is received while the VMM record
is Creating, and is forwarded to the sled-agent. The Creating state
represents a VMM where we have allocated a sled to run the instance and
created a VMM record for the Propolis process we intend to start, but
have not yet sent a vmm_ensure request to the sled-agent.

When the sled-agent receives the request to stop the VMM, it has not yet
received an ensure request to create that VMM, and therefore
(correctly) indicates that it does not exist, which results in Nexus
moving the VMM to Failed and kicking off an instance_update saga to
transition the instance to Failed and delete the vmm record.
However, this is invisible to the start saga, which continues its
attempt to register and run the VMM, rather than unwinding. This
ultimately results in the sled-agent running a VMM that the control
plane considers dead. The control plane may then attempt to restart the
instance elsewhere, which gets stuck when the new VMM tries to activate
the same crucible disk(s) that the zombie VMM is already using.

This branch closes the race by having the
Nexus::instance_ensure_registered method read back the database record
for the VMM to ensure that it successfully advanced the state to
Starting after sending the vmm_ensure request to the sled-agent.
I've included a test reproducing the race, which uses the new test
utilities I added in #11606, to temporarily interrupt the sled-agent
simulator and advance the VMM to Failed before responding to the
vmm_ensure request. Prior to this change, the start saga continues,
now it unwinds correctly.

In my opinion, the behavior of forwarding requests to the sled-agent
when a VMM is in the Creating state remains wrong. We allow a user
to "cancel" starting an instance when it is in the Starting state, and
the Creating VMM state maps to the Starting instance state in the
external API. Therefore, we shouldn't just change the API to reject
requests to stop an instance with a Creating VMM: I really want to
avoid leaking the fact that some external-API states may represent
multiple internal states, and therefore really dislike having Starting
sometimes let you stop the instance but other times not let you. Now
that we fixed the race, the current behavior does do more or less the
correct thing, it just does it in a kind of awful way. When the VMM
has not yet been started on the sled, we shouldn't send a request to the
sled to stop it and rely on the fact that the sled-agent will mark it as
failed to unwind the start saga. But, this PR doesn't change any of
that; it just fixes the dangerous consequences of this behavior.
Subsequently, we may want to change how instances are stopped when the
VMM is in the Creating state. I'd prefer to do that in a separate
change.

Fixes #11560.

@hawkw
hawkw added this pull request to stack #11609 October 28, 2026 00:26
@hawkw hawkw added bug Something that isn't working. nexus Related to nexus labels Oct 28, 2026
@hawkw

hawkw commented Oct 28, 2026

Copy link
Copy Markdown
Member Author

There may be a less horrific way to fix this, but, this certainly seems to work, and it seemed like it was a solution that would be robust with regards to other changes in this vicinity, even though all this code is kind of a general pile of broken glass and rusty nails. I can try to explain the thinking here better tomorrow, but for now I really need to step away from the computer.

@hawkw
hawkw requested a review from iximeow October 28, 2026 00:29
@hawkw

hawkw commented Oct 28, 2026

Copy link
Copy Markdown
Member Author

After thinking it over a bit more, I think it might be somewhat neater to:

  • move instance_ensure_unregistered to the undo action of (new) a node immediately before the sis_ensure_registered node, rather than its undo action. That way, it always executes when unwinding, regardless of whether the ensure succeeds or fails. The forward action of that new node would be a no-op.
  • ensure instance_ensure_unregistered is idemoptent (it is, but it should probably retry on transient failures)
  • allow sis_ensure_registered to fail if the DB state was not written because the generation advanced (this requires a lot of annoying plumbing)

This way we don't need the kinda weird separate "did it actually work?" node.

@hawkw
hawkw marked this pull request as draft October 28, 2026 21:07
@hawkw
hawkw removed this pull request from stack #11609 October 29, 2026 16:10
@hawkw
hawkw changed the base branch from eliza/vmm-registration-pause to eliza/retry-ensure-unregistered October 29, 2026 16:53
@hawkw
hawkw added this pull request to stack #11630 October 29, 2026 16:54
@hawkw
hawkw removed this pull request from stack #11630 October 29, 2026 17:04
@hawkw
hawkw changed the base branch from eliza/retry-ensure-unregistered to eliza/always-ensure-unregistered October 29, 2026 17:24
@hawkw
hawkw added this pull request to stack #11632 October 29, 2026 17:24
@hawkw
hawkw marked this pull request as ready for review October 29, 2026 22:55
Base automatically changed from eliza/always-ensure-unregistered to main October 30, 2026 15:57
@hawkw

hawkw commented Oct 30, 2026 •

Copy link
Copy Markdown
Member Author

In my opinion, the behavior of forwarding requests to the sled-agent
when a VMM is in the Creating state remains wrong. We allow a user
to "cancel" starting an instance when it is in the Starting state, and
the Creating VMM state maps to the Starting instance state in the
external API. Therefore, we shouldn't just change the API to reject
requests to stop an instance with a Creating VMM: I really want to
avoid leaking the fact that some external-API states may represent
multiple internal states, and therefore really dislike having Starting
sometimes let you stop the instance but other times not let you. Now
that we fixed the race, the current behavior does do more or less the
correct thing, it just does it in a kind of awful way. When the VMM
has not yet been started on the sled, we shouldn't send a request to the
sled to stop it and rely on the fact that the sled-agent will mark it as
failed to unwind the start saga. But, this PR doesn't change any of
that; it just fixes the dangerous consequences of this behavior.
Subsequently, we may want to change how instances are stopped when the
VMM is in the Creating state. I'd prefer to do that in a separate
change.

Regarding this: I think this might be as simple as:

  • if the VMM is Creating then try to advance its state to Stopping, conditional on the generation number not having advanced
    • if the state advances to Stopping, then the start saga will try to advance it to Starting after registering the VMM with the sled, which will fail, and the start saga unwinds, destroying the VMM. The instance will then move to Stopped successfully.
    • if the state is no longer Creating, then the VMM is now present on the sled, so send the stop request to the sled-agent. This will stop the VMM normally.

But, it's complex enough that I want to think a bit harder about it. I don't think what I'm describing here will introduce any new races, but...

@jgallagher jgallagher 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.

Regarding this: I think this might be as simple as:

  • if the VMM is Creating then try to advance its state to Stopping, conditional on the generation number not having advanced
    • if the state advances to Stopping, then the start saga will try to advance it to Starting after registering the VMM with the sled, which will fail, and the start saga unwinds, destroying the VMM. The instance will then move to Stopped successfully.
    • if the state is no longer Creating, then the VMM is now present on the sled, so send the stop request to the sled-agent. This will stop the VMM normally.

If the state advances to Stopping in this scenario, will that kick off another saga (instance update?) that's now racing with the still-running start saga? If so, will the instance + VMM end up in the expected state regardless of which one manages to tell sled-agent to stop first?

paused_registration.resume();
let saga_result =
running_saga.wait_until_stopped().await.into_raw_result();
match dbg!(saga_result.kind) {

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.

nit - leftover dbg!()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I like leaving dbg! macros like this in tests, since if the test fails, nextest prints them all out to stdout without requiring you to track down the log file in /tmp to see what happened...but I can remove it if you really don't like that.

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'm not a huge fan; if I see it I assume it was leftover, and if someone wanted intentional output they use {e,}println!(). But it's really more of a "oh this looks like an accident" than any preference.

Comment thread nexus/src/app/instance.rs Outdated
// caller. We shall check whether the state we wrote back to the
// database is the VMM's current state. If it is not in the expected
// state, then something else has acted on that VMM record, with which
// we may have raced!

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 feels pretty sketchy to me, but some of that is certainly from a mismatch between what I (naively) expected notify_vmm_updated() to do and what it actually does, so maybe this is all fine. I'll just ramble here and see if anything makes sense.

At a glance it doesn't seem like fetching the state here to see if it matches what we wrote can really fix any race, because the db state could change between when we fetch it and when we return Ok(_) to the caller, so an Ok(_) return can't mean "the db state, if you were to read it right now, would match what I just wrote". If that's a thing the caller cares about, they would need to fetch it themselves.

However, all that reasoning is based on "see if it matches what we wrote", because I assumed notify_vmm_updated() would fail if we failed to write to the db (i.e., because our data is stale). That's also supported by the "We shall check whether the state we wrote back to the database is the VMM's current state." in this comment block.

But notify_vmm_updated() doesn't fail even if it didn't write to the db - if our generation is out of date, we end up returning Ok(UpdateAndQueryResult { status: UpdateStatus::NotUpdatedButExists, found }) from vmm_update_runtime_on_connection(). That bubbles out into an Ok(VmmStateUpdateResult { found_vmm, vmm_updated: false, ..migration fields.. }) from vmm_and_migration_update_runtime(). That's used to decide whether process_vmm_update() returns an update saga that's kicked off by notify_vmm_updated(), but by that point we've lost the found_vmm we loaded from the db and the details about whether or not our write actually landed.

Given all of that, I think my mild order of preference (ignoring work involved) would be something like:

  1. Change the call stack of notify_vmm_updated() so that at this point, we know whether our write landed. Maybe we also want the found_vmm record to decide what kind of error to return?
  2. Reword the comment here to try to summarize the above - it's not that we need to see if what we wrote is still there (which is inherently racy) - it's that we need to see whether our write landed at all. I don't like this as much because I think this will also fail if our write did land, but then another write landed afterwards - failing here just because we didn't read our write "fast enough" doesn't seem correct.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, so, you are correct that the behavior of notify_vmm_updated kind of hiding whether the write succeeded or failed makes this all a bit trickier. I had originally wanted to do, essentially, your option (1) here myself, rather than doing a second read afterwards. The tricky bit is that the other callers don't want it to return an error if the write was a stale generation, so we would want to bubble up the found_vmm and allow the caller to compare it with the generation they were trying to write, instead. That's where I...started getting sketched out by the semantics of check_if_exists, since if the write succeeds, it actually returns the previous value, rather than the one that was written...

@hawkw hawkw Nov 2, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Okay, so it ended up changing a lot more code than I had hoped, but a7ebf22 changes things based on the thread I started pulling while thinking through this suggestion. I think it makes this all a lot less sketchy. In particular, I've done the following:

  1. Nexus::notify_vmm_updated and its callstack (i.e. process_vmm_update,
    DataStore::vmm_and_migrations_update_runtime) to return the updated VMM state, so we can eliminate the re-query. This is done by changing the return value to a struct ProcessedVmmUpdate that contains any needed update saga alongside the database update result.
  2. Then, I've changed the check for whether or not Nexus::instance_ensure_registered should return an error to specifically check for whether the requested state is now in the database based on that return value. This is not done using the check_if_exists value that indicates whether this write was the one that wrote the expected state, because the function must still succeed if called idempotently in a saga.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's possible some of this refactoring could be pulled out into its own PR, but I don't really know if it's worth it at this point.

@hawkw

hawkw commented Nov 2, 2026

Copy link
Copy Markdown
Member Author

Regarding this: I think this might be as simple as:

  • if the VMM is Creating then try to advance its state to Stopping, conditional on the generation number not having advanced

    • if the state advances to Stopping, then the start saga will try to advance it to Starting after registering the VMM with the sled, which will fail, and the start saga unwinds, destroying the VMM. The instance will then move to Stopped successfully.
    • if the state is no longer Creating, then the VMM is now present on the sled, so send the stop request to the sled-agent. This will stop the VMM normally.

If the state advances to Stopping in this scenario, will that kick off another saga (instance update?) that's now racing with the still-running start saga? If so, will the instance + VMM end up in the expected state regardless of which one manages to tell sled-agent to stop first?

No, Stopping is not a state that requires an instance-update saga. The instance-update saga is only necessary if the active VMM changes to Destroyed or Failed, or if a migration completes.

However, I've been continuing to push on this in a separate branch, and I think it will probably end up having to be a new VMM state, since Stopping is a state that is considered to "exist on a sled" and therefore gets health-checked by the instance_watcher background task, which will try to advance the VMM to Failed if it queries the sled-agent after it's been unregistered. I think we may need to have a special state for VMMs which were requested to stop while there was an in-progress start saga (I'm tentatively naming this Cancelling). This would ensure that the VMM is "owned" by the start saga and the periods during which it may or may not exist on the sled are resolved by the start saga unwinding, without other parts of Nexus trying to talk to the sled-agent about it.

I'm hoping to have a PR up for these proposed changes soon, but it's hairy enough that I want to make sure there are a lot more tests first. I'm also pretty leery of making the (already quite complex) state machine even more complicated. There is always the alternative approach of just saying that Starting instances can't be stopped or rebooted until they are Running, which certainly simplifies things a lot, but I don't love the UX of that...

This more or less addresses the suggestions from
#11608 (comment).
It ends up being a somewhat larger change than I had hoped, but I think
it makes this all a lot less sketchy.

In particular, I've changed `Nexus::notify_vmm_updated` and its
callstack (i.e. `process_vmm_update`,
`DataStore::vmm_and_migrations_update_runtime`) to return the updated
VMM state, so we can eliminate the re-query. This is done by changing
the return value to a struct `ProcessedVmmUpdate` that contains any
needed update saga alongside the database update result. Then, I've
changed the check for whether or not `Nexus::instance_ensure_registered`
should return an error to specifically check for whether the requested
state is now in the database based on that return value. This is *not*
done using the `check_if_exists` value that indicates whether *this*
write was the one that wrote the expected state, because the function
must still succeed if called idempotently in a saga.

@jgallagher jgallagher 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.

Thanks, I like the revised changes rather a lot more. 👍 Just a handful more questions - thanks for your patience.

Comment thread nexus/src/app/instance.rs Resolved
Comment thread nexus/src/app/instance.rs Resolved
} else {
// If we *didn't* update the VMM state, we can just return
// the value that `check_if_exists` gave us.
found_vmm

@jgallagher jgallagher Nov 2, 2026 •

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.

Since TIL check_if_exists doesn't check for soft-deleted rows, if it was soft-deleted when we ran the update, I think the behavior would be:

  • our update wouldn't apply because it has a filter(dsl::time_deleted.is_null())
  • but check_if_exists's CTE would still find the row since it doesn't filter out soft deletes

and we'd return it here. Maybe that's fine? But it seems weird - should we check for found_vmm.time_deleted.is_some() and do... something different (???) if it is?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, so, hm. If this could get called in an unwinding saga, this behavior would, unfortunately, be correct, since we would want undo actions that try to mutate the state of a VMM record that has been soft-deleted to succeed, rather than erroring out and getting the unwinding saga stuck. If that were the case, I think we would have to make it the caller's responsibility to decide if "VMM record was soft-deleted" should be an error or not.

However, I don't think this method is called in an undo action, but...I'll have to double check. If it isn't, we could probably make it return an error if the VMM has been deleted.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hmm, actually, if we returned an error here if the VMM record has been soft-deleted, that would also bubble-up into an external API request to stop the instance, if some other actor has already observed the VMM go to the Destroyed state and deleted it before the API endpoint returned. In that case, we would potentially turn a successful stop into an error, which seems bad. 😬

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.

Returning an error from trying to stop an instance that someone else deleted seems okay? Oh, or is the order there something like:

  • stop request comes in
  • we stop the VMM but are still running the saga
  • someone else successfully soft deletes the instance
  • we get here and have to decide what to do

? So we did successfully stop it, then someone else deleted it before we returned? Yeah that seems unfortunate 😬

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's not the instance record that would be soft-deleted, but the VMM record. The VMM record for a Destroyed or Failed VMM may be soft-deleted after the corresponding instance record has been marked as Stopped and has had the VMM record. detached from it. In that case, the instance has not been deleted, just the VMM that it was previously using, and we don't want to return an error that incorrectly suggests the instance has been deleted.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The important thing here is to remember that records in the VMM table don't represent externally visible API objects, so the caller doesn't care whether or not they have been deleted (since they don't directly correspond to a resource the caller can view or interact with).

A deleted VMM doesn't mean the instance is deleted, it means that the instance is no longer running on that VMM (either because it has no VMM and is stopped, or because it is running someplace else), and the control plane has completed cleaning up the networking state and virtual resource provisioning records associated with that VMM, so it deleted the VMM record after transitioning the instance to Stopped (if the VMM was destroyed due to stopping), restarted the instance (if the VMM failed), or updated the instance to point at the VMM it migrated to (if the instance migrated out).

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.

Ahhh right, thanks.

@hawkw

hawkw commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

@claude review

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code review found no issues

No high-confidence issues detected in this change.

@hawkw
hawkw merged commit 778db1c into main Nov 3, 2026
20 checks passed
@hawkw
hawkw deleted the eliza/fix-11606 branch November 3, 2026 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something that isn't working. nexus Related to nexus

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Instance state change gets 404 during start saga; Nexus marks VMM as Failed but propolis keeps running.

2 participants