fix(backup): give up on a pg_dump that does not finish within an hour (#7089)

exportPostgresDB started pg_dump with context.Background(), so a
PostgreSQL server that accepted the connection and then stopped answering
hung the database download and the scheduled Telegram and Discord
backups forever; SkipIfStillRunning then silently skipped every later
backup run.

pg_dump now runs under a one-hour deadline and the caller gets an error
that names the timeout.
This commit is contained in:
Aleksey Fedorov
2026-11-24 08:05:53 +10:00
committed by GitHub
parent e38ad52d3c
commit 5b0cbe6473
2 changed files with 48 additions and 1 deletions
+12 -1
View File
@@ -2021,6 +2021,10 @@ func pgConnEnv(dsn string) (env []string, dbname string, err error) {
return env, dbname, nil
}
// pgDumpTimeout bounds one pg_dump run: a server that stops answering would hang the
// download and, through SkipIfStillRunning, silently skip every later scheduled backup.
var pgDumpTimeout = time.Hour
func (s *ServerService) exportPostgresDB() ([]byte, error) {
bin, err := exec.LookPath("pg_dump")
if err != nil {
@@ -2030,12 +2034,19 @@ func (s *ServerService) exportPostgresDB() ([]byte, error) {
if err != nil {
return nil, common.NewErrorf("invalid PostgreSQL DSN: %v", err)
}
cmd := exec.CommandContext(context.Background(), bin, "--format=custom", "--no-owner", "--no-privileges", "--dbname", dbname)
ctx, cancel := context.WithTimeout(context.Background(), pgDumpTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, bin, "--format=custom", "--no-owner", "--no-privileges", "--dbname", dbname)
cmd.Env = env
// Stop waiting for the output pipes shortly after pg_dump is killed.
cmd.WaitDelay = 10 * time.Second
var out, stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
if ctx.Err() != nil {
return nil, common.NewErrorf("pg_dump did not finish within %s; check that the PostgreSQL server is responding", pgDumpTimeout)
}
return nil, common.NewErrorf("pg_dump failed: %v: %s", err, strings.TrimSpace(stderr.String()))
}
return out.Bytes(), nil
@@ -0,0 +1,36 @@
package service
import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
)
// A PostgreSQL server that accepts the connection and then stops answering
// must not hang the database download and the scheduled backups forever.
func TestExportPostgresDBGivesUpOnAStalledServer(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the fake pg_dump is a POSIX shell script")
}
bin := t.TempDir()
if err := os.WriteFile(filepath.Join(bin, "pg_dump"), []byte("#!/bin/sh\nexec sleep 30\n"), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
t.Setenv("XUI_DB_DSN", "postgres://xui:secret@127.0.0.1:5432/xui?sslmode=disable")
prev := pgDumpTimeout
pgDumpTimeout = 300 * time.Millisecond
t.Cleanup(func() { pgDumpTimeout = prev })
start := time.Now()
_, err := (&ServerService{}).exportPostgresDB()
if err == nil || !strings.Contains(err.Error(), "pg_dump did not finish within 300ms") {
t.Fatalf("exportPostgresDB() error = %v, want the timeout", err)
}
if took := time.Since(start); took > 15*time.Second {
t.Fatalf("exportPostgresDB() returned after %v", took)
}
}