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

Open
opened 2026-11-07 14:34:21 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/7089
Author: @Dv2Corn
Created: 11/6/2026
Status: 🔄 Open

Base: main ← Head: fix/pg-dump-timeout


📝 Commits (1)

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

📊 Changes

2 files changed (+50 additions, -1 deletions)

View changed files

📝 internal/web/service/server.go (+14 -1)
➕ internal/web/service/server_pg_dump_test.go (+36 -0)

📄 Description

Summary

On a PostgreSQL install, pg_dump now runs under a one-hour deadline. A server that accepts the connection and then stops answering no longer hangs the database download and the scheduled Telegram and Discord backups forever; the caller gets a clear error instead.

Why

exportPostgresDB starts pg_dump with context.Background() (server.go#L2033), so nothing ever stops it. It is reached through GetDb() (server.go#L1638-L1641) from:

If PostgreSQL accepts the connection and then stalls (a lock that is never released, a half-dead network path, a server under heavy I/O pressure), pg_dump waits forever and so does its caller. The cron scheduler wraps every job in SkipIfStillRunning (web.go#L548), so the stuck Telegram or Discord notify job (web.go#L406, #L419) makes every later run of it be skipped, with no log line (cron.DiscardLogger). The admin stops getting both the periodic reports and the backups and is never told why.

What changes:

  • pg_dump runs under context.WithTimeout with pgDumpTimeout (one hour, generous for any panel database), and cmd.WaitDelay of 10 seconds so a killed pg_dump cannot keep Run waiting on its output pipes.
  • A timeout is returned as pg_dump did not finish within 1h0m0s; check that the PostgreSQL server is responding instead of the bare signal: killed. The callers already handle a GetDb() error: the download returns it to the panel, the Telegram backup and the Discord backup command tell the admin that the backup failed, and the Discord report logs it and is sent without the file.

The SQLite path is not changed.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

How was this tested?

New test internal/web/service/server_pg_dump_test.go:

  • TestExportPostgresDBGivesUpOnAStalledServer puts a fake pg_dump on PATH that sleeps for 30 seconds (a server that never answers), sets XUI_DB_DSN and lowers pgDumpTimeout to 300 ms. It expects the timeout error well before the fake would exit. Skipped on Windows (the fake is a shell script).
  • On main (with only pgDumpTimeout declared so the test compiles) it fails after the full 30 seconds: exportPostgresDB() error = <nil>, want the timeout. With the fix it passes in 0.30 s.

Run with Go 1.27.3 and the internal/web/dist stub the CI uses:

  • go build ./..., go vet ./... and gofmt -l are clean.
  • go test -shuffle=on -count=1 ./internal/web/service passes.
  • go test -race -shuffle=on -count=1 ./internal/web/service passes.

Screenshots / recordings

N/A

Breaking changes

None. A pg_dump that runs longer than one hour is now stopped and reported as a failed backup.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass.
  • I updated the Wiki / README / API docs if user-facing behavior changed.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/7089 **Author:** [@Dv2Corn](https://github.com/Dv2Corn) **Created:** 11/6/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/pg-dump-timeout` --- ### 📝 Commits (1) - [`bbbb4fd`](https://github.com/MHSanaei/3x-ui/commit/bbbb4fde049d8b1e4ccb9384da8009369b0d6f3f) fix(backup): give up on a pg_dump that does not finish within an hour ### 📊 Changes **2 files changed** (+50 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/server.go` (+14 -1) ➕ `internal/web/service/server_pg_dump_test.go` (+36 -0) </details> ### 📄 Description ## Summary On a PostgreSQL install, `pg_dump` now runs under a one-hour deadline. A server that accepts the connection and then stops answering no longer hangs the database download and the scheduled Telegram and Discord backups forever; the caller gets a clear error instead. ## Why `exportPostgresDB` starts `pg_dump` with `context.Background()` ([server.go#L2033](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/service/server.go#L2033)), so nothing ever stops it. It is reached through `GetDb()` ([server.go#L1638-L1641](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/service/server.go#L1638-L1641)) from: - the database download in the panel ([controller/server.go#L344-L345](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/controller/server.go#L344-L345)); - the Telegram backup, both the one sent with the periodic report ([tgbot_report.go#L54-L57](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/service/tgbot/tgbot_report.go#L54-L57), [#L112](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/service/tgbot/tgbot_report.go#L112)) and the one from the bot's backup screen ([screen_views.go#L254](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/service/tgbot/screen_views.go#L254)); - the Discord report and backup command ([discord/report.go#L170](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/service/discord/report.go#L170), [discord/gateway.go#L535](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/service/discord/gateway.go#L535)). The Discord job sets a 60-second context ([discord_notify_job.go#L41](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/job/discord_notify_job.go#L41)), but `GetDb()` takes no context, so it does not reach `pg_dump`. If PostgreSQL accepts the connection and then stalls (a lock that is never released, a half-dead network path, a server under heavy I/O pressure), `pg_dump` waits forever and so does its caller. The cron scheduler wraps every job in `SkipIfStillRunning` ([web.go#L548](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/web.go#L548)), so the stuck Telegram or Discord notify job ([web.go#L406](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/web.go#L406), [#L419](https://github.com/MHSanaei/3x-ui/blob/b54325dcc9d1dd640d5d11482e0a121c698c54a3/internal/web/web.go#L419)) makes every later run of it be skipped, with no log line (`cron.DiscardLogger`). The admin stops getting both the periodic reports and the backups and is never told why. What changes: - `pg_dump` runs under `context.WithTimeout` with `pgDumpTimeout` (one hour, generous for any panel database), and `cmd.WaitDelay` of 10 seconds so a killed `pg_dump` cannot keep `Run` waiting on its output pipes. - A timeout is returned as `pg_dump did not finish within 1h0m0s; check that the PostgreSQL server is responding` instead of the bare `signal: killed`. The callers already handle a `GetDb()` error: the download returns it to the panel, the Telegram backup and the Discord backup command tell the admin that the backup failed, and the Discord report logs it and is sent without the file. The SQLite path is not changed. ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [ ] Frontend (UI / panel pages) - [x] Backend (API endpoints, login, settings) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [x] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [x] Telegram bot ## How was this tested? New test `internal/web/service/server_pg_dump_test.go`: - `TestExportPostgresDBGivesUpOnAStalledServer` puts a fake `pg_dump` on `PATH` that sleeps for 30 seconds (a server that never answers), sets `XUI_DB_DSN` and lowers `pgDumpTimeout` to 300 ms. It expects the timeout error well before the fake would exit. Skipped on Windows (the fake is a shell script). - On `main` (with only `pgDumpTimeout` declared so the test compiles) it fails after the full 30 seconds: `exportPostgresDB() error = <nil>, want the timeout`. With the fix it passes in 0.30 s. Run with Go 1.27.3 and the `internal/web/dist` stub the CI uses: - `go build ./...`, `go vet ./...` and `gofmt -l` are clean. - `go test -shuffle=on -count=1 ./internal/web/service` passes. - `go test -race -shuffle=on -count=1 ./internal/web/service` passes. ## Screenshots / recordings N/A ## Breaking changes None. A `pg_dump` that runs longer than one hour is now stopped and reported as a failed backup. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [x] `go build ./...` and the test suite pass locally. - [ ] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. - [ ] I updated the Wiki / README / API docs if user-facing behavior changed. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6643
No description provided.