Fix the subprocess leak issue in the nox/popen.py file - #1165
Closed
wu-zhao-min wants to merge 1 commit into
Closed
Conversation
Contributor
Author
|
closed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem description : The popen() function in nox/popen.py creates a child process with subprocess.Popen() and calls proc.communicate() inside a try block whose only except clause catches KeyboardInterrupt . On any other exception path (e.g. OSError from a broken pipe, subprocess.SubprocessError , or a disk-full error while writing to a redirected stdout file), the exception propagates without terminating or waiting on the child. Neither proc.terminate() / proc.kill() nor proc.wait() is invoked, and the Popen object's stdin / stdout / stderr pipe file descriptors are released only by non-deterministic garbage collection rather than deterministically.
Hazard statement : During stress testing, every session.run() invocation that hits a transient I/O error during communicate() leaks one orphaned child process (which keeps running and consuming memory/CPU) together with its pipe file descriptors. Long-term stress testing accumulates orphan processes and exhausts file descriptors, eventually causing "too many open files" errors or system resource exhaustion.
Repair solution : Wrap the subprocess.Popen() call in a with statement so that Popen.exit waits for the process and closes its pipes, and add a finally block that calls proc.kill() whenever proc.poll() is None , ensuring the child is terminated and reaped on every exit path—including non- KeyboardInterrupt exceptions.