Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
WindowsError [Error 5] Access Denied when trying to terminate a process #1023
I'm getting a
Minimum example follows.
What I've run:
# Access denied when trying to call terminate import gevent.monkey gevent.monkey.patch_subprocess() import subprocess as sp proc = sp.Popen('dir', shell=True, stdout=sp.PIPE, stderr=sp.PIPE) proc.terminate()
Here is the error I'm getting:
This error does not occurr without applying the patch.
# no errors without monkey patching import subprocess as sp proc = sp.Popen('dir', shell=True, stdout=sp.PIPE, stderr=sp.PIPE) proc.terminate()
I noticed that
# gevent/subprocess.py Popen.terminate def terminate(self): """Terminates the process """ TerminateProcess(self._handle, 1)
# subprocess.py Popen.terminate def terminate(self): """Terminates the process """ try: _subprocess.TerminateProcess(self._handle, 1) except OSError as e: # ERROR_ACCESS_DENIED (winerror 5) is received when the # process already died. if e.winerror != 5: raise rc = _subprocess.GetExitCodeProcess(self._handle) if rc == _subprocess.STILL_ACTIVE: raise self.returncode = rc
Thank you for your report and analysis!
It looks like the stdlib code you reference was added in Python 3.1 and then backported to Python 2.7.4+ (the gevent subprocess module predates that). It added a suite of tests, but gevent doesn't run them (and it's not obvious to me why it doesn't, despite the comment that I wrote two years ago---it may have to do with the call to
On POSIX, at least, we know preemptively when the process is dead and don't bother trying to signal it, but we don't have child watchers on windows that would always tell us that. We could add a similar check there, but it would be an optimization: we'd still need the try/except logic.