Skip to content

Fix circular reference issue between EditPredictionButton and PopoverMenuHandle#42351

Merged
SomeoneToIgnore merged 2 commits intozed-industries:mainfrom
feeiyu:fix_leak
Nov 10, 2025
Merged

Fix circular reference issue between EditPredictionButton and PopoverMenuHandle#42351
SomeoneToIgnore merged 2 commits intozed-industries:mainfrom
feeiyu:fix_leak

Conversation

@feeiyu
Copy link
Contributor

@feeiyu feeiyu commented Nov 10, 2025

Closes #ISSUE

While working on issue #40906, I discovered that RemoteClient was not being released after the remote project closed.
Analysis revealed a circular reference between EditPredictionButton and PopoverMenuHandle.

Dependency Chain: RemoteClient → Project → ZetaEditPredictionProvider → EditPredictionButton ↔ PopoverMenuHandle

image

a) EditPredictionButton hold the reference of PopoverMenuHandle

zed/crates/zed/src/zed.rs

Lines 386 to 394 in 5f82264

let edit_prediction_button = cx.new(|cx| {
edit_prediction_button::EditPredictionButton::new(
app_state.fs.clone(),
app_state.user_store.clone(),
edit_prediction_menu_handle.clone(),
app_state.client.clone(),
cx,
)
});

b) PopoverMenuHandle hold the reference of Fn which capture Entity<EditPredictionButton>

let this = cx.entity();
let mut popover_menu = PopoverMenu::new("zeta")
.menu(move |window, cx| {
Some(this.update(cx, |this, cx| this.build_zeta_context_menu(window, cx)))
})
.anchor(Corner::BottomRight)
.with_handle(self.popover_menu_handle.clone());

if let Some(trigger_handle) = self.trigger_handle.take()
&& let Some(menu_builder) = self.menu_builder.clone()
{
*trigger_handle.0.borrow_mut() = Some(PopoverMenuHandleState {
menu_builder,
menu: element_state.menu.clone(),
on_open: self.on_open.clone(),
});
}

Release Notes:

  • N/A

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Nov 10, 2025
Copy link
Contributor

@SomeoneToIgnore SomeoneToIgnore left a comment

Choose a reason for hiding this comment

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

Superb, thank you so much for all the context and a fix.

I've found a fixed a few of these myself, so I suspect this might not be the last one around.
Not yet sure what to do about a systematic way to fix this, but any other similar fixes are very welcome.

@SomeoneToIgnore SomeoneToIgnore self-assigned this Nov 10, 2025
@SomeoneToIgnore SomeoneToIgnore merged commit 42ed032 into zed-industries:main Nov 10, 2025
25 checks passed
@feeiyu
Copy link
Contributor Author

feeiyu commented Nov 10, 2025

Perhaps we could check if key objects, such as Project/Workspace, are properly released after the project closure?

@SomeoneToIgnore
Copy link
Contributor

after the project closure

Workspace isn't necessary going to be released, as technically, workspace == window and can be reused.
But overall, the "after the windows closure" idea seems viable to me.

SomeoneToIgnore pushed a commit that referenced this pull request Nov 11, 2025
11happy pushed a commit to 11happy/zed that referenced this pull request Dec 1, 2025
…MenuHandle (zed-industries#42351)

Closes #ISSUE

While working on issue zed-industries#40906, I discovered that RemoteClient was not
being released after the remote project closed.
Analysis revealed a circular reference between EditPredictionButton and
PopoverMenuHandle.

Dependency Chain: RemoteClient → Project → ZetaEditPredictionProvider →
EditPredictionButton ↔ PopoverMenuHandle

<img width="400" height="300" alt="image"
src="https://github.com/user-attachments/assets/6b716c9b-6938-471a-b044-397314b729d4"
/>

a) EditPredictionButton hold the reference of PopoverMenuHandle 

https://github.com/zed-industries/zed/blob/5f8226457ee6e1346a224ae6b0329f014ea883f7/crates/zed/src/zed.rs#L386-L394

b) PopoverMenuHandle hold the reference of Fn which capture
`Entity<EditPredictionButton>`

https://github.com/zed-industries/zed/blob/5fc54986c72f2863645302c5e6a99277f8c38cab/crates/edit_prediction_button/src/edit_prediction_button.rs#L382-L389


https://github.com/zed-industries/zed/blob/a9bc890497f1edaf4f177385cf96785de60e910c/crates/ui/src/components/popover_menu.rs#L376-L384


Release Notes:

- N/A
11happy pushed a commit to 11happy/zed that referenced this pull request Dec 1, 2025
SomeoneToIgnore pushed a commit that referenced this pull request Dec 3, 2025
CherryWorm pushed a commit to CherryWorm/zed that referenced this pull request Dec 16, 2025
someone13574 pushed a commit to someone13574/zed that referenced this pull request Dec 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants