diff --git a/ext/mysql2/client.c b/ext/mysql2/client.c index 2650b2f..f2942c2 100644 --- a/ext/mysql2/client.c +++ b/ext/mysql2/client.c @@ -257,7 +257,6 @@ static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) { int fd, retval; int async = 0; VALUE opts, defaults; - int(*selector)(int, fd_set *, fd_set *, fd_set *, struct timeval *) = NULL; GET_CLIENT(self) REQUIRE_OPEN_DB(client); @@ -299,12 +298,11 @@ static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) { // the below code is largely from do_mysql // http://github.com/datamapper/do fd = client->net.fd; - selector = rb_thread_alone() ? *select : *rb_thread_select; for(;;) { FD_ZERO(&fdset); FD_SET(fd, &fdset); - retval = selector(fd + 1, &fdset, NULL, NULL, NULL); + retval = rb_thread_select(fd + 1, &fdset, NULL, NULL, NULL); if (retval < 0) { rb_sys_fail(0); diff --git a/spec/mysql2/client_spec.rb b/spec/mysql2/client_spec.rb index 7c5c8ce..2df7a77 100644 --- a/spec/mysql2/client_spec.rb +++ b/spec/mysql2/client_spec.rb @@ -78,6 +78,33 @@ describe Mysql2::Client do @client.query("SELECT 1") }.should raise_error(Mysql2::Error) end + + # XXX this test is not deterministic (because Unix signal handling is not) + # and may fail on a loaded system + it "should run signal handlers while waiting for a response" do + mark = {} + trap(:USR1) { mark[:USR1] = Time.now } + begin + mark[:START] = Time.now + pid = fork do + sleep 1 # wait for client "SELECT sleep(2)" query to start + Process.kill(:USR1, Process.ppid) + sleep # wait for explicit kill to prevent GC disconnect + end + @client.query("SELECT sleep(2)") + mark[:END] = Time.now + mark.include?(:USR1).should be_true + (mark[:USR1] - mark[:START]).should >= 1 + (mark[:USR1] - mark[:START]).should < 1.1 + (mark[:END] - mark[:USR1]).should > 0.9 + (mark[:END] - mark[:START]).should >= 2 + (mark[:END] - mark[:START]).should < 2.1 + Process.kill(:TERM, pid) + Process.waitpid2(pid) + ensure + trap(:USR1, 'DEFAULT') + end + end if RUBY_PLATFORM !~ /mingw|mswin/ end it "should respond to #escape" do