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