🐛 Let a decoder's state go off rt's completion thread at a loss

At a device's loss the table walk (fault_walk) upgrades each decoder's
Weak<Shared> on the thread that read the first failed command buffer, rt's
completion thread among them. Where the decoder went during the walk, the
walk's hold was the last one, and the decoder's objects were dropped there: a
drop that waited for a command buffer's completion would hang the thread that
completes it.

TableWalk::each now hands a last hold to a thread of its own,
reims-vgpu.graphics-tables-let-go, through the platform's threads, so a
refused spawn ends the process as theirs do. The test drops the decoder in
the walk's callback and checks that its state went on that thread, and that a
walk of a decoder that outlives it spawns nothing. The host sees only a new
library thread name; nothing the guest sees changes.
This commit is contained in:
Anees Iqbal committed 2026-11-21 19:14:23 -08:00
1 parent 36655c417b
commit d7fec652de
4 files changed
+114 -4

No files matched your search

@@ -865,6 +865,14 @@ pub(crate) type EachTableObject<'a, P> = dyn for<'o> FnMut(u32, u64, bool, Table
/// A decoder's tables, held weakly, for the walk at its device's loss (`super::fault_walk`).
pub(crate) struct TableWalk<P: Platform>(Weak<Shared<P>>);
/// The thread a decoder's state is let go of on where the walk at its device's loss held it last
/// ([`TableWalk::each`]).
pub(crate) const TABLES_LET_GO: &str = "reims-vgpu.graphics-tables-let-go";
/// That thread's stack: a std thread's, as the decoder's workers have, which let go of the same
/// objects.
const TABLES_LET_GO_STACK: usize = 2 << 20;
impl<P: Platform> TableWalk<P> {
/// Whether the decoder's state is still there.
pub(crate) fn is_live(&self) -> bool {
@@ -876,6 +884,13 @@ impl<P: Platform> TableWalk<P> {
/// `bound` at most in all for the locks: the map of contexts, held through the walk, so no
/// context is let go of here, and each table's. Answers the contexts walked, those whose
/// table was not had in time, and whether the map was not.
///
/// The walk holds the decoder's state from its start to its end. Where the decoder went
/// meanwhile, so the walk's hold is the last, the state is let go of on a thread of its own
/// ([`TABLES_LET_GO`]), not on the walk's: that is the thread that reads a lost device's
/// first failed command buffer, rt's completion thread among them, and a drop of the
/// decoder's objects there that waited for a command buffer's completion would hang it. A
/// refused thread ends the process, as the platform's threads do.
pub(crate) fn each(
&self,
bound: std::time::Duration,
@@ -884,6 +899,26 @@ impl<P: Platform> TableWalk<P> {
let Some(shared) = self.0.upgrade() else {
return (0, Vec::new(), false);
};
let walked = Self::each_of(&shared, bound, f);
if let Some(last) = Arc::into_inner(shared) {
let threads = last.platform.threads().handle();
reims_vgpu_vulkan::core::rt::spawn_named(
&*threads,
TABLES_LET_GO,
TABLES_LET_GO_STACK,
Box::new(last),
|| {},
);
}
walked
}
/// [`Self::each`]'s walk of `shared`.
fn each_of(
shared: &Shared<P>,
bound: std::time::Duration,
f: &mut EachTableObject<'_, P>,
) -> (usize, Vec<u32>, bool) {
let clock = shared.platform.clock();
let start = clock.now_mono().as_nanos();
let left = || {
@@ -14347,4 +14382,74 @@ lacks {part}"
(RGFX_PIPELINE_REFUSED, RGFX_PIPELINE_KIND_RENDER)
);
}
/// The walk of a decoder's tables at its device's loss lets go of the decoder's state, where
/// the decoder went during the walk and its hold is the last, on a thread of its own, not on
/// the walk's (rt's completion thread at a loss); where the decoder is still there, the walk
/// lets go of nothing and starts no thread
#[test]
fn a_loss_walk_holding_a_decoder_s_state_last_lets_it_go_off_its_own_thread() {
use std::sync::mpsc;
use std::time::Duration;
/// Sends the name of the thread it is dropped on.
struct Tell(mpsc::Sender<Option<String>>);
impl Drop for Tell {
fn drop(&mut self) {
let _ = self
.0
.send(std::thread::current().name().map(str::to_owned));
}
}
let p = Arc::new(FakePlatform::new());
let device = p.device();
let mut dec = GraphicsDecoder::<FakePlatform>::new(
Arc::clone(&p),
Arc::new(reims_vgpu_graphics::region::Region::new(0x10000).unwrap()),
);
let buffer = RgfxBufferCreate {
header: RgfxMsgHeader {
r#type: RGFX_CMD_BUFFER_CREATE,
size: size_of::<RgfxBufferCreate>() as u32,
seqno: 0,
},
obj_id: 1,
gpu_address: 0x4_0000,
length: 65536,
..RgfxBufferCreate::default()
};
assert_eq!(dec.decode(&device, buffer.as_bytes()).unwrap(), Ok(()));
dec.end_message();
// The state's drop drops the hook, which tells the thread it was dropped on.
let (tx, rx) = mpsc::channel();
let tell = Tell(tx);
dec.set_walk_hook(Some(Arc::new(move |_: usize| {
let _ = &tell;
})));
let walk = dec.table_walk();
let spawned = |p: &FakePlatform| {
(p.fake_threads().spawned().into_iter())
.filter(|&(name, _)| name == TABLES_LET_GO)
.count()
};
// The decoder outlives the walk: nothing is let go of, and no thread started.
let mut seen = 0;
let walked = walk.each(Duration::from_secs(5), &mut |_, _, _, _| seen += 1);
assert_eq!((walked, seen), ((1, Vec::new(), false), 1));
assert_eq!(rx.try_recv(), Err(mpsc::TryRecvError::Empty));
assert_eq!(spawned(&p), 0);
// The decoder goes during the walk: the walk's hold is the last, and the state goes on
// the let-go thread once the walk is done.
let mut held = Some(dec);
let walked = walk.each(Duration::from_secs(5), &mut |_, _, _, _| drop(held.take()));
assert_eq!(walked, (1, Vec::new(), false));
assert!(held.is_none());
let on = rx.recv_timeout(Duration::from_secs(10));
assert_eq!(on, Ok(Some(TABLES_LET_GO.to_owned())));
assert_eq!(spawned(&p), 1);
assert!(!walk.is_live());
}
}
+4 -2
View File
@@ -2,8 +2,10 @@
//! at its loss to place the driver's fault addresses among their live objects
//! (`reims_vgpu_vulkan::rt_platform::fault`'s docs). The walk keeps no record before the loss: it
//! reads the tables as they are then, waiting `fault::BOUND` in all at most for their locks, and
//! names the contexts whose tables it did not have in time. A device of another platform is told
//! nothing.
//! names the contexts whose tables it did not have in time. Where the decoder goes during the
//! walk, so the walk holds its state last, the state is let go of on a thread of its own, not on
//! the walk's, which may be rt's completion thread (`decode::TableWalk::each`). A device of
//! another platform is told nothing.
//!
//! As a table stores a buffer, texture or heap, the object is told the guest's id for it
//! ([`name`]), which the device's history of released ranges and its lines of the command buffers
+2 -1
View File
@@ -397,7 +397,8 @@ returns. The library logs through the `log` crate; a Rust host installs its own
Threads are named `reims-vgpu.<role>`: those above, `reims-vgpu.display` (the display thread),
`reims-vgpu.window` (a window's drawing thread), `reims-vgpu.ui` (on Linux and Windows, the thread
every window's events and callbacks run on), and `reims-vgpu.graphics-video-<i>`,
`reims-vgpu.graphics-let-go-<i>` and `reims-vgpu.graphics-event-waits` (a graphics device's). The names are diagnostics, not contract.
`reims-vgpu.graphics-let-go-<i>`, `reims-vgpu.graphics-event-waits` and
`reims-vgpu.graphics-tables-let-go` (a graphics device's). The names are diagnostics, not contract.
A queue's worker, the display thread among them, has core's `WORKER_STACK`, 512 KiB.
### The locks around `raise_interrupt`
+3 -1
View File
@@ -496,7 +496,9 @@ so a block that takes a recursive lock its caller holds would re-enter it.
process's releases (`reims-vgpu.graphics-closes-<i>`), staged copies of surfaces' caches into
their pages (`reims-vgpu.graphics-into-pages-<i>`), the video caches' spare surfaces made
ahead on Windows (`reims-vgpu.graphics-drawn-spares-<i>`), video
(`reims-vgpu.graphics-video-<i>`) and event waits (`reims-vgpu.graphics-event-waits`). How
(`reims-vgpu.graphics-video-<i>`) and event waits (`reims-vgpu.graphics-event-waits`), and
the thread a decoder's state is let go of on where the walk of its tables at the device's loss
held it last (`reims-vgpu.graphics-tables-let-go`, `graphics::decode`'s `TableWalk::each`). How
long a pool keeps an idle thread is `reims-vgpu-graphics`'s `keep`. The guest's Shared pool
has two threads of its own (15.1): its taker (`reims-vgpu.graphics-pool-ahead`), which takes
the blocks of its own asks for more pool, and its thread (`reims-vgpu.graphics-pool`), which