Skip to content

Commit 50f0980

Browse files
leonardocearmru
andauthored
fix: prevent leaking connection when snapshot backup fail (cloudnative-pg#6879)
The instance manager allocates a PostgreSQL connection every time a volume snapshot backup need to be taken and then runs `pg_backup_start` on it. That connection will stay up until the backup is done. If the snapshotting process fails, the backup will be marked as failed but the connection won't be closed. This patch ensures that a successive backup is able to proceed correctly by closing the previous connection. A successive commit will allow the operator to close that stale commit even when there's no successive backup. Partially closes cloudnative-pg#6761 Signed-off-by: Leonardo Cecchi <leonardo.cecchi@enterprisedb.com> Signed-off-by: Armando Ruocco <armando.ruocco@enterprisedb.com> Co-authored-by: Armando Ruocco <armando.ruocco@enterprisedb.com>
1 parent fee1bc5 commit 50f0980

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

pkg/management/postgres/webserver/backup_connection.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ func (bc *backupConnection) closeConnection(backupName string) error {
8585
return bc.conn.Close()
8686
}
8787

88+
func (bc *backupConnection) forceCloseConnection() error {
89+
bc.sync.Lock()
90+
defer bc.sync.Unlock()
91+
92+
return bc.conn.Close()
93+
}
94+
8895
func (bc *backupConnection) executeWithLock(backupName string, cb func() error) {
8996
bc.sync.Lock()
9097
defer bc.sync.Unlock()

pkg/management/postgres/webserver/remote.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,10 @@ func (ws *remoteWebserverEndpoints) backup(w http.ResponseWriter, req *http.Requ
277277
sendUnprocessableEntityJSONResponse(w, "PROCESS_ALREADY_RUNNING", "")
278278
return
279279
}
280-
if err := ws.currentBackup.closeConnection(p.BackupName); err != nil {
280+
log.Info("trying to close the current backup connection",
281+
"backupName", ws.currentBackup.data.BackupName,
282+
)
283+
if err := ws.currentBackup.forceCloseConnection(); err != nil {
281284
if !errors.Is(err, sql.ErrConnDone) {
282285
log.Error(err, "Error while closing backup connection (start)")
283286
}

0 commit comments

Comments
 (0)