Avoid allocating in pre_exec closure#104
Conversation
Summary of ChangesHello @purplesyringa, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refactors error handling within a Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly addresses a potential deadlock issue in multi-threaded programs by removing memory allocation from the pre_exec closure. The use of ? to leverage nix's non-allocating From<Errno> for std::io::Error conversion is the right approach here. This change improves the robustness and safety of the code, following best practices for writing pre_exec closures. As you noted in the description, this also has the benefit of preserving the original errno, which is great for debugging. The change is well-explained and correctly implemented. Great job!
The documentation for
pre_execspecifies that allocating in thepre_execclosure is not guaranteed to work.Specifically, this is bad in multi-threaded programs: if the fork occurs while the allocator lock is held by another thread, deadlocks can occur, since there's no one left in the new process to unlock the mutex. I do not believe this is UB, and modern libc offer protections against this issue, but this isn't POSIX-compliant and should preferably be avoided.
zaundoesn't seem to be multi-threaded at a first glance, but I'm filing this anyway as part of going through the list at rust-lang/rust#148971. If it's single-threaded, you don't need to merge this, but I'd play safe.nixprovides a non-allocatingimpl From<Errno> for std::io::Error, which can be used instead. This doesn't allow an additional message to be added, but since error messages aren't transmitted acrosspre_execboundary anyway, this doesn't make visible behavior any worse. On the flip side, this ensures that the correct error code is forwarded to the parent process, instead of the default-EINVAL.