php / php/php-src

RST packets when disconnecting SSL session and occasional errors when haproxy is loadbalancing mysql

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

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

Bug Extension: mysqli Status: Needs Triage
Ngôn ngữ chính
C
Star
40.4k
Fork
8.1k
Merge trung bình
2 ngày 13 giờ
Pull request đã merge (30 ngày)
96

Mô tả

Description

I set up an HAProxy load balancer in front of a MySQL cluster to distribute connections from my PHP app to the database. However, I noticed that around 40% of the time, HAProxy logs an error with termination state "SD." According to the HAProxy documentation:

  • S: The TCP session was unexpectedly aborted by the server or explicitly refused.
  • D: The session was in the DATA phase.

I reproduced this issue using simple PHP code that connects and disconnects from the MySQL server:

<?php
$username = "foobar";
$servername = "lb.foo.bar";
$password = "foobar";
$dbname = "test";

$mysqli = new mysqli();
$mysqli->real_connect($servername, $username, $password, $dbname, NULL, NULL, MYSQLI_CLIENT_SSL);

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}
echo "Connected successfully with SSL";

$mysqli->close();
?>

When analyzing the traffic with tcpdump, I observed a TLS encrypted alert packet, followed by the session being torn down and RST packets arriving from the server. Occasionally, two RST packets are sent, but when three RST packets are received, HAProxy logs the "SD" error.

Initially, I suspected a configuration issue, but my setup aligns with HAProxy's recommended configuration. I tested direct MySQL connections (bypassing HAProxy) and encountered the same connection teardown behavior. Interestingly, switching PHP to non-SSL resolved the issue, and I saw a normal TCP teardown sequence (FIN, ACK; FIN, ACK; ACK).

I also tested Python and Go applications with SSL, and both showed the expected TCP teardown sequence, similar to non-SSL connections in PHP. Only PHP with SSL seems to trigger the abnormal behavior, with a TLS encrypted alert packet followed by an unexpected termination.

Problematic tcpdump:
Screenshot 2024-09-08 at 19 24 54

Same connection with Python code:
Screenshot 2024-09-08 at 19 27 34

In summary:

  • PHP with SSL causes unexpected RST packets and HAProxy "SD" errors.
  • Python and Go, even with SSL, behave as expected.
  • The issue seems to stem from how PHP handles SSL termination compared to other languages.

Is this a bug in PHP's SSL handling, or is this expected behavior when using SSL with MySQL in PHP?

PHP Version

PHP 8.3.11

Operating System

AlmaLinux 8

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

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 bằng cách tái hiện kết nối và quá trình teardown SSL của mysqli trong PHP 8.3.11 được mô tả trong issue, sau đó so sánh đầu ra của tcpdump và log của HAProxy với các trường hợp Python và Go. Công việc được xem là hoàn tất khi xác định được liệu PHP có gây ra chuỗi RST hay không và ghi lại một bản sửa lỗi, workaround hoặc hành vi được mong đợi đã được xác nhận.

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

Đánh giá

Công nghệ
mysql, php
Lĩnh vực
databases, networking
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
25/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.