Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): fix leakage of SessionMetaData #728

Merged
merged 1 commit into from
Sep 18, 2021

Conversation

tw4452852
Copy link
Contributor

Currently, when the server exits, Session data is leaked because 'server_listener' thread still holds a reference to it.
Due to this thread blocking on the socket accepting, to fix this problem, we change it to polling mode (the timer interval is 10 milliseconds) and add a flag to indicate exit.

Signed-off-by: Tw tw19881113@gmail.com

@imsnif imsnif requested a review from kunalmohan September 16, 2021 13:58
@kunalmohan
Copy link
Member

@tw4452852 Thanks for catching this! I'm a bit curious about how big of an issue this is and whether there is an alternate approach to solve this. This only happens when zellij is exiting and IIUC the OS releases any memory allocated to a process after it exits.

The reason I'm concerned is that the new change adds polling to the thread so the thread will wake up every 10ms. We had a similar polling approach in some other section of zellij in some of the earlier releases due to which users complained of higher CPU and power consumption (Linked Issue: #509).

@tw4452852
Copy link
Contributor Author

tw4452852 commented Sep 17, 2021

Hi @kunalmohan

Indeed, any allocated memory will be released by the OS when the process exits. But as I did some temporary files cleanup in PR #723 when wasm thread exits. And this depends on the notification sent when SessionMetaData drops:

let _ = self.senders.send_to_plugin(PluginInstruction::Exit);

Also if we add more actions when threads exit in the future, this really needs to be fixed I think.

Regarding the CPU usage when polling, it's almost ignorable compared to the blocking mode. As I use this during my daily work.

@imsnif
Copy link
Member

imsnif commented Sep 17, 2021

I hope it's okay if I chime in here: if this is in order to facilitate something else that hasn't been merged yet, maybe we can look into that first and then see if we still need this one? I guess @TheLostLambda will probably have some useful input regarding cached plugin disk metadata.

@kunalmohan
Copy link
Member

FWIW we drop the SessionMetaData object on exit here:

*session_data.write().unwrap() = None;

But there are other cases where we don't drop it on exit (this and this). So maybe we can move the above statement outside the loop, before:
thread_handles
.lock()
.unwrap()
.drain(..)
.for_each(|h| drop(h.join()));

@tw4452852
Copy link
Contributor Author

Ah, I see!

@tw4452852
Copy link
Contributor Author

tw4452852 commented Sep 17, 2021

@kunalmohan
BTW, after re-reading the code, I think the second clone is unnecessary:

let session_data = session_data.clone();
let session_state = session_state.clone();
let to_server = to_server.clone();
thread_handles.lock().unwrap().push(
thread::Builder::new()
.name("server_router".to_string())
.spawn({
let session_data = session_data.clone();
let os_input = os_input.clone();
let to_server = to_server.clone();
move || {
route_thread_main(
session_data,
session_state,
os_input,
to_server,
)
}
})
.unwrap(),

Correct me if I'm wrong.

@kunalmohan
Copy link
Member

@tw4452852 yes, you are right!

There are different reasons leading the server thread exits,
currently we only release the cached session data when client
exits normally. This fix covers all the cases.

Signed-off-by: Tw <tw19881113@gmail.com>
@tw4452852
Copy link
Contributor Author

Thanks @kunalmohan

I have updated the patch according to your suggestions.

Copy link
Member

@kunalmohan kunalmohan left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks!!

@kunalmohan kunalmohan merged commit ac7bcf1 into zellij-org:main Sep 18, 2021
@tw4452852 tw4452852 deleted the server_exit branch September 19, 2021 01:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants