Open Jim-with-a-J opened 1 year ago
Thanks for the report. This is very strange indeed. We use this package for killing processes: https://github.com/jesseduffield/kill
The windows code is fairly convoluted, and it wouldn't surprise me if the bug lived in there somewhere.
The reason it's complicated is because we want to be able to kill child processes of the processes we spawn, and windows does not make this easy.
I don't know golang, but I assume you query all the processes and filter down to those with a PPID matching the target (perhaps through multiple levels to catch grandchildren etc). I see CreateToolhelp32Snapshot and friends mentioned in that code, so that seems the most likely.
I think Windows doesn't maintain integrity of that PPID, it's just a static value set at process creation - so if the original process X that spawned Y exits, and then PIDX gets reused for a git process later, process Y could get killed as a 'child' of the git process. I think this might fit in with the fact that it only seems to happen after a reasonable length of time (long enough for PIDS to start getting recycled).
I can't think of a good way around this, except perhaps adding calls to GetProcessTimes for each target to make sure it has been running for less time than the parent process, making some already complicated code more complicated :(
Would job objects help? https://learn.microsoft.com/en-us/windows/win32/procthread/job-objects
This issue has been causing havoc for me for the past couple of weeks. The idea that lazygit of all things could be responsible never occurred to me. I also found out about it via Process Exit Monitoring.
As @Jim-with-a-J surmised, I believe the issue is that PPid doesn't necessarily point to an existing process. Once a process is created, its PPid is static, and over time will be reused. When lazygit happens to create a process with a PID which is equal to the PPid of an old, long running process, it will assume ownership of this old process and add it to the kill chain.
I've opened a PR in the kill repo (https://github.com/jesseduffield/kill/pull/1) as a proof of concept. I've never used go before so I may be doing various things wrong (and feel free to fix it), but I think this general idea should avoid the problem in all but the most extreme cases.
this happens so frequently that lazygit is unusable on windows, any solution even a hacky one is appreciated
I can also confirm having this problem where others programs get randomly terminated. Finally managed to find the cause using "Process Exit Monitoring" and turns out it was lazygit.
In my case, the main victim is AutoHotkey, but I believe Chrome also got terminated a few times. Frequency seems random: sometimes it goes a week without issues, worst case it happens 3-4 times in an hour.
Describe the bug Processes occasionally exit for no apparent reason while using lazygit. Usually after it has been running in the background for a while. It does not seem predictable or reliably reproducible. After failing to find an explanation for randomly exiting programs, I enabled Process Exit Monitoring and now I have events saying lazygit.exe was the killing process. Microsoft Teams, Outlook and Opera are common victims, but I've seen other programs exit unexpectedly as well. I cannot say for sure lazygit was responsible in all cases though.
An example event message from the exit monitor just says: The process 'C:\Path\To\Teams.exe' was terminated by the process 'C:\Path\To\lazygit.exe' with termination code 1. The creation time for the exiting process was [hex timestamp].
It's not clear how often this happens, I only have three events mentioning lazygit since I enabled the monitor a few weeks ago. I'm sorry this is such a nebulous report, but I wanted to submit it in case people more familiar with the code might have better explanations or suggestions of things to investigate.
To Reproduce Steps to reproduce the behavior:
Expected behavior Non-git processes are never terminated.
Version info: commit=5e388e21c8ca6aa883dbcbe45c47f6fdd5116815, build date=2023-08-07T14:05:48Z, build source=binaryRelease, version=0.40.2, os=windows, arch=amd64, git version=2.41.0.windows.3
git version 2.41.0.windows.3