Hi,
Could Git add a user-invoked command to recover from a stale `index.lock` when no Git process runs anymore? It could wait briefly, then remove the lock and continue, but only if no other Git operation contests it.
Thanks
threads / discuss / 66867
Subject: User-initiated recovery for stale index.lock
Hi,
Could Git add a user-invoked command to recover from a stale `index.lock` when no Git process runs anymore? It could wait briefly, then remove the lock and continue, but only if no other Git operation contests it.
Thanks
Re: User-initiated recovery for stale index.lock
Hi Steve,
On Tue, 17 Nov 2026 at 20:51, Steve <steves.sk@gmail.com> wrote:
> Could Git add a user-invoked command to recover from a stale > `index.lock` when no Git process runs anymore? It could wait briefly, > then remove the lock and continue, but only if no other Git operation > contests it.
Git already has part of this. Since Git 2.54 there is core.lockfilePid. With it set, Git writes the PID of the lock holder next to the lock (index~pid.lock), and if it can't take the lock, it tells you whether that process is still running:
Lock was held by process 99999, which is no longer running; the lock file appears to be stale
You still remove index.lock yourself, though.
Jun Wu also has a series in 'seen' that adds core.indexLockTimeout, so that commands wait a little for the index lock before failing [1]. That covers your "wait briefly" suggestion.
Removing the lock automatically is tricky. A PID can be reused by an unrelated process, and it says nothing when the repository is also used from another machine, for example over NFS. When the PID file was discussed, Junio was wary even of telling users to remove the lock [2].
[1] https://lore.kernel.org/git/20261017073231.3357477-1-quark.zju@gmail.com/ [2] https://lore.kernel.org/git/xmqqbjj4hnkr.fsf@gitster.g/
Thanks, Tanga
Re: User-initiated recovery for stale index.lock
Hi Tanga,
Thanks for the links. Since core.lockfilePid already tracks the PID, why couldn't a manual recovery command perform that check itself and remove the lock when explicitly invoked by the user, along with ensuring that we wait for some reasonable time? Removing the file by hand doesn't allow users to apply any safety checks.
Thanks, Steve
st 25. 11. 2026 o 3:05 Tanga Joris <tangajoris@gmail.com> napísal(a):
> > Hi Steve, > > On Tue, 17 Nov 2026 at 20:51, Steve <steves.sk@gmail.com> wrote: > > Could Git add a user-invoked command to recover from a stale > > `index.lock` when no Git process runs anymore? It could wait briefly, > > then remove the lock and continue, but only if no other Git operation > > contests it. > > Git already has part of this. Since Git 2.54 there is > core.lockfilePid. With it set, Git writes the PID of the lock holder > next to the lock (index~pid.lock), and if it can't take the lock, it > tells you whether that process is still running: > > Lock was held by process 99999, which is no longer running; the lock > file appears to be stale > > You still remove index.lock yourself, though. > > Jun Wu also has a series in 'seen' that adds core.indexLockTimeout, > so that commands wait a little for the index lock before failing [1]. > That covers your "wait briefly" suggestion. > > Removing the lock automatically is tricky. A PID can be > reused by an unrelated process, and it says nothing when the > repository is also used from another machine, for example over NFS. > When the PID file was discussed, Junio was wary even of telling users > to remove the lock [2]. > > [1] https://lore.kernel.org/git/20261017073231.3357477-1-quark.zju@gmail.com/ > [2] https://lore.kernel.org/git/xmqqbjj4hnkr.fsf@gitster.g/ > > Thanks, > Tanga
Re: User-initiated recovery for stale index.lock
Hi Steve,
On Wed, 25 Nov 2026 at 16:07, Steve <steves.sk@gmail.com> wrote:
> Since core.lockfilePid already tracks the PID, > why couldn't a manual recovery command perform that check itself and > remove the lock when explicitly invoked by the user, along with > ensuring that we wait for some reasonable time? Removing the file by > hand doesn't allow users to apply any safety checks.
That's a fair point. A command could check things that removing the file by hand doesn't.
The problem is that the check can be wrong. If Git is killed while it holds the lock, both index.lock and index~pid.lock are left behind. If you then remove only index.lock, the next Git process can't write its own PID file, because the old one is still there. So the old PID stays.
I tried running "git commit" with the old PID file still there, and left its editor open. Then "git add" in another terminal said:
Lock was held by process 99999, which is no longer running; the lock file appears to be stale
So a command that trusts this check would remove a lock that is still in use. It also means index~pid.lock has to be removed together with index.lock.
Waiting wouldn't help much here. If the lock is stale, it stays there no matter how long we wait. And "git commit" keeps the lock for as long as its editor is open, so a lock that is still in use can also be there for minutes.
Also, the PID file doesn't say which machine the process is on. So if the repository is shared over NFS, a process on another machine that holds the lock can look gone from here.
Thanks, Tanga
Re: User-initiated recovery for stale index.lock
Hi Steve,
On Thu, 3 Dec 2026 at 02:28, Steve <steves.sk@gmail.com> wrote:
> That only happens when the lock is removed by hand. The command would > remove both files, after a short wait for a command that has just > taken the lock but not yet written its PID file.
The old PID file can already be there before anyone runs the command. Before Git 2.54, Git's own error message told users to remove index.lock by hand. Once someone does that, every Git process that takes the lock runs without its own PID file, and the short wait isn't helpful, because that process never writes one.
The command could catch this by checking if the PID file is older than index.lock, since Git writes the PID file right after it takes the lock.
> Could it store the hostname, like gc.pid does?
I think it could, and it's not a new idea. Torsten asked for the hostname when the PID file was first sent [1], and the file was kept as "pid <value>" so that more fields could be added later [2].
gc.pid already does this. It stores the hostname next to the PID, and if the gc is running on another machine, it doesn't check the PID and treats it as still running.
[1] https://lore.kernel.org/git/20251203194809.GA13919@tb-raspi4/ [2] https://lore.kernel.org/git/pull.2011.v2.git.1765997966593.gitgitgadget@gmail.com/
Thanks, Tanga