php / php/php-src

MYSQLI_OPT_CONNECT_TIMEOUT and mysql.connect_timeout not working when connecting to a non-responsive server

オープン
#15,193 コメント 5 件 リアクション 1 件 担当者 1 名 GitHub で見る

@SakiTakamachi がすでに取り組んでいます。

2024年8月2日 から。

Bug Extension: mysqli Status: Needs Triage
主要言語
C
スター
40.4k
フォーク
8.1k
平均マージ
2日 13時間
マージ済み PR(30日)
96

説明

Description

I encountered an issue where the MYSQLI_OPT_CONNECT_TIMEOUT option and the mysql.connect_timeout setting do not work as expected when attempting to connect to a non-responsive server. The connection attempt hangs instead of timing out.
In contrast, when attempting to connect to a non-existent server, the connection attempt fails immediately.

Steps to Reproduce:

  1. Set up a non-responsive server.
    I made it by python like this(tcp_server.py):
import socket

HOST = '0.0.0.0'
PORT = 12345

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    print(f"Listening on {HOST}:{PORT}")
    while True:
        conn, addr = s.accept()
        print(f"Connected by {addr}")
  1. Use the mysqli extension to connect to this server with the MYSQLI_OPT_CONNECT_TIMEOUT option set.
<?php
$connect = @mysqli_init();
mysqli_options($connect, MYSQLI_OPT_CONNECT_TIMEOUT, 5);
@mysqli_real_connect($connect, 'host.docker.internal', 'user', 'password', null, 12345, "", 0);
  1. Observe that the connection attempt does not timeout as expected.

Expected Behavior:
The connection attempt should timeout after 5 seconds as specified MYSQLI_OPT_CONNECT_TIMEOUT duration and rise an exception.

Actual Behavior:
The connection attempt hangs without timing out.

I used the following PHP.

php -v
PHP 8.2.21 (cli) (built: Jul 23 2024 06:55:37) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.21, Copyright (c) Zend Technologies

The mysqlnd.net_read_timeout and MYSQL_OPT_READ_TIMEOUT options work, but they affect not only the connection handshake but also every query execution, which is not desirable for our use case.

PHP Version

PHP 8.2.21

Operating System

No response

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。