1--TEST--
2Killing a persistent connection.
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once 'skipifconnectfailure.inc';
8?>
9--INI--
10mysqli.allow_persistent=1
11mysqli.max_persistent=2
12--FILE--
13<?php
14    require_once 'table.inc';
15
16    $host = 'p:' . $host;
17    if (!$plink = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
18        printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
19            $host, $user, $db, $port, $socket);
20
21    // get the thread ids of the two connections...
22    $thread_id = mysqli_thread_id($link);
23    $pthread_id = mysqli_thread_id($plink);
24
25    if (!$res = mysqli_query($link, 'SHOW FULL PROCESSLIST'))
26        printf("[002] Cannot get processlist, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
27
28    $running_threads = array();
29    while ($row = mysqli_fetch_assoc($res))
30        $running_threads[$row['Id']] = $row;
31    mysqli_free_result($res);
32
33    if (count($running_threads) < 2)
34        printf("[003] Processlist is too short, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
35
36    if (!isset($running_threads[$thread_id]))
37        printf("[004] Cannot find thread id of the regular link, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
38
39    if (!isset($running_threads[$pthread_id]))
40        printf("[005] Cannot find thread id of the persistent link, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
41
42    // Kill the persistent connection - don't use mysqli_kill, mysqlnd will catch that...
43    if (!mysqli_query($link, sprintf('KILL %d', $pthread_id)))
44        printf("[006] Cannot kill persistent connection, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
45    mysqli_close($plink);
46    // Give the server think-time to kill the pthread
47    sleep(1);
48
49    if (!$res = mysqli_query($link, 'SHOW FULL PROCESSLIST'))
50        printf("[007] Cannot get processlist, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
51
52    $running_threads2 = array();
53    while ($row = mysqli_fetch_assoc($res))
54        $running_threads2[$row['Id']] = $row;
55    mysqli_free_result($res);
56
57    if (isset($running_threads2[$pthread_id]))
58        printf("[008] Thread of the persistent connection should have been gone, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
59    if (!isset($running_threads2[$thread_id]))
60        printf("[009] Thread of the regular connection should be still there, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
61
62    // On PHP side this should do nothing. PHP should not try to close the connection or something.
63    try {
64        mysqli_close($plink);
65    } catch (Error $exception) {
66        echo $exception->getMessage() . "\n";
67    }
68
69    if (!$plink = @my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
70        printf("[011] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
71            $host, $user, $db, $port, $socket);
72    if (!$res3 = @mysqli_query($plink, 'SELECT id FROM test ORDER BY id LIMIT 1')) {
73        printf("[012] New persistent connection cannot execute queries, [%d] %s\n", @mysqli_errno($plink), @mysqli_error($plink));
74    }
75
76    @mysqli_free_result($res3);
77    @mysqli_close($plink);
78    mysqli_close($link);
79
80    // remove the "p:<host>" from the host variable
81    $host = substr($host, 2);
82    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
83        printf("[013] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
84            $host, $user, $db, $port, $socket);
85    if (!$res4 = mysqli_query($link, 'SELECT id FROM test ORDER BY id LIMIT 1'))
86        printf("[014] New regular connection cannot execute queries, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
87
88    mysqli_free_result($res4);
89    mysqli_close($link);
90    print "done!";
91?>
92--CLEAN--
93<?php
94require_once 'clean_table.inc';
95?>
96--EXPECT--
97mysqli object is already closed
98done!
99