mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-08-06 22:33:07 +02:00
Terminate git processes more gracefully to avoid the stale index.lock problem (#4782)
- **PR Description** Instead of killing git processes when we no longer need them, close their ptys or output pipes; this makes them terminate more gracefully, and it avoids the problem of left-over index.lock files that the next git command chokes on. To fix the index.lock problem, only the first two commits of the branch are needed. I decided to go a bit further and add more commits to make similar changes to other places in the code; these might be considered more risky than necessary, and we might decide to drop or revert them if they cause problems. But it does allow us to remove the kill package dependency altogether. Fixes #2050.
This commit is contained in:
1
go.mod
1
go.mod
@ -16,7 +16,6 @@ require (
|
||||
github.com/jesseduffield/generics v0.0.0-20250517122708-b0b4a53a6f5c
|
||||
github.com/jesseduffield/go-git/v5 v5.14.1-0.20250407170251-e1a013310ccd
|
||||
github.com/jesseduffield/gocui v0.3.1-0.20250711082438-4aa4fd0b4d22
|
||||
github.com/jesseduffield/kill v0.0.0-20250101124109-e216ddbe133a
|
||||
github.com/jesseduffield/lazycore v0.0.0-20221012050358-03d2e40243c5
|
||||
github.com/jesseduffield/minimal/gitignore v0.3.3-0.20211018110810-9cde264e6b1e
|
||||
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
|
||||
|
2
go.sum
2
go.sum
@ -196,8 +196,6 @@ github.com/jesseduffield/go-git/v5 v5.14.1-0.20250407170251-e1a013310ccd h1:ViKj
|
||||
github.com/jesseduffield/go-git/v5 v5.14.1-0.20250407170251-e1a013310ccd/go.mod h1:lRhCiBr6XjQrvcQVa+UYsy/99d3wMXn/a0nSQlhnhlA=
|
||||
github.com/jesseduffield/gocui v0.3.1-0.20250711082438-4aa4fd0b4d22 h1:vhMwEsLlMtuKKo9/z3Qcggycgad8oV7+siwOZEnJDOs=
|
||||
github.com/jesseduffield/gocui v0.3.1-0.20250711082438-4aa4fd0b4d22/go.mod h1:sLIyZ2J42R6idGdtemZzsiR3xY5EF0KsvYEGh3dQv3s=
|
||||
github.com/jesseduffield/kill v0.0.0-20250101124109-e216ddbe133a h1:UDeJ3EBk04bXDLOPvuqM3on8HvyJfISw0+UMqW+0a4g=
|
||||
github.com/jesseduffield/kill v0.0.0-20250101124109-e216ddbe133a/go.mod h1:FSWDLKT0NQpntbDd1H3lbz51fhCVlMzy/J0S6nM727Q=
|
||||
github.com/jesseduffield/lazycore v0.0.0-20221012050358-03d2e40243c5 h1:CDuQmfOjAtb1Gms6a1p5L2P8RhbLUq5t8aL7PiQd2uY=
|
||||
github.com/jesseduffield/lazycore v0.0.0-20221012050358-03d2e40243c5/go.mod h1:qxN4mHOAyeIDLP7IK7defgPClM/z1Kze8VVQiaEjzsQ=
|
||||
github.com/jesseduffield/minimal/gitignore v0.3.3-0.20211018110810-9cde264e6b1e h1:uw/oo+kg7t/oeMs6sqlAwr85ND/9cpO3up3VxphxY0U=
|
||||
|
@ -171,16 +171,17 @@ func (self *cmdObjRunner) RunAndProcessLines(cmdObj *CmdObj, onLine func(line st
|
||||
line := scanner.Text()
|
||||
stop, err := onLine(line)
|
||||
if err != nil {
|
||||
stdoutPipe.Close()
|
||||
return err
|
||||
}
|
||||
if stop {
|
||||
_ = Kill(cmd)
|
||||
stdoutPipe.Close() // close the pipe so that the called process terminates
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if scanner.Err() != nil {
|
||||
_ = Kill(cmd)
|
||||
stdoutPipe.Close()
|
||||
return scanner.Err()
|
||||
}
|
||||
|
||||
@ -335,7 +336,7 @@ func (self *cmdObjRunner) runAndDetectCredentialRequest(
|
||||
tr := io.TeeReader(handler.stdoutPipe, cmdWriter)
|
||||
|
||||
go utils.Safe(func() {
|
||||
self.processOutput(tr, handler.stdinPipe, promptUserForCredential, cmdObj)
|
||||
self.processOutput(tr, handler.stdinPipe, promptUserForCredential, handler.close, cmdObj)
|
||||
})
|
||||
})
|
||||
}
|
||||
@ -344,6 +345,7 @@ func (self *cmdObjRunner) processOutput(
|
||||
reader io.Reader,
|
||||
writer io.Writer,
|
||||
promptUserForCredential func(CredentialType) <-chan string,
|
||||
closeFunc func() error,
|
||||
cmdObj *CmdObj,
|
||||
) {
|
||||
checkForCredentialRequest := self.getCheckForCredentialRequestFunc()
|
||||
@ -357,14 +359,16 @@ func (self *cmdObjRunner) processOutput(
|
||||
if ok {
|
||||
responseChan := promptUserForCredential(askFor)
|
||||
if responseChan == nil {
|
||||
// Returning a nil channel means we should kill the process.
|
||||
// Note that we don't break the loop after this, because we
|
||||
// still need to drain the output, otherwise the Wait() call
|
||||
// later might block.
|
||||
if err := Kill(cmdObj.GetCmd()); err != nil {
|
||||
// Returning a nil channel means we should terminate the process.
|
||||
// We achieve this by closing the pty that it's running in. Note that this won't
|
||||
// work for the case where we're not running in a pty (i.e. on Windows), but
|
||||
// in that case we'll never be prompted for credentials, so it's not a concern.
|
||||
if err := closeFunc(); err != nil {
|
||||
self.log.Error(err)
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
if task != nil {
|
||||
task.Pause()
|
||||
}
|
||||
@ -378,7 +382,6 @@ func (self *cmdObjRunner) processOutput(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// having a function that returns a function because we need to maintain some state inbetween calls hence the closure
|
||||
|
@ -127,7 +127,7 @@ func TestProcessOutput(t *testing.T) {
|
||||
writer := &strings.Builder{}
|
||||
|
||||
cmdObj := &CmdObj{task: gocui.NewFakeTask()}
|
||||
runner.processOutput(reader, writer, toChanFn(scenario.promptUserForCredential), cmdObj)
|
||||
runner.processOutput(reader, writer, toChanFn(scenario.promptUserForCredential), func() error { return nil }, cmdObj)
|
||||
|
||||
if writer.String() != scenario.expectedToWrite {
|
||||
t.Errorf("expected to write '%s' but got '%s'", scenario.expectedToWrite, writer.String())
|
||||
|
@ -13,7 +13,6 @@ import (
|
||||
"github.com/samber/lo"
|
||||
|
||||
"github.com/atotto/clipboard"
|
||||
"github.com/jesseduffield/kill"
|
||||
"github.com/jesseduffield/lazygit/pkg/common"
|
||||
"github.com/jesseduffield/lazygit/pkg/config"
|
||||
"github.com/jesseduffield/lazygit/pkg/utils"
|
||||
@ -264,16 +263,6 @@ func (c *OSCommand) PipeCommands(cmdObjs ...*CmdObj) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Kill kills a process. If the process has Setpgid == true, then we have anticipated that it might spawn its own child processes, so we've given it a process group ID (PGID) equal to its process id (PID) and given its child processes will inherit the PGID, we can kill that group, rather than killing the process itself.
|
||||
func Kill(cmd *exec.Cmd) error {
|
||||
return kill.Kill(cmd)
|
||||
}
|
||||
|
||||
// PrepareForChildren sets Setpgid to true on the cmd, so that when we run it as a subprocess, we can kill its group rather than the process itself. This is because some commands, like `docker-compose logs` spawn multiple children processes, and killing the parent process isn't sufficient for killing those child processes. We set the group id here, and then in subprocess.go we check if the group id is set and if so, we kill the whole group rather than just the one process.
|
||||
func PrepareForChildren(cmd *exec.Cmd) {
|
||||
kill.PrepareForChildren(cmd)
|
||||
}
|
||||
|
||||
func (c *OSCommand) CopyToClipboard(str string) error {
|
||||
escaped := strings.ReplaceAll(str, "\n", "\\n")
|
||||
truncated := utils.TruncateWithEllipsis(escaped, 40)
|
||||
|
@ -18,10 +18,13 @@ func (gui *Gui) newCmdTask(view *gocui.View, cmd *exec.Cmd, prefix string) error
|
||||
|
||||
manager := gui.getManager(view)
|
||||
|
||||
var r io.ReadCloser
|
||||
start := func() (*exec.Cmd, io.Reader) {
|
||||
r, err := cmd.StdoutPipe()
|
||||
var err error
|
||||
r, err = cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
gui.c.Log.Error(err)
|
||||
r = nil
|
||||
}
|
||||
cmd.Stderr = cmd.Stdout
|
||||
|
||||
@ -32,8 +35,15 @@ func (gui *Gui) newCmdTask(view *gocui.View, cmd *exec.Cmd, prefix string) error
|
||||
return cmd, r
|
||||
}
|
||||
|
||||
onClose := func() {
|
||||
if r != nil {
|
||||
r.Close()
|
||||
r = nil
|
||||
}
|
||||
}
|
||||
|
||||
linesToRead := gui.linesToReadFromCmdTask(view)
|
||||
if err := manager.NewTask(manager.NewCmdTask(start, prefix, linesToRead, nil), cmdStr); err != nil {
|
||||
if err := manager.NewTask(manager.NewCmdTask(start, prefix, linesToRead, onClose), cmdStr); err != nil {
|
||||
gui.c.Log.Error(err)
|
||||
}
|
||||
|
||||
|
@ -5,12 +5,10 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/jesseduffield/gocui"
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
||||
"github.com/jesseduffield/lazygit/pkg/utils"
|
||||
"github.com/sasha-s/go-deadlock"
|
||||
"github.com/sirupsen/logrus"
|
||||
@ -167,14 +165,7 @@ func (self *ViewBufferManager) NewCmdTask(start func() (*exec.Cmd, io.Reader), p
|
||||
// and the user is flicking through a bunch of items.
|
||||
self.throttle = time.Since(startTime) < THROTTLE_TIME && timeToStart > COMMAND_START_THRESHOLD
|
||||
|
||||
// Kill the still-running command.
|
||||
if err := oscommands.Kill(cmd); err != nil {
|
||||
if !strings.Contains(err.Error(), "process already finished") {
|
||||
self.Log.Errorf("error when trying to kill cmd task: %v; Command: %v %v", err, cmd.Path, cmd.Args)
|
||||
}
|
||||
}
|
||||
|
||||
// for pty's we need to call onDone here so that cmd.Wait() doesn't block forever
|
||||
// close the task's stdout pipe (or the pty if we're using one) to make the command terminate
|
||||
onDone()
|
||||
}
|
||||
})
|
||||
|
21
vendor/github.com/jesseduffield/kill/LICENSE
generated
vendored
21
vendor/github.com/jesseduffield/kill/LICENSE
generated
vendored
@ -1,21 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 Jesse Duffield
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
3
vendor/github.com/jesseduffield/kill/README.md
generated
vendored
3
vendor/github.com/jesseduffield/kill/README.md
generated
vendored
@ -1,3 +0,0 @@
|
||||
# Kill
|
||||
|
||||
Go package for killing processes across different platforms. Handles killing children of processes as well as the process itself.
|
33
vendor/github.com/jesseduffield/kill/kill_default_platform.go
generated
vendored
33
vendor/github.com/jesseduffield/kill/kill_default_platform.go
generated
vendored
@ -1,33 +0,0 @@
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package kill
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// Kill kills a process. If the process has Setpgid == true, then we have anticipated that it might spawn its own child processes, so we've given it a process group ID (PGID) equal to its process id (PID) and given its child processes will inherit the PGID, we can kill that group, rather than killing the process itself.
|
||||
func Kill(cmd *exec.Cmd) error {
|
||||
if cmd.Process == nil {
|
||||
// You can't kill a person with no body
|
||||
return nil
|
||||
}
|
||||
|
||||
if cmd.SysProcAttr != nil && cmd.SysProcAttr.Setpgid {
|
||||
// minus sign means we're talking about a PGID as opposed to a PID
|
||||
return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||
}
|
||||
|
||||
return cmd.Process.Kill()
|
||||
}
|
||||
|
||||
// PrepareForChildren ensures that child processes of this parent process will share the same group id
|
||||
// as the parent, meaning when the call Kill on the parent process, we'll kill
|
||||
// the whole group, parent and children both. Gruesome when you think about it.
|
||||
func PrepareForChildren(cmd *exec.Cmd) {
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{
|
||||
Setpgid: true,
|
||||
}
|
||||
}
|
174
vendor/github.com/jesseduffield/kill/kill_windows.go
generated
vendored
174
vendor/github.com/jesseduffield/kill/kill_windows.go
generated
vendored
@ -1,174 +0,0 @@
|
||||
// adapted from https://blog.csdn.net/fyxichen/article/details/51857864
|
||||
|
||||
package kill
|
||||
|
||||
import (
|
||||
"golang.org/x/sys/windows"
|
||||
"os"
|
||||
"os/exec"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const PROCESS_ALL_ACCESS = windows.STANDARD_RIGHTS_REQUIRED | windows.SYNCHRONIZE | 0xffff
|
||||
|
||||
func GetWindowsHandle(pid int) (handle windows.Handle, err error) {
|
||||
handle, err = windows.OpenProcess(PROCESS_ALL_ACCESS, false, uint32(pid))
|
||||
return
|
||||
}
|
||||
|
||||
func GetCreationTime(pid int) (time int64, err error) {
|
||||
handle, err := GetWindowsHandle(pid)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer closeHandle(HANDLE(handle))
|
||||
|
||||
var u syscall.Rusage
|
||||
err = syscall.GetProcessTimes(syscall.Handle(handle), &u.CreationTime, &u.ExitTime, &u.KernelTime, &u.UserTime)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
time = u.CreationTime.Nanoseconds()
|
||||
return
|
||||
}
|
||||
|
||||
// Kill kills a process, along with any child processes it may have spawned.
|
||||
func Kill(cmd *exec.Cmd) error {
|
||||
if cmd.Process == nil {
|
||||
// You can't kill a person with no body
|
||||
return nil
|
||||
}
|
||||
|
||||
ptime, err := GetCreationTime(cmd.Process.Pid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pids := Getppids(uint32(cmd.Process.Pid), ptime)
|
||||
for _, pid := range pids {
|
||||
pro, err := os.FindProcess(int(pid))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
pro.Kill()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// PrepareForChildren ensures that child processes of this parent process will share the same group id
|
||||
// as the parent, meaning when the call Kill on the parent process, we'll kill
|
||||
// the whole group, parent and children both. Gruesome when you think about it.
|
||||
func PrepareForChildren(cmd *exec.Cmd) {
|
||||
// do nothing because on windows our Kill function handles children by default.
|
||||
}
|
||||
|
||||
const (
|
||||
MAX_PATH = 260
|
||||
TH32CS_SNAPPROCESS = 0x00000002
|
||||
)
|
||||
|
||||
type ProcessInfo struct {
|
||||
Name string
|
||||
Pid uint32
|
||||
PPid uint32
|
||||
}
|
||||
|
||||
type PROCESSENTRY32 struct {
|
||||
DwSize uint32
|
||||
CntUsage uint32
|
||||
Th32ProcessID uint32
|
||||
Th32DefaultHeapID uintptr
|
||||
Th32ModuleID uint32
|
||||
CntThreads uint32
|
||||
Th32ParentProcessID uint32
|
||||
PcPriClassBase int32
|
||||
DwFlags uint32
|
||||
SzExeFile [MAX_PATH]uint16
|
||||
}
|
||||
|
||||
type HANDLE uintptr
|
||||
|
||||
var (
|
||||
modkernel32 = syscall.NewLazyDLL("kernel32.dll")
|
||||
procCreateToolhelp32Snapshot = modkernel32.NewProc("CreateToolhelp32Snapshot")
|
||||
procProcess32First = modkernel32.NewProc("Process32FirstW")
|
||||
procProcess32Next = modkernel32.NewProc("Process32NextW")
|
||||
procCloseHandle = modkernel32.NewProc("CloseHandle")
|
||||
)
|
||||
|
||||
func Getppids(pid uint32, ptime int64) []uint32 {
|
||||
infos, err := GetProcs()
|
||||
if err != nil {
|
||||
return []uint32{pid}
|
||||
}
|
||||
var pids []uint32 = make([]uint32, 0, len(infos))
|
||||
var index int = 0
|
||||
pids = append(pids, pid)
|
||||
|
||||
var length int = len(pids)
|
||||
for index < length {
|
||||
for _, info := range infos {
|
||||
if info.PPid == pids[index] {
|
||||
ctime, err := GetCreationTime(int(info.Pid))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if ctime >= ptime {
|
||||
// Only appending if child is newer than parent, otherwise PPid was reused
|
||||
pids = append(pids, info.Pid)
|
||||
}
|
||||
}
|
||||
}
|
||||
index += 1
|
||||
length = len(pids)
|
||||
}
|
||||
return pids
|
||||
}
|
||||
|
||||
func GetProcs() (procs []ProcessInfo, err error) {
|
||||
snap := createToolhelp32Snapshot(TH32CS_SNAPPROCESS, uint32(0))
|
||||
if snap == 0 {
|
||||
err = syscall.GetLastError()
|
||||
return
|
||||
}
|
||||
defer closeHandle(snap)
|
||||
var pe32 PROCESSENTRY32
|
||||
pe32.DwSize = uint32(unsafe.Sizeof(pe32))
|
||||
if process32First(snap, &pe32) == false {
|
||||
err = syscall.GetLastError()
|
||||
return
|
||||
}
|
||||
procs = append(procs, ProcessInfo{syscall.UTF16ToString(pe32.SzExeFile[:260]), pe32.Th32ProcessID, pe32.Th32ParentProcessID})
|
||||
for process32Next(snap, &pe32) {
|
||||
procs = append(procs, ProcessInfo{syscall.UTF16ToString(pe32.SzExeFile[:260]), pe32.Th32ProcessID, pe32.Th32ParentProcessID})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func createToolhelp32Snapshot(flags, processId uint32) HANDLE {
|
||||
ret, _, _ := procCreateToolhelp32Snapshot.Call(uintptr(flags), uintptr(processId))
|
||||
if ret <= 0 {
|
||||
return HANDLE(0)
|
||||
}
|
||||
return HANDLE(ret)
|
||||
}
|
||||
|
||||
func process32First(snapshot HANDLE, pe *PROCESSENTRY32) bool {
|
||||
ret, _, _ := procProcess32First.Call(uintptr(snapshot), uintptr(unsafe.Pointer(pe)))
|
||||
return ret != 0
|
||||
}
|
||||
|
||||
func process32Next(snapshot HANDLE, pe *PROCESSENTRY32) bool {
|
||||
ret, _, _ := procProcess32Next.Call(uintptr(snapshot), uintptr(unsafe.Pointer(pe)))
|
||||
return ret != 0
|
||||
}
|
||||
|
||||
func closeHandle(object HANDLE) bool {
|
||||
ret, _, _ := procCloseHandle.Call(uintptr(object))
|
||||
return ret != 0
|
||||
}
|
3
vendor/modules.txt
vendored
3
vendor/modules.txt
vendored
@ -230,9 +230,6 @@ github.com/jesseduffield/go-git/v5/utils/trace
|
||||
# github.com/jesseduffield/gocui v0.3.1-0.20250711082438-4aa4fd0b4d22
|
||||
## explicit; go 1.12
|
||||
github.com/jesseduffield/gocui
|
||||
# github.com/jesseduffield/kill v0.0.0-20250101124109-e216ddbe133a
|
||||
## explicit; go 1.18
|
||||
github.com/jesseduffield/kill
|
||||
# github.com/jesseduffield/lazycore v0.0.0-20221012050358-03d2e40243c5
|
||||
## explicit; go 1.18
|
||||
github.com/jesseduffield/lazycore/pkg/boxlayout
|
||||
|
Reference in New Issue
Block a user