From 5b0cbe64739e8442b0ee8095c9610247cf63bd25 Mon Sep 17 00:00:00 2001 From: Aleksey Fedorov <320380254+Dv2Corn@users.noreply.github.com> Date: Tue, 24 Nov 2026 08:05:53 +1000 Subject: [PATCH] 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. --- internal/web/service/server.go | 13 +++++++- internal/web/service/server_pg_dump_test.go | 36 +++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 internal/web/service/server_pg_dump_test.go diff --git a/internal/web/service/server.go b/internal/web/service/server.go index d598c8a59..0cd9deec0 100644 --- a/internal/web/service/server.go +++ b/internal/web/service/server.go @@ -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 diff --git a/internal/web/service/server_pg_dump_test.go b/internal/web/service/server_pg_dump_test.go new file mode 100644 index 000000000..5c1d34513 --- /dev/null +++ b/internal/web/service/server_pg_dump_test.go @@ -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) + } +}