Bug #47703 event scheduler can crash or hang during server shutdown
Submitted: 29 Sep 2009 8:39 Modified: 28 Sep 2010 23:36
Reporter: Shane Bester (Platinum Quality Contributor) Email Updates:
Status: Won't fix Impact on me:
None 
Category:MySQL Server: Stored Routines Severity:S1 (Critical)
Version:5.1.39 OS:Any (Windows and Linux)
Assigned to: CPU Architecture:Any

[29 Sep 2009 8:39] Shane Bester
Description:
while event scheduler was starting, running, stopping and server was shutdown I got a crash:

mysqld.exe!Event_queue::lock_data()[event_queue.cc:669]
mysqld.exe!Event_queue::recalculate_activation_times()[event_queue.cc:439]
mysqld.exe!Event_scheduler::run()[event_scheduler.cc:458]
mysqld.exe!event_scheduler_thread()[event_scheduler.cc:238]
mysqld.exe!pthread_start()[my_winthread.c:85]
mysqld.exe!_callthreadstart()[thread.c:295]
mysqld.exe!_threadstart()[thread.c:275]
kernel32.dll!BaseThreadStart()

How to repeat:
start server with --event-scheduler=1 and create an event:

drop event if exists myevent5;
delimiter $
create event myevent5 on schedule every 1 second enable do
begin
declare i int default '0';
declare continue handler for sqlexception begin end;
repeat
set global event_scheduler=0;
set global event_scheduler=1;
set i:=i+1;
until i>1000 end repeat;

end $

delimiter ;

then after couple seconds, shutdown server:
mysqladmin -uroot shutdown

Suggested fix:
first fix this crash. even without the set global... i did sometimes get a hang on shutdown of server, so there are potentially multiple bugs in this code.
[29 Sep 2009 9:01] Valeriy Kravchuk
With non-debug binaries of 5.1.39 on Windows I've got only a flood of messages:

...
090929 11:58:01 [Note] Event Scheduler: Killing the scheduler thread, thread id 9019
090929 11:58:01 [Note] Event Scheduler: Waiting for the scheduler thread to reply
090929 11:58:01 [Note] Event Scheduler: scheduler thread started with id 9019
090929 11:58:01 [Note] Event Scheduler: Stopped
090929 11:58:01 [Note] Event Scheduler: scheduler thread started with id 9020
090929 11:58:01 [Note] C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqld: Normal shutdown

090929 11:58:01 [Note] Event Scheduler: Killing the scheduler thread, thread id 9020
090929 11:58:01 [Note] Event Scheduler: Waiting for the scheduler thread to reply
090929 11:58:01 [Note] Event Scheduler: Stopped
090929 11:58:01 [Note] Event Scheduler: Purging the queue. 1 events
090929 11:58:01  InnoDB: Starting shutdown...
090929 11:58:02  InnoDB: Shutdown completed; log sequence number 0 22432690
090929 11:58:02 [Warning] Forcing shutdown of 1 plugins
090929 11:58:02 [Note] C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqld: Shutdown complete

but no crash.
[29 Sep 2009 9:06] Susanne Ebrecht
Verified as described by using Ubuntu 9.04 amd 64 and actual bzr tree of MySQL 5.1 (5.1.40-debug)
[29 Sep 2009 9:10] Susanne Ebrecht
CREATE DATABASE event_test;

USE event_test;

SET GLOBAL event_scheduler=1;

CREATE EVENT myevent5 on schedule every 1 second enable do
DELIMITER §
BEGIN
DECLARE i int default '0';
DECLARE continue handler for sqlexception begin end;
REPEAT
SET global event_scheduler=0;
SET global event_scheduler=1;
SET i:=i+1;
UNTIL i>1000 END REPEAT;
END§

Other terminal:

$ ./bin/mysqladmin shutdown
[29 Sep 2009 9:10] MySQL Verification Team
the release build is affected too. try it a few times, or log the console output to a file to speed it up. running it under valgrind saw many invalid reads of memory..
[20 Sep 2010 18:18] Konstantin Osipov
Please verify against 5.5.6, it is likely to have been fixed there.
[24 Sep 2010 19:37] Sveta Smirnova
Correct: not repeatable nor with mysql-next-mr, neither with mysql-trunk. Still repeatable with 5.1
[27 Sep 2010 18:11] Konstantin Osipov
This is fixed in 5.5. Backporting to 5.1 may introduce a regression.
Since the bug is rare, it's considered not worth the risk.
Please send it back to us if new information surfaces.
[28 Sep 2010 23:37] Omer Barnir
This is a 5.1 issue only and is fixed in 5.5.