MagicStack / MagicStack/asyncpg

Connect call failed error doesn't distinguish port mismatch from "server not running"

Đang mở
#1,342 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
Python
Star
8.1k
Fork
468
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

When connecting to a wrong port (e.g. PostgreSQL configured to listen on 5433 but DATABASE_URL specifies 5432), the error is a raw, unmodified OSError:

ConnectionRefusedError: [Errno 111] Connect call failed ('127.0.0.1', 5432)

This is identical to what you'd see if PostgreSQL wasn't running at all, there's nothing in the message pointing at "wrong port" as a possibility. I found a related case in sqlalchemy/sqlalchemy#8261 where someone spent a while debugging this exact symptom before discovering it was a port mismatch (Debian's non-default port in that case). Issue #694 also hit the identical message back in 2021.

Traced it to connect_utils.py, the OSError is caught as last_error in the address-iteration loop and re-raised verbatim with no added context:

raise last_error or exceptions.TargetServerAttributeNotMatched(...)

Proposed fix: when re-raising, append a short hint with the host/port that was attempted and a suggestion to verify the server is listening there, something like:

raise last_error from None if last_error is None else type(last_error)(
f"{last_error}. Verify PostgreSQL is running and listening on {addr}."
)

(or similar, open to whatever wrapping approach fits the codebase best). One thing worth flagging: modifying the exception text could affect anyone doing exact string matching on the error message, though that seems like an unlikely pattern to rely on. Happy to put together a PR if this direction is welcome.

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

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu trong connect_utils.py, tại vòng lặp duyệt địa chỉ nơi OSError được bắt lại, lưu dưới dạng last_error rồi được ném lại. Xem xét phần xử lý ngoại lệ xung quanh, sau đó làm cho các lỗi kết nối xác định host/port đã được thử và đề xuất kiểm tra xem PostgreSQL có đang lắng nghe tại đó hay không; kiểm tra cả trường hợp sai port và trường hợp máy chủ chưa chạy, đồng thời cân nhắc tác động của việc thay đổi văn bản ngoại lệ.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
postgresql, python
Lĩnh vực
databases
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
72/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.