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) + } +}