Fix panic in remove_item (#21480)

In #20742 we added a call to remove_item that retain an item index over
an
await point. This led to a race condition that could panic if another
tab was
removed during that time. (cc @mgsloan)

This changes the API to make it harder to misuse.

Release Notes:

- Fixed a panic when closing tabs containing new unsaved files
This commit is contained in:
Conrad Irwin 2024-12-03 12:09:53 -08:00 committed by GitHub
parent 492ca219d3
commit b28287ce91
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 20 deletions

View file

@ -3723,7 +3723,7 @@ impl Workspace {
let mut new_item = task.await?;
pane.update(cx, |pane, cx| {
let mut item_ix_to_remove = None;
let mut item_to_remove = None;
for (ix, item) in pane.items().enumerate() {
if let Some(item) = item.to_followable_item_handle(cx) {
match new_item.dedup(item.as_ref(), cx) {
@ -3733,7 +3733,7 @@ impl Workspace {
break;
}
Some(item::Dedup::ReplaceExisting) => {
item_ix_to_remove = Some(ix);
item_to_remove = Some((ix, item.item_id()));
break;
}
None => {}
@ -3741,8 +3741,8 @@ impl Workspace {
}
}
if let Some(ix) = item_ix_to_remove {
pane.remove_item(ix, false, false, cx);
if let Some((ix, id)) = item_to_remove {
pane.remove_item(id, false, false, cx);
pane.add_item(new_item.boxed_clone(), false, false, Some(ix), cx);
}
})?;