-
-
Notifications
You must be signed in to change notification settings - Fork 911
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
Library leaves dangling processes after use #1333
Comments
Thanks for all the research done to investigate, here are my thoughts.
Indeed, this comes from a time long gone where python would use ref counting and the deterministic destruction of objects that comes with it. Since the adoption of GC this behaviour is no more and GitPython is somewhat broken due to it. On unix system it's not noticeable, on Windows however, it is. Generally, GitPython in long-running processes should be avoided, and if it really has to run as part of the long-running processes these should probably be well isolated.
I welcome any fix and would hope it can be done in a non-breaking, additive fashion. For example, those who write new code would use the context managers and get perfect resource cleanup, but existing code may keep working as well with the current behaviour.
If that were the case it would probably show in all kinds of places. But assuming it's true, it should be possible to remove the persistent process (or offer a flag not to use it) and solve this issue that way. This means avoiding such a persistent process could be another avenue for a fix.
If better control over resources is a requirement, GitPython might not be adequate for the job, and if I had to use python and wanted to use git, I'd probably settle for |
AFAIK, I tested out
|
Hi, I've faced a problem on Windows with file/directory removal with
shutil.rmtree()
. The problem, by itself, is not new and already has known solutions, includinggit.util.rmtree()
. However, just using this function was not enough in my case and I started to dig deeper - there was an error about an open file handle. I discovered there were extra childgit
processes hanging in the process tree, but, unexpectedly, it was happening after all the library objects were removed:Then I tried to catch and trace
Popen
calls from the library and found out this function:GitPython/git/cmd.py
Lines 1190 to 1199 in 5b3669e
From comments and names, it looks like such persistent behavior is intended. There is also the
AutoInterrupt
class here:GitPython/git/cmd.py
Lines 367 to 373 in 5b3669e
returned from:
GitPython/git/cmd.py
Line 841 in 5b3669e
The class comment indicates that the process should be killed when the object goes out of scope. To me, it looks like an attempt to imitate the RAII C++ idiom. Unfortunately, in Python it does not work this way because of garbage collector and the scoping rules. That is, an object that has no references can be removed in any time after it lost its last reference. The "pythonic" way to deal with this behavior is using a context manager (which this class is not).
My suggestion is to collect these processes and manage them on a library/repo level, or wrap this in a context manager (which is better than just calling
wait()
manually in case of exceptions). BTW, probably, the process created is not reused after the first call.Upd: I see there is an undocumented
repo.close()
, which can help, but can have undesired side-effects because of a forced gc call.The text was updated successfully, but these errors were encountered: