Title: Remote.fetch(kill_after_timeout=...) does not stop git when it writes nothing more to stderr · Issue #2276 · gitpython-developers/GitPython · GitHub
Open Graph Title: Remote.fetch(kill_after_timeout=...) does not stop git when it writes nothing more to stderr · Issue #2276 · gitpython-developers/GitPython
X Title: Remote.fetch(kill_after_timeout=...) does not stop git when it writes nothing more to stderr · Issue #2276 · gitpython-developers/GitPython
Description: Question The documentation for Remote.fetch describes kill_after_timeout as "a timeout in seconds for the git command, after which the process should be killed". When git writes nothing more to stderr after the deadline, which is what ha...
Open Graph Description: Question The documentation for Remote.fetch describes kill_after_timeout as "a timeout in seconds for the git command, after which the process should be killed". When git writes nothing more to std...
X Description: Question The documentation for Remote.fetch describes kill_after_timeout as "a timeout in seconds for the git command, after which the process should be killed". When git writes nothing m...
Opengraph URL: https://github.com/gitpython-developers/GitPython/issues/2276
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Remote.fetch(kill_after_timeout=...) does not stop git when it writes nothing more to stderr","articleBody":"### Question\n\nThe documentation for `Remote.fetch` describes `kill_after_timeout` as \"a timeout in seconds for the git command, after which the process should be killed\". When git writes nothing more to stderr after the deadline, which is what happens when a remote stalls, the call never returns and the `git fetch` process keeps running. Is this a known limitation, or a bug?\n\n### Context\n\nWe use `kill_after_timeout` to put an upper bound on how long a fetch can run.\n\n### Reproduction\n\n```python\nimport socket\nimport tempfile\nimport threading\nimport time\nfrom pathlib import Path\n\nfrom git import Repo\nfrom git.exc import GitCommandError\n\n# A remote that accepts the connection and never answers.\nserver = socket.socket()\nserver.bind((\"127.0.0.1\", 0))\nserver.listen()\nport = server.getsockname()[1]\nheld = []\nthreading.Thread(target=lambda: [held.append(server.accept()[0]) for _ in iter(int, 1)], daemon=True).start()\n\nrepo = Repo.init(Path(tempfile.mkdtemp()))\nrepo.create_remote(\"origin\", f\"git://127.0.0.1:{port}/stalled.git\")\n\nstarted = time.monotonic()\ntry:\n repo.remotes.origin.fetch(kill_after_timeout=3)\n print(\"returned\")\nexcept GitCommandError as exc:\n print(f\"GitCommandError after {time.monotonic() - started:.1f}s: {exc}\")\n```\n\n**Expected:** a `GitCommandError` after about 3 seconds.\n\n**Actual:** the call never returns, and `git fetch -v -- origin` keeps running until it is killed by hand.\n\n**Reproduced with:**\n- GitPython 3.2.0, git 2.47.3, Python 3.13, Linux (the `python:3.13` Docker image)\n- GitPython 3.1.61 and 3.2.0, git 2.52.0, Python 3.14.7, macOS\n\n### What I think happens\n\n`Remote.fetch` does not pass `kill_after_timeout` to the git command. It passes it to `_get_fetch_info_from_stderr`, and from there to `handle_process_output`. When the stderr pump thread is still alive after the timeout, `handle_process_output` calls `AutoInterrupt._terminate()`. That method closes `stdin`, `stdout` and `stderr` *before* it calls `proc.terminate()`.\n\nThe pump thread is blocked in `read()` on that same stderr stream, so it holds the buffered reader's lock, and `stderr.close()` waits for that lock indefinitely. The process is never signalled. A native stack sample of the stuck process (on macOS) shows exactly that:\n\n- **main thread:** `_io_TextIOWrapper_close` → `_io__Buffered_close` → `_enter_buffered_busy` → `PyThread_acquire_lock_timed`\n- **pump thread:** `_textiowrapper_readline` → `_io__Buffered_read1` → `read`\n\nSo the timeout takes effect only if git writes to stderr after the deadline, which releases the pump thread's read.\n\nThe command-level timeout works for the same remote: `repo.git.fetch(\"origin\", kill_after_timeout=3)` goes through `Git.execute`, whose watchdog sends `SIGKILL`. It raised `GitCommandError` with status -9 after 3.0 seconds, on both platforms.\n\n### A possible fix\n\nSignal the process before closing its streams, either by reordering `_terminate()` or by killing the process in `handle_process_output` before calling `_terminate()`. Once git exits, the write end of the pipe closes, `read()` returns EOF, and the close no longer blocks.\n\nOne caveat, which I have not tested: for `https://` and `ssh` remotes, `git fetch` starts a helper (`git-remote-https` or `ssh`) that inherits its stderr. If only `git fetch` is signalled, a helper stuck on the network may keep the pipe open, and the read may still block. A complete fix might need to signal the children too, as the `Git.execute` watchdog does, or the whole process group. \n\nTwo smaller points, in case they are useful:\n\n- The loop joins each pump thread one after another with the full timeout, so the worst-case wait is the timeout multiplied by the number of pumped streams.\n- `_terminate()` sends `SIGTERM`, while the `Git.execute` watchdog sends `SIGKILL`.\n\n`Remote.pull` and `Remote.push` appear to use the same path, but I have only tested `fetch`.","author":{"url":"https://github.com/ogenstad","@type":"Person","name":"ogenstad"},"datePublished":"2026-10-07T07:07:33Z","url":"https://github.com/gitpython-developers/GitPython/issues/2276"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:6f7f6b14-c742-e8f3-52b8-41638ab28733 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A13E:395430:40F095:57D23C:6B00C92E |
| html-safe-nonce | 481adfcc9ccc0b83a51647d79881d5c8c5e558aede073e28a8bd55e232032b05 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMTNFOjM5NTQzMDo0MEYwOTU6NTdEMjNDOjZCMDBDOTJFIiwidmlzaXRvcl9pZCI6IjkxMzY3MTU2ODc1NjQyMDAyMzgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | baa9534cfda52aea455d35b535f1bd81e78ccdaf7a8de2d785588b588ef3dd50 |
| hovercard-subject-tag | issue:5740900100 |
| 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/2276/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a3112d0f8d5abfc6b3e2bd0b3b21526697d9aa03365ca051876f9815bd12fcc4/gitpython-developers/GitPython/issues/2276 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a3112d0f8d5abfc6b3e2bd0b3b21526697d9aa03365ca051876f9815bd12fcc4/gitpython-developers/GitPython/issues/2276 |
| og:image:alt | Question The documentation for Remote.fetch describes kill_after_timeout as "a timeout in seconds for the git command, after which the process should be killed". When git writes nothing more to std... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ogenstad |
| hostname | github.com |
| expected-hostname | github.com |
| None | 98dc00e171c204462a6f367482a0f8c3c6b5dbca99cd7390e82d2df49ce096e4 |
| 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 | ef013399cf0a3c324bea644a685d7c1e0f06b5b7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width