Bug #88858 mysql_ping generates SIGPIPE on timed out connection.
Submitted: 11 Dec 2017 12:59 Modified: 11 Dec 2017 13:16
Reporter: Alastair Growcott Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: C API (client library) Severity:S3 (Non-critical)
Version:5.7.20 OS:Linux (XUbuntu)
Assigned to: CPU Architecture:Any
Tags: mysql_ping

[11 Dec 2017 12:59] Alastair Growcott
Description:
When my client program calls mysql_ping() on a connection that has timed out after not being used for 8 hours, the program crashes with a SIGPIPE.

The backtrace is:

#0  0x00007f381a6e439d in __libc_send (fd=6, buf=0x1bf4050, n=5, flags=0)
    at ../sysdeps/unix/sysv/linux/x86_64/send.c:26
#1  0x00007f381b50eb45 in ?? ()
   from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20
#2  0x00007f381b4eb9c6 in ?? ()
   from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20
#3  0x00007f381b4ebb83 in ?? ()
   from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20
#4  0x00007f381b4ec227 in ?? ()
   from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20
#5  0x00007f381b4e7658 in ?? ()
   from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20
#6  0x00007f381b4dab26 in mysql_ping ()
   from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20

The manual (https://dev.mysql.com/doc/refman/5.7/en/c-api-threaded-clients.html) states:

To avoid aborting the program when a connection terminates, MySQL blocks SIGPIPE on the first call to mysql_library_init(), mysql_init(), or mysql_connect().

It appears that mysql_ping() is unblocking the signal during its operation.

How to repeat:
Set the following configuration in order to quickly stimulate the bug, so you don't need to wait 8 hours:

wait_timeout=30
interactive_timeout=30

My program opens a connection, prepares a statement, and executes it to retrieve information. It then waits for incoming TCP connections, receives user information and queries the database to authenticate the user. Before it queries the database it calls mysql_ping() to verify that the connection is still alive.

I have written a test program to reproduce these steps but it doesn't show the crash.

Before I implemented the mysql_ping() method, if the connection had timed out an error was returned on the query.
[11 Dec 2017 13:03] Alastair Growcott
I've just realised that my code may have called mysql_close() and not reconnected before calling mysql_ping() which may mean that this is a duplicate of https://bugs.mysql.com/bug.php?id=56669 although that issue generates a SIGSEGV.
[11 Dec 2017 13:13] Alastair Growcott
I think I'm having a bad day. It looks very definitely like a poorly managed close of the DB handle (I forgot to set the boolean flag indicating connection status to false) is causing an old and probably deallocated database handle to be used.

Please close this bug and my sincerest apologies for wasting your time.
[11 Dec 2017 13:16] MySQL Verification Team
Thank you for the feedback.