Files
git/t/t0031-lockfile-pid.sh
Paulo Casaretto dbdcab6b89 lockfile: add PID file for debugging stale locks
When a lock file is held, it can be helpful to know which process owns
it, especially when debugging stale locks left behind by crashed
processes. Add an optional feature that creates a companion PID file
alongside each lock file, containing the PID of the lock holder.

For a lock file "foo.lock", the PID file is named "foo~pid.lock". The
tilde character is forbidden in refnames and allowed in Windows
filenames, which guarantees no collision with the refs namespace
(e.g., refs "foo" and "foo~pid" cannot both exist). The file contains
a single line in the format "pid <value>" followed by a newline.

The PID file is created when a lock is acquired (if enabled), and
automatically cleaned up when the lock is released (via commit or
rollback). The file is registered as a tempfile so it gets cleaned up
by signal and atexit handlers if the process terminates abnormally.

When a lock conflict occurs, the code checks for an existing PID file
and, if found, uses kill(pid, 0) to determine if the process is still
running. This allows providing context-aware error messages:

  Lock is held by process 12345. Wait for it to finish, or remove
  the lock file to continue.

Or for a stale lock:

  Lock was held by process 12345, which is no longer running.
  Remove the stale lock file to continue.

The feature is controlled via core.lockfilePid configuration (boolean).
Defaults to false. When enabled, PID files are created for all lock
operations.

Existing PID files are always read when displaying lock errors,
regardless of the core.lockfilePid setting. This ensures helpful
diagnostics even when the feature was previously enabled and later
disabled.

Signed-off-by: Paulo Casaretto <pcasaretto@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2026-01-22 12:15:46 -08:00

106 lines
2.8 KiB
Bash
Executable File

#!/bin/sh
test_description='lock file PID info tests
Tests for PID info file alongside lock files.
The feature is opt-in via core.lockfilePid config setting (boolean).
'
. ./test-lib.sh
test_expect_success 'stale lock detected when PID is not running' '
git init repo &&
(
cd repo &&
touch .git/index.lock &&
printf "pid 99999" >.git/index~pid.lock &&
test_must_fail git -c core.lockfilePid=true add . 2>err &&
test_grep "process 99999, which is no longer running" err &&
test_grep "appears to be stale" err
)
'
test_expect_success 'PID info not shown by default' '
git init repo2 &&
(
cd repo2 &&
touch .git/index.lock &&
printf "pid 99999" >.git/index~pid.lock &&
test_must_fail git add . 2>err &&
# Should not crash, just show normal error without PID
test_grep "Unable to create" err &&
! test_grep "is held by process" err
)
'
test_expect_success 'running process detected when PID is alive' '
git init repo3 &&
(
cd repo3 &&
echo content >file &&
# Get the correct PID for this platform
shell_pid=$$ &&
if test_have_prereq MINGW && test -f /proc/$shell_pid/winpid
then
# In Git for Windows, Bash uses MSYS2 PIDs but git.exe
# uses Windows PIDs. Use the Windows PID.
shell_pid=$(cat /proc/$shell_pid/winpid)
fi &&
# Create a lock and PID file with current shell PID (which is running)
touch .git/index.lock &&
printf "pid %d" "$shell_pid" >.git/index~pid.lock &&
# Verify our PID is shown in the error message
test_must_fail git -c core.lockfilePid=true add file 2>err &&
test_grep "held by process $shell_pid" err
)
'
test_expect_success 'PID info file cleaned up on successful operation when enabled' '
git init repo4 &&
(
cd repo4 &&
echo content >file &&
git -c core.lockfilePid=true add file &&
# After successful add, no lock or PID files should exist
test_path_is_missing .git/index.lock &&
test_path_is_missing .git/index~pid.lock
)
'
test_expect_success 'no PID file created by default' '
git init repo5 &&
(
cd repo5 &&
echo content >file &&
git add file &&
# PID file should not be created when feature is disabled
test_path_is_missing .git/index~pid.lock
)
'
test_expect_success 'core.lockfilePid=false does not create PID file' '
git init repo6 &&
(
cd repo6 &&
echo content >file &&
git -c core.lockfilePid=false add file &&
# PID file should not be created when feature is disabled
test_path_is_missing .git/index~pid.lock
)
'
test_expect_success 'existing PID files are read even when feature disabled' '
git init repo7 &&
(
cd repo7 &&
touch .git/index.lock &&
printf "pid 99999" >.git/index~pid.lock &&
# Even with lockfilePid disabled, existing PID files are read
# to help diagnose stale locks
test_must_fail git add . 2>err &&
test_grep "process 99999" err
)
'
test_done