Title: Library leaves dangling processes after use · Issue #1333 · gitpython-developers/GitPython · GitHub
Open Graph Title: Library leaves dangling processes after use · Issue #1333 · gitpython-developers/GitPython
X Title: Library leaves dangling processes after use · Issue #1333 · gitpython-developers/GitPython
Description: 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, including git.util.rmtree(). However, just using this function was not enough in my...
Open Graph Description: 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, including git.util.rmtree(). However, just ...
X Description: 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, including git.util.rmtree(). However, j...
Opengraph URL: https://github.com/gitpython-developers/GitPython/issues/1333
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Library leaves dangling processes after use","articleBody":"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, including `git.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 child `git` processes hanging in the process tree, but, unexpectedly, it was happening after all the library objects were removed:\r\n\r\n\r\nThen I tried to catch and trace `Popen` calls from the library and found out this function:\r\nhttps://github.com/gitpython-developers/GitPython/blob/5b3669e24a8ce7f3f482de86fcf95620db643467/git/cmd.py#L1190-L1199\r\n\r\nFrom comments and names, it looks like such persistent behavior is intended. There is also the `AutoInterrupt` class here:\r\nhttps://github.com/gitpython-developers/GitPython/blob/5b3669e24a8ce7f3f482de86fcf95620db643467/git/cmd.py#L367-L373\r\n\r\nreturned from:\r\nhttps://github.com/gitpython-developers/GitPython/blob/5b3669e24a8ce7f3f482de86fcf95620db643467/git/cmd.py#L841\r\n\r\nThe 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](https://en.cppreference.com/w/cpp/language/raii). 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*). \r\n\r\nMy 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.\r\n\r\n``` python\r\nimport git\r\nr = git.Repo.init('test_repo') \r\nr.index.commit('aa') \r\ndel r\r\n```\r\nUpd: I see there is an undocumented `repo.close()`, which can help, but can have undesired side-effects because of a forced gc call.","author":{"url":"https://github.com/zhiltsov-max","@type":"Person","name":"zhiltsov-max"},"datePublished":"2021-09-02T12:08:23.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/1333/GitPython/issues/1333"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:57527582-4a52-bc70-8486-5a52108089a1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BC8C:254E6C:25F79E0:310738B:696C9AA1 |
| html-safe-nonce | 38cf38c4edde0d2bf6116224db1fd0c1b149a3f7a2e7aa0d265a77a7fd9ab333 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQzhDOjI1NEU2QzoyNUY3OUUwOjMxMDczOEI6Njk2QzlBQTEiLCJ2aXNpdG9yX2lkIjoiODI3NDgzMzkwMTQ4NTg1NzQ0MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 946a0cb94b8de809190791e27bcabfcc114962c85548548ca3cd2c882bde2970 |
| hovercard-subject-tag | issue:986628313 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/gitpython-developers/GitPython/1333/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e47c5cdb29421269d20ddc836112f024670b4f31b8c246cd76a5d28aeb489060/gitpython-developers/GitPython/issues/1333 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e47c5cdb29421269d20ddc836112f024670b4f31b8c246cd76a5d28aeb489060/gitpython-developers/GitPython/issues/1333 |
| og:image:alt | 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, including git.util.rmtree(). However, just ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | zhiltsov-max |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d |
| turbo-cache-control | no-preview |
| go-import | github.com/gitpython-developers/GitPython git https://github.com/gitpython-developers/GitPython.git |
| octolytics-dimension-user_id | 503709 |
| octolytics-dimension-user_login | gitpython-developers |
| octolytics-dimension-repository_id | 1126087 |
| octolytics-dimension-repository_nwo | gitpython-developers/GitPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1126087 |
| octolytics-dimension-repository_network_root_nwo | gitpython-developers/GitPython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 38f4d3209478aaeff3ef254f1919defede86af41 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width