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 how closure is transferred to the clone call. #173

Merged
merged 5 commits into from
Jul 31, 2021

Conversation

yihuaf
Copy link
Collaborator

@yihuaf yihuaf commented Jul 30, 2021

The main objective of this PR is to fix how the closure is passed into the clone call. Because the new process created by libc::clone will execute the closure, and because of how rust manages the lifetime of memory, special care is needed to make sure the ownership of the memory associated with the closure is correctly transferred to the new process. Otherwise, the new process will only get a reference of the memory and LLVM can decide to free the memory right after the clone call returns in the parent process.

nix-rust/nix#919 is an explanation of why nix::sched::clone wrapper has the exact same issue and is not implemented correctly. While there is a fix, nix-rust/nix#920, but it seems to be abandoned since 2018. I took the hint from that PR as the basis of this PR.

Note: this patch is a bit complicated than I originally intended. So please double check if the comments around how the closure is passed to clone in fork.rs are easy to understand. This is a case where Rust makes this more painful but also illustrates that rust does force you to reason through how your code allocates memory correctly.

A follow up to fix #163

Copy link
Member

@utam0k utam0k left a comment

Choose a reason for hiding this comment

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

I commented on this, and I agree with you overall. I'll close the PR on my end.

@yihuaf yihuaf requested a review from utam0k July 30, 2021 17:38
Copy link
Member

@utam0k utam0k left a comment

Choose a reason for hiding this comment

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

awesome!

@utam0k utam0k merged commit 307e44a into youki-dev:main Jul 31, 2021
@yihuaf yihuaf deleted the yihuaf/163 branch July 31, 2021 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants