fix(tabs): close by stable document id instead of vector index

Bug:
Message::TabClose carried a vector index captured when the view message
was built. Closing several tabs in one batch (Close All / Close Others
with unsaved-changes dialogs, or a queue of continuation updates) let an
earlier close shift the vector before a later TabClose(idx) was handled:
self.tabs.remove(idx) either panicked (index past the end) or closed a
different tab than the one the user confirmed.

Reproduce:
1. Open several drawings and modify each so they all prompt on close.
2. Use Close All, or close two tabs in quick succession so both messages
   are handled in one batch.
3. Before: panic in remove(idx), or the wrong tab closes while the tab
   the user chose stays open (or its work is lost).
4. After: the chosen tab closes; an id already closed by an earlier
   message is a no-op.

Fix:
TabClose now carries the tab's stable u64 id, resolved to a vector
position when the message is handled, not when it is built;
PendingClose::Tab stores ids the same way. All construction sites pass
tab.id (a raw usize index no longer type-checks), and
DocTabCloseAll/DocTabCloseOthers build their queues from live ids at
handling time. A missing id means the tab is already gone, so the
handler returns Task::none().
This commit is contained in:
Karim Jerbi committed 2026-09-30 19:15:49 +01:00
1 parent 9c4aa5f896
commit 2d6f4fcfdf
10 files changed
+178 -25

No files matched your search

+12 -2
View File
@@ -1954,13 +1954,23 @@ impl OpenCADStudio {
Task::none()
}
Message::TabClose(idx) => {
Message::TabClose(tab_id) => {
self.hovered_doc_tab = None;
self.on_tab_close(idx)
// Resolve the stable id when the message is handled, not when
// it was built: an earlier close in the same batch may have
// shifted or removed the tab a captured index referred to.
match self.tabs.iter().position(|tab| tab.id == tab_id) {
Some(idx) => self.on_tab_close(idx),
// Already closed by an earlier message — nothing to do.
None => Task::none(),
}
}
Message::DocTabSaveAll => self.dispatch_command("SAVEALL"),
// Both close-* variants resolve ids against the live tab list at
// handling time (like `TabClose` above), so a batch that closed
// tabs earlier cannot leave them stale.
Message::DocTabCloseAll => {
let ids = self
.tabs