Postgres connection pool leak on every StartClient/reconnect cycle
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 58/100
Hướng nghiên cứu
Bắt đầu trong pkg/whatsmeow/service/whatsmeow.go, tập trung vào sqlstore.New trong StartClient và các đường đi của ReconnectClient và kill-channel quay lại đó. Tái hiện sự cố với Postgres và kiểm tra pg_stat_activity để tìm các kết nối nhàn rỗi qua các chu kỳ khởi động lại. Công việc được xem là hoàn tất khi các chu kỳ reconnect hoặc QR teardown lặp lại không còn tích lũy các pool hoặc làm cạn kiệt max_connections.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Bug: Postgres connection pool leak on every StartClient/reconnect cycle
Commit: 9337afc (branch main, tag ~0.7.2)
File: pkg/whatsmeow/service/whatsmeow.go
Description
StartClient calls sqlstore.New(context.Background(), "postgres", w.config.PostgresAuthDB, nil) (lines ~322/329) to build the whatsmeow auth *sqlstore.Container on every invocation. This opens a brand new *sql.DB connection pool each time. container.Close() is never called anywhere in the file (confirmed with a full-file search).
StartClient is re-entered on:
ReconnectClient(line ~174), which callsStartInstance→StartClientagain- the kill-channel branch of
StartClient's own select loop (line ~629), which unconditionally callsw.StartClient(cd)again on any kill signal, not just genuine reconnect scenarios - every new QR pairing attempt that ends in
teardownQR→ kill signal → restart
Each of these paths creates a new pool that is never closed. The old pool (and any connections it opened) is simply dropped, leaking idle connections into the auth Postgres database. In production this fills max_connections (default 100) within roughly an hour of normal instance create/delete/reconnect activity, with idle connections observed sitting for days/weeks. Symptom on the client side: pq: sorry, too many clients already, and QR pairing requests timing out (504) because the container never manages to connect.
Suggested fix
Cache the *sqlstore.Container on whatsmeowService (it already holds authDB *sql.DB as a struct field) and reuse it across StartClient/ReconnectClient calls instead of building a new one each time. At minimum, defer container.Close() (or close the previous container before replacing it) on every path that currently discards it without cleanup.
Repro
- Create/delete a WhatsApp instance repeatedly (or force a few QR timeouts) against a Postgres-backed auth DB (
POSTGRES_AUTH_DBset). SELECT count(*) FROM pg_stat_activity;on the auth DB climbs steadily withstate = 'idle'rows that never close.- Given enough cycles,
max_connectionsis exhausted and pairing starts failing withpq: sorry, too many clients already.
- Ngôn ngữ chính
- Go
- Star
- 878
- Fork
- 461
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của evolution-foundation/evolution-go
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
-
bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
evolution-foundation/evolution-go#97 · 2 bình luận ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
Tất cả issue của evolution-foundation/evolution-go
Issue tương tự
-
nix: vendorHash is outdated Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
-
Bob Shell support Đang mởenhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
santhosh-tekuri/jsonschema#276 ·