php / php/php-src

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

Aperta
#15,807 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Extension: mysqli Status: Needs Triage
Lingua principale
C
Stelle
40.4k
Fork
8.1k
Merge medio
2g 13h
PR unite (30g)
96

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo la connessione e la chiusura SSL di mysqli in PHP 8.3.11 descritte nell’issue, quindi confronta l’output di tcpdump e i log di HAProxy con i casi Python e Go. Il lavoro sarà considerato completato quando sarà stato determinato se PHP causa la sequenza RST e sarà stata documentata una correzione, una soluzione alternativa o un comportamento previsto confermati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
mysql, php
Ambito
databases, networking
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.