[prev in list] [next in list] [prev in thread] [next in thread] 

List:       mysql
Subject:    Re: signal handling in mysql cli
From:       Sebastien FLAESCH <sf () 4js ! com>
Date:       2014-12-12 13:49:03
Message-ID: 548AF24F.2080205 () 4js ! com
[Download RAW message or body]

Thank you Rafal for your answer!

We do not use threads, it's a single threaded program. So we cannot set
a flag to exec the KILL QUERY later on in another thread.

The only flag I use is to avoid subsequent KILL QUERY attempts when one
has already been sent...

The code in the sigint handler is quite simple, basic mysql_ calls and
debug messages (fprintfs):

     if (currConn && currConn->canInterrupt) {
         char cmd[50];
         MYSQL *h;
         currConn->canInterrupt = 0;
         h = mysql_init(NULL);
         if (!mysql_real_connect(h, currConn->c_host, currConn->c_user,
                                 currConn->c_auth, currConn->c_dbnm,
                                 currConn->c_port, currConn->c_sock,
                                 CLIENT_FOUND_ROWS)) {
             DBGMSGINT(2, "Interrupt: Could not connect to database: %d.",
                       mysql_errno(h));
             return 0;
         }
         sprintf(cmd, "KILL QUERY %ld", currConn->threadId);
         if (mysql_query(h, cmd) != 0) {
             DBGMSGSTR(2, "Could not execute %s.", cmd);
         }
         mysql_close(h);
     }


Should do the job...

Thanks.
Seb

On 12/12/2014 12:12 PM, rafal somla wrote:
> Hello Sebastein,
> 
> On 2014-12-03 17:25, Sebastien FLAESCH wrote:
> > Hi all,
> > 
> > I have a similar question regarding KILL QUERY usage:
> > 
> > We have a C client program using libmysqlclient.so, it is a
> > single-threaded program.
> > 
> > When running a long query, how can I send the KILL QUERY command when a
> > SIGINT (CTRL-C)
> > is caught? (of course we implement a signal handler, so we keep the
> > control)
> > 
> > => Is is safe to establish a new connection to the server in the signal
> > handler, using
> > mysql_init() + mysql_real_connect(), and execute the KILL QUERY with the
> > mysql thread
> > id I got from the initial mysql_init() / mysql_real_connect()?
> > 
> > I made some tests, and it seems to work fine, the long query returns SQL
> > error -1317:
> > "Query execution was interrupted" (which is expected)
> > 
> > We want to support SQL interruption properly, so please someone from the
> > dev team,
> > give me a clear answer... I don't want to use a side effect or
> > undocumented feature.
> 
> Issuing KILL QUERY from a separate connection is the way to do it. We do not \
> currently have any CAPI "abort" function - introducing such functionality would \
> require changes on the protocol level. 
> > 
> > Doing all this stuff in a signal handler is certainly risky... no?
> 
> Whether it is safe to do I/O extensive work inside a signal handler is another \
> topic, not related to MySQL client library. Depends on your particular case and how \
> careful you are about writing handler code which can be executed at any time. One \
> safe option is that signal handler only sets a flag informing about the signal and \
> then either the main logic of your application detects the flag and performs KILL \
> operation, or you have a separate dedicated thread to do this job.
> 
> Rafal (from Connectors/C team @ MySQL)
> 
> > 
> > I could not find the information in the documentation (mysql_real_connect).
> > 
> > I wish there would be an API like mysql_cancel_query(), similar to
> > Oracle's OCI
> > OCIBreak().
> > 
> > Thanks!
> > Seb
> > 
> > 
> > 
> > On 12/02/2014 05:13 PM, walter harms wrote:
> > > hi list,
> > > 
> > > when i use CTRL-C to break a query that works fine in interactive mode.
> > > 
> > > mysql> select sleep(10) ;
> > > ^CCtrl-C -- sending "KILL QUERY 24289" to server ...
> > > Ctrl-C -- query aborted.
> > > +-----------+
> > > > sleep(10) |
> > > +-----------+
> > > +-----------+
> > > 1 row in set (0.86 sec)
> > > 
> > > but when i use the noninteractive mode
> > > timeout 5 mysql -BAN -e "select now(); select sleep (100) ; select
> > > now() "
> > > 
> > > i looks like that  but "show full processlist;" shows otherwise and
> > > that is true
> > > as a list of long running querys showed.
> > > 
> > > Is there a way to make it behave like the interactive version ?
> > > Now it is a bit confusing for everyone.
> > > 
> > > re,
> > > wh
> > > 
> > 
> > 
> 


-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/mysql


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic