Skip to content

Fix the subprocess leak issue in the nox/popen.py file - #1165

Closed
wu-zhao-min wants to merge 1 commit into
wntrblm:mainfrom
wu-zhao-min:main
Closed

Fix the subprocess leak issue in the nox/popen.py file#1165
wu-zhao-min wants to merge 1 commit into
wntrblm:mainfrom
wu-zhao-min:main

Conversation

@wu-zhao-min

Copy link
Copy Markdown
Contributor

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.

@wu-zhao-min

Copy link
Copy Markdown
Contributor Author

closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant