Handled signal during a query kills the connection (08S01, TCP Provider 0x2714): EINTR treated as fatal; SQL_ATTR_QUERY_TIMEOUT makes it unavoidable — deterministic repro

Abierto
#1,682 3 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
58/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
linux, php, sql
Área
backend, database

Línea de trabajo

Comienza con la reproducción matrix.php proporcionada y ejecuta la matriz 2×2 de señal/timeout contra SQL Server; después compara el comportamiento de ppoll y recvfrom con strace -f -T. Traza la ruta de espera del socket de pdo_sqlsrv para el manejo de EINTR. Se considera terminado cuando las esperas interrumpidas se reintentan sin cerrar la conexión y la matriz se completa con una conexión utilizable en todas las celdas.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

area: external/odbc bug odbc

Summary

A handled POSIX signal delivered while the driver is waiting for results fatally kills the connection with SQLSTATE[08S01] ... TCP Provider: Error code 0x2714 (0x2714 = 10004 = WSAEINTR). The driver treats EINTR on its socket wait as a fatal connection error instead of retrying the wait.

With SQL_ATTR_QUERY_TIMEOUT set (PDO::SQLSRV_ATTR_QUERY_TIMEOUT), this is unavoidable: the result wait becomes ppoll(fd, deadline, sigmask=NULL), and ppoll is never restarted by SA_RESTART (POSIX), so even well-behaved signal handlers kill the connection. Without the timeout attribute, the wait is a blocking recvfrom() that SA_RESTART restarts — but a handler registered without SA_RESTART kills that path too, so the underlying EINTR handling is the defect in both cases.

Deterministic: 12/12 across the test matrix below, plus a cross-check against a second SQL Server major version.

Environment

  • PHP 8.5.9 (NTS), pdo_sqlsrv 5.13.3
  • msodbcsql18 18.6.2.1-1, unixODBC 2.3.12
  • Debian-based container, linux/arm64 (mechanism is syscall-level; older reports of the same error on x86_64: #365, #835, #885)
  • SQL Server 2022 (16.0.4250) and SQL Server 2019 (mcr.microsoft.com/mssql/server images) — identical results, as expected for a client-side defect
  • TLS on (Encrypt=1;TrustServerCertificate=1); reproduces with unixODBC pooling on and off

Reproduction

docker network create repro
docker run -d --name mssql --network repro --network-alias mssql \
  -e ACCEPT_EULA=Y -e MSSQL_SA_PASSWORD='Repro0x2714!Pw' \
  mcr.microsoft.com/mssql/server:2022-latest

matrix.php (needs pdo_sqlsrv + pcntl):

<?php
// argv: <armed|control> <restart|norestart>
[$mode, $restart] = [$argv[1], $argv[2] === 'restart'];
$opts = $mode === 'armed' ? [PDO::SQLSRV_ATTR_QUERY_TIMEOUT => 30] : [];
$pdo = new PDO("sqlsrv:Server=mssql,1433;Database=master;Encrypt=1;TrustServerCertificate=1",
    'sa', getenv('SA_PASSWORD'), $opts);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
pcntl_async_signals(true);
pcntl_signal(SIGUSR1, function () {}, $restart);   // restart = SA_RESTART on/off
try {
    $pdo->query("WAITFOR DELAY '00:00:04'");
    $q = 'COMPLETED';
} catch (PDOException $e) {
    $q = str_contains($e->getMessage(), '0x2714') ? '0x2714' : substr($e->getMessage(), 0, 40);
}
try { $pdo->query('SELECT 1'); $c = 'usable'; } catch (PDOException $e) { $c = 'DEAD'; }
printf("%-8s restart=%-5s query=%-9s conn=%s\n", $mode, var_export($restart, true), $q, $c);

Driver script — run the query, deliver one signal mid-wait:

php matrix.php armed restart & PID=$!; sleep 2; kill -USR1 $PID; wait $PID

Results (3 runs per cell, fully deterministic)

handler with SA_RESTART handler without SA_RESTART
SQLSRV_ATTR_QUERY_TIMEOUT set 0x2714, connection DEAD 0x2714, connection DEAD
no timeout attribute COMPLETED, connection usable 0x2714, connection DEAD

Same 2×2 against SQL Server 2019: identical.

The signal does not need to come from pcntl: an external kill -PROF <pid> against the SIGPROF handler PHP itself installs (Zend execution-timeout machinery, present in every PHP process) produces the same 0x2714 — which is how real php-fpm deployments hit this without any signal-related code of their own.

strace

Same query (WAITFOR DELAY '00:00:04'), strace -f -T:

# no timeout attribute — restartable blocking read:
recvfrom(5, "\27\3\3\0a...", 20776, 0, NULL, NULL) = 102 <4.002522>

# SQL_ATTR_QUERY_TIMEOUT set — non-restartable deadline poll:
ppoll([{fd=5, events=POLLIN}], 1, {tv_sec=2, tv_nsec=0}, NULL, 0) = 0 (Timeout) <2.002987>

strace also shows the timeout implementation creates no timers and no signals of its own (no timer_create/setitimer/alarm) — the deadline is purely the ppoll timeout, so the defect is confined to EINTR handling on the socket waits.

Expected behavior

EINTR from ppoll/recvfrom is not a connection failure. The wait should be retried with the remaining deadline (or the ppoll sigmask argument used to defer signals for the wait's duration). As it stands, setting a query timeout converts any routinely-signaled process — php-fpm being the canonical case, with its built-in SIGPROF/SIGUSR handlers — into one that sporadically kills healthy connections mid-query with a fatal, unretried 08S01.

Related (all closed, none with a deterministic repro)

#365 (random 0x2714 + the SMux 08S02 companion), #835 (0x2714 near the timeout boundary), #885 (SIGALRM during query), #1242 (0x2714 after Apache reload — the reload's signals, in hindsight).

Lenguaje dominante
PHP
Estrellas
1.9k
Forks
386
Merge medio
4 d 21 h
PR fusionados (30 d)
9

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de microsoft/msphpsql

Todos los issues de microsoft/msphpsql

Issues similares

Más issues de PHP

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.