Re: Safely timing out DBI queries



On Sat, Sep 16, 2006 at 08:31:54PM -0400, Sam Tregar wrote:
On Sat, 16 Sep 2006, Dean Arnold wrote:

I think your best bet might be to work with the DBD::mysql maintainers
to implement some driver-specific nonblocking versions of
execute/prepare (and maybe fetch), as well as support for
'out of band' cancel.

That's an interesting idea.

For any driver that uses a network socket to connect you could close()
the socket in the signal handler to (relatively) safely timeout.

Should be fairly clean/safe for the db client library state, though
unsafe signals means there's still a chance perl's internal state could
be corrupted.

On the server-side the query may be left running on databases that
don't detect disconnects (which I think still includes mysql).

A minor problem with this approach is how to determine the socket file
descriptor. For drivers that don't/can't make the socket fd available,
it requires checking which fd's are open before and after the connect.
Kludgy but effective.


Right now I'm putting together
DBIx::Timeout which implements my fork()-based timeout in a reusable
package. Seems to work, although I've learned to expect forking code
to have unexpected bugs which take time to shake out.

It's mysql specific currently, using $id=$dbh->{thread_id} and do("KILL $id").
(And shouldn't thread_id be mysql_thread_id?)

Would be nice to generalise it. There was talk sometime ago (perhaps on
dbi-dev) of extending the DBI api along these lines:

$id = $dbh->{SessionId};

$dbh->kill_session($id);

Would certainly be simple for me to add to the DBI. Then it's just a
simple matter of getting the drivers to implement it :)


Also, rather than fork each time you need a timeout it would be nice to
be able to have a single 'watchdog' process and use some form of fast
IPC to tell it when to be active (passing the timeout to use and
relevant session id) and when to be inactive. A pipe would be fine.
But perhaps that could be a new DBIx::Watchdog module. Just a thought.

Tim.
.



Relevant Pages

  • Re: Recommended for i700
    ... Were you using the Socket card or the Sandisk card with the Socket drivers? ...
    (microsoft.public.pocketpc.wireless)
  • Re: Recommended for i700
    ... I finally installed the Socket SD Drivers last night... ... discernable change in the temperature of the card after it was removed. ... feelings of Microsoft or any of its employees. ...
    (microsoft.public.pocketpc.wireless)
  • Re: Cant get WPA-PSK on iPAQ h2210 (PPC 2003)?
    ... Yes, but this is a device with internal WIFI, so HP could do it. ... "Supports WPA and WPA-PSK on Windows Mobile 2003 Second Edition, ... > In the case of the OP his card has to support it via drivers. ... If Socket ...
    (microsoft.public.pocketpc.wireless)
  • Re: Support for TI FlashMedia (pci id 104c:8033, 104c:803b) flash card readers
    ... Stick proper handler to the socket ... of "card" then as that is widely used in the other mmc host drivers. ...
    (Linux-Kernel)
  • Re: ndis(4) patch to replace obsolete if_watchdog interface
    ... This works different to the rest of the network drivers. ... You arm the callout and stop it after each Tx, ... was already implemented for timeouts related to the NDIS systems. ... Set a timeout in case the chip goes out to lunch. ...
    (freebsd-current)