php / php/php-src

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

Ouverte
#15,807 1 commentaire 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Bug Extension: mysqli Status: Needs Triage
Langage dominant
C
Étoiles
40.4k
Forks
8.1k
Merge moyen
2 j 13 h
PR mergées (30 j)
96

Description

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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par reproduire la connexion et la fermeture SSL de mysqli de PHP 8.3.11 décrites dans l’issue, puis comparez la sortie de tcpdump et les logs HAProxy avec les cas Python et Go. Le travail sera considéré comme terminé lorsqu’il aura été déterminé si PHP provoque la séquence RST et qu’un correctif, un contournement ou un comportement attendu confirmé aura été documenté.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
mysql, php
Domaine
databases, networking
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.