-
Notifications
You must be signed in to change notification settings - Fork 1.5k
cleanup: refactor svccommio class #13843
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR refactors the SvcCommIo helper class to use RAII patterns, significantly simplifying the code and improving maintainability. The changes replace a monolithic implementation with well-structured RAII wrapper classes (ConsoleInput and ConsoleOutput) that automatically manage console state restoration.
Key changes:
- Introduced ConsoleInput and ConsoleOutput RAII wrapper classes with factory methods
- Eliminated manual cleanup code by leveraging RAII destructors
- Reduced code length and complexity through better separation of concerns
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/windows/common/svccommio.hpp | Adds ConsoleInput and ConsoleOutput RAII wrapper class declarations; updates SvcCommIo to use these wrappers instead of manual state tracking |
| src/windows/common/svccommio.cpp | Implements RAII wrappers with factory methods; refactors SvcCommIo constructor to use the new wrappers; removes manual cleanup code replaced by RAII destructors |
| { | ||
| _stdHandles.StdOut.Handle = LXSS_HANDLE_USE_CONSOLE; | ||
| _stdHandles.StdOut.HandleType = LxssHandleConsole; | ||
| return std::unique_ptr<ConsoleOutput>(new ConsoleOutput(std::move(ConsoleHandle), Mode)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I'd have a preference towards either having ConsoleOutput be default constructible / movable, or using an std::optional in SvcCommIo so we can avoid having to allocate a std::unique_ptr here
| auto RestoreOutput = wil::scope_exit([&] { | ||
| if (RestoreMode) | ||
| const auto Error = GetLastError(); | ||
| if (Error != ERROR_INVALID_PARAMETER && Error != ERROR_PIPE_NOT_CONNECTED) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like currently throw WSL_E_CONSOLE if we hit this, what's the reason for the behavior change for that error code ?
jobayer110
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
H
This helper class has always been a bit of a mess, this change reimplements the class using RAII classes and reduces complexity and code length.