Skip to content

Commit

Permalink
Ensure the port isn't open before our git daemon
Browse files Browse the repository at this point in the history
If a previous run of the git daemon still has port 9418 open, then
we might think we successfully ran `git daemon`, when instead our
asynchronous command could be failing to bind the port and quitting
immediately. (The use of `set -e` would not mitigate this, since
the `git daemon ... &` command would have successfully forked off
the asynchronous subprocess.)

One way that could happen, at least in principle, is if the `EXIT`
trap from a previous run did not cause the `git-daemon` process to
terminate. That could happen either due to:

- The `EXIT` trap somehow not running.

- The child `git-daemon` process not being caused to terminate when
  its parent `git` process terminates. They are actually separate
  processes: Running a `git daemon ...` command creates a `git`
  process, which then creates a `git-daemon` subprocess (where
  `git-daemon` is in the `git-core` directory `git --exec-path`
  gives). The parent `git` process forks before exec-ing the child
  `git-daemon` process. So they have separate PIDs. The PID from
  `$!` belongs to the parent `git` process.

  However, both processes are usually terminated, at least in
  simplified local experiments.

This change aims to detect such conditions. (It might be worth
keeping even if that kind of problem is not currently happening.)
  • Loading branch information
EliahKagan committed Dec 17, 2024
1 parent 39a1cc7 commit eca9f99
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion tests/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@ function small-repo-in-sandbox() {
}

function launch-git-daemon() {
local port=9418
if nc -z localhost "$port"; then
echo "Port $port should not have been open before this test's run of the git daemon!" >&2
return 1
fi
git -c uploadpack.allowrefinwant daemon --verbose --base-path=. --export-all --user-path &>/dev/null &
daemon_pid=$!
while ! nc -z localhost 9418; do
while ! nc -z localhost "$port"; do
sleep 0.1
done
trap 'kill $daemon_pid' EXIT
Expand Down

0 comments on commit eca9f99

Please sign in to comment.