harbor-db: missing STOPSIGNAL turns every container stop into an unclean shutdown
Expected behavior and actual behavior:
Expected: stopping the harbor-db container (pod deletion, rollout, helm upgrade, node drain) shuts PostgreSQL down cleanly within a few seconds.
Actual: while any client is connected, which is always the case because harbor-core and jobservice keep connection pools open, PostgreSQL never finishes shutting down. The container is SIGKILLed when the grace period expires (120s in harbor-helm), and the next start has to run crash recovery:
[1] LOG: received smart shutdown request
... (nothing until SIGKILL 120s later)
# next start
LOG: database system was interrupted; last known up at ...
LOG: database system was not properly shut down; automatic recovery in progress
LOG: redo starts at 0/1BA24C8Root cause:
goharbor/harbor-db does not set STOPSIGNAL, so the runtime sends SIGTERM. For PostgreSQL, SIGTERM means Smart Shutdown: it stops accepting new connections and waits for all existing sessions to end on their own. Pooled idle connections never end, so the shutdown never completes.
The official postgres image sets STOPSIGNAL SIGINT (Fast Shutdown) for exactly this reason. Harbor already sets a component-specific stop signal elsewhere (STOPSIGNAL SIGQUIT in make/photon/nginx/Dockerfile and the portal Dockerfile), but not for the database.
Checked on main, v2.15.3-rc1, v2.15.2, v2.14.4 and v2.11.0: no STOPSIGNAL in make/photon/db/Dockerfile or Dockerfile.base, and the published images have "StopSignal": null in their config.
This looks related to the comment added in #15028 (fix for #15000) in docker-entrypoint.sh:
# In some cases, like helm upgrade, the postgresql may not quit cleanly.That change works around the unclean state during the PG major upgrade path. The fix below would remove the cause.
Steps to reproduce the problem:
A harbor-db StatefulSet with a PVC and the chart's terminationGracePeriodSeconds: 120, plus one client pod that holds an idle psql session (standing in for harbor-core's pool). Delete the DB pod once with the stock image and once with an image that only adds STOPSIGNAL SIGINT.
apiVersion: v1
kind: Secret
metadata:
name: db
stringData:
POSTGRES_PASSWORD: changeit
---
apiVersion: v1
kind: Service
metadata:
name: db
spec:
selector:
app: db
ports:
- port: 5432
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: db
spec:
serviceName: db
replicas: 1
selector:
matchLabels:
app: db
template:
metadata:
labels:
app: db
spec:
securityContext:
runAsUser: 999
fsGroup: 999
terminationGracePeriodSeconds: 120
containers:
- name: database
image: DB_IMAGE
imagePullPolicy: IfNotPresent
envFrom:
- secretRef:
name: db
readinessProbe:
exec:
command: ["/docker-healthcheck.sh"]
initialDelaySeconds: 1
periodSeconds: 5
timeoutSeconds: 5
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: client
spec:
replicas: 1
selector:
matchLabels:
app: client
template:
metadata:
labels:
app: client
spec:
containers:
- name: client
image: DB_IMAGE
imagePullPolicy: IfNotPresent
command: ["/bin/sh", "-c"]
args:
- |
while true; do
rm -f /tmp/stdin && mkfifo /tmp/stdin
psql -h db -U postgres -q < /tmp/stdin & p=$!
exec 3>/tmp/stdin
wait $p
exec 3>&-
sleep 1
done
env:
- name: PGPASSWORD
valueFrom:
secretKeyRef:
name: db
key: POSTGRES_PASSWORDkind create cluster --name harbor-db-stop
docker pull goharbor/harbor-db:v2.15.2
printf 'FROM goharbor/harbor-db:v2.15.2\nSTOPSIGNAL SIGINT\n' | docker build -t harbor-db-sigint:v2.15.2 -
kind load docker-image --name harbor-db-stop goharbor/harbor-db:v2.15.2 harbor-db-sigint:v2.15.2
for case in stock:goharbor/harbor-db:v2.15.2 fixed:harbor-db-sigint:v2.15.2; do
ns=${case%%:*}; img=${case#*:}
kubectl create namespace $ns
sed "s#DB_IMAGE#$img#" repro.yaml | kubectl -n $ns apply -f -
kubectl -n $ns rollout status sts/db
kubectl -n $ns rollout status deploy/client
sleep 10
# the client's idle session should be listed
kubectl -n $ns exec db-0 -- psql -U postgres -tAc \
"select count(*) from pg_stat_activity where backend_type='client backend' and pid <> pg_backend_pid()"
kubectl -n $ns logs -f db-0 > $ns-before.log &
time kubectl -n $ns delete pod db-0
kubectl -n $ns wait --for=condition=Ready pod/db-0 --timeout=300s
grep -E '\[1\] LOG: (received|database system is shut down)' $ns-before.log
kubectl -n $ns logs db-0 | grep -E 'was shut down at|not properly shut down'
doneThe first received fast shutdown request in *-before.log comes from the entrypoint's own initdb on first boot. The shutdown that matters is the one logged by PID [1].
Result (kind v0.33.0, Kubernetes v1.37.0, containerd 2.3.4, harbor-db v2.15.2 / PostgreSQL 18.3):
| image | kubectl delete pod |
shutdown | next start |
|---|---|---|---|
goharbor/harbor-db:v2.15.2 |
121s | received smart shutdown request, SIGKILL at grace period |
not properly shut down; automatic recovery in progress |
same + STOPSIGNAL SIGINT |
0s | received fast shutdown request, database system is shut down |
database system was shut down at ... |
With the stock image, readiness also fails for the whole grace period (FATAL: the database system is shutting down).
We see the same thing in production on Harbor v2.11.0 (harbor-helm 1.15.0, GKE, containerd): after helm upgrade the old DB pod took about 2m20s to go away, and the postmaster.pid it left behind still said stopping.
Proposed fix:
# make/photon/db/Dockerfile
STOPSIGNAL SIGINTThis covers both docker-compose and Kubernetes installs, since containerd and Docker both honor the image's stop signal. Fast shutdown rolls back open transactions and disconnects clients, then writes a shutdown checkpoint, so the data directory is left consistent.
If an image change is not wanted, harbor-helm could add a preStop hook running pg_ctl stop -m fast, but that only covers Kubernetes.
I'll open a PR with the one-line change.
Versions:
- harbor version: v2.15.2 (repro), v2.11.0 (production)
- harbor-helm: 1.15.0 (production)
- Kubernetes: v1.37.0 on kind v0.33.0 (repro), GKE (production)
- containerd: 2.3.4 (repro)
- PostgreSQL: 18.3 (harbor-db v2.15.2)
Source: goharbor/harbor