From dbe39fb2b44d64fa1d53bd6ab53315247209a515 Mon Sep 17 00:00:00 2001 From: Willem Vanhulle Date: Sun, 25 Oct 2026 13:20:44 +0100 Subject: [PATCH] fix: arm the root switch from signals that arrive with a disconnect --- src/systemd_link/boot_feed.rs | 50 ++++++++++++++++++++++----------- src/systemd_link/systemd_bus.rs | 42 ++++++++++++++++----------- 2 files changed, 60 insertions(+), 32 deletions(-) diff --git a/src/systemd_link/boot_feed.rs b/src/systemd_link/boot_feed.rs index 62a13d1..05d4625 100644 --- a/src/systemd_link/boot_feed.rs +++ b/src/systemd_link/boot_feed.rs @@ -20,7 +20,8 @@ use crate::{ monotonic_clock::monotonic_now, switch_root::Sysroot, systemd_bus::{ - BusConnection, FetchedUnit, JobListing, Scope, Session, Signal, StatusOutput, + BusConnection, FetchedUnit, JobListing, Scope, Session, Signal, SignalBatch, + StatusOutput, }, }, }; @@ -95,6 +96,13 @@ impl TrackedUnit { } } +fn queued_units(signals: &[Signal]) -> impl Iterator { + signals.iter().filter_map(|signal| match signal { + Signal::JobNew { unit, .. } => Some(unit.as_str()), + _ => None, + }) +} + struct BootTracker { scope: Scope, target: RootRequest, @@ -196,14 +204,30 @@ impl BusFeed { impl BootTracker { async fn receive(&mut self, connection: &mut BusConnection) -> Result, Error> { - let signals = connection - .receive_signals(BACKGROUND_CHECK_INTERVAL) - .await?; - Ok(self - .collect_changes(connection, signals, async |tracker, connection, signal| { - tracker.changes_for(connection, signal).await - }) - .await) + match connection.receive_signals(BACKGROUND_CHECK_INTERVAL).await { + SignalBatch::Open(signals) => { + self.arm_root_switch(queued_units(&signals)); + Ok(self + .collect_changes(connection, signals, async |tracker, connection, signal| { + tracker.changes_for(connection, signal).await + }) + .await) + } + SignalBatch::Closed(signals) => { + self.arm_root_switch(queued_units(&signals)); + Err(Error::Disconnected) + } + } + } + + fn arm_root_switch<'unit>(&mut self, mut queued_units: impl Iterator) { + if self.phase == Phase::Initrd + && matches!(self.root_switch, RootSwitch::NotQueued) + && queued_units.any(|unit| unit == SWITCH_ROOT_UNIT) + && let Ok(sysroot) = Sysroot::hold() + { + self.root_switch = RootSwitch::Queued(sysroot); + } } async fn collect_changes( @@ -288,6 +312,7 @@ impl BootTracker { .into_iter() .filter(|job| shutting_down || job.kind == JobKind::BringsUp) .collect(); + self.arm_root_switch(jobs.iter().map(|job| job.unit.as_str())); let requested_root = self.requested_root(connection, &jobs).await?; let boot_progress = self.boot_progress_changes(connection, attempt).await?; let tracked_root = self.track(connection, &requested_root).await; @@ -407,13 +432,6 @@ impl BootTracker { let unit = tracked .as_ref() .map_or(unit, |tracked| tracked.name().to_owned()); - if self.phase == Phase::Initrd - && matches!(self.root_switch, RootSwitch::NotQueued) - && unit == SWITCH_ROOT_UNIT - && let Ok(sysroot) = Sysroot::hold() - { - self.root_switch = RootSwitch::Queued(sysroot); - } tracked .and_then(TrackedUnit::into_change) .into_iter() diff --git a/src/systemd_link/systemd_bus.rs b/src/systemd_link/systemd_bus.rs index c8fc9c3..d17f17b 100644 --- a/src/systemd_link/systemd_bus.rs +++ b/src/systemd_link/systemd_bus.rs @@ -62,6 +62,11 @@ impl StatusOutput { } } +pub enum SignalBatch { + Open(Vec), + Closed(Vec), +} + pub enum Signal { JobNew { id: u32, @@ -207,17 +212,14 @@ impl Session { poll(&mut watched, Some(&Timespec::default())).is_ok_and(|ready| ready > 0) } - fn read_one_chunk(&self) -> Result<(), Error> { - let still_connected = - self.channel.read_write(Some(Duration::ZERO)).is_ok() && self.channel.is_connected(); - still_connected.then_some(()).ok_or(Error::Disconnected) + fn read_one_chunk(&self) -> bool { + self.channel.read_write(Some(Duration::ZERO)).is_ok() && self.channel.is_connected() } - fn arrived_messages(&self) -> Result, Error> { + fn arrived_messages(&self) -> Vec { iter::repeat_with(|| self.read_one_chunk()) - .find(|outcome| outcome.is_err() || !self.socket_has_unread_bytes()) - .unwrap_or(Ok(()))?; - Ok(iter::from_fn(|| self.channel.pop_message()).collect()) + .find(|still_connected| !still_connected || !self.socket_has_unread_bytes()); + iter::from_fn(|| self.channel.pop_message()).collect() } } @@ -240,15 +242,18 @@ impl BusConnection { } fn drain_replies(&mut self) -> Result, Error> { - let (signals, replies): (Vec, Vec) = self - .session - .get_ref() - .arrived_messages()? + let session = self.session.get_ref(); + let (signals, replies): (Vec, Vec) = session + .arrived_messages() .into_iter() .partition(|message| message.msg_type() == MessageType::Signal); self.early_signals .extend(signals.iter().filter_map(parse_signal)); - Ok(replies) + if session.channel.is_connected() { + Ok(replies) + } else { + Err(Error::Disconnected) + } } async fn next_replies(&mut self) -> Result, Error> { @@ -350,12 +355,17 @@ impl BusConnection { .read1()?) } - pub async fn receive_signals(&mut self, patience: Duration) -> Result, Error> { + pub async fn receive_signals(&mut self, patience: Duration) -> SignalBatch { if self.early_signals.is_empty() { readable_or_timeout(&self.session, patience).await; } - self.drain_replies()?; - Ok(mem::take(&mut self.early_signals)) + let still_connected = self.drain_replies().is_ok(); + let signals = mem::take(&mut self.early_signals); + if still_connected { + SignalBatch::Open(signals) + } else { + SignalBatch::Closed(signals) + } } }