From bca0e7f14e1d8a7405eaf48aaeb9ff9530fb28b2 Mon Sep 17 00:00:00 2001 From: Nikolay Vashchenko Date: Thu, 2 Feb 2017 04:05:04 +0300 Subject: [PATCH 1/3] Handling race condition, caused by MRI global interpretation lock on threads, that make system calls. --- lib/puma/server.rb | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/lib/puma/server.rb b/lib/puma/server.rb index 8516463988..0a35a754f8 100644 --- a/lib/puma/server.rb +++ b/lib/puma/server.rb @@ -892,35 +892,31 @@ def graceful_shutdown end end - # Stops the acceptor thread and then causes the worker threads to finish - # off the request queue before finally exiting. - # - def stop(sync=false) + def notify_safely(message) begin - @notify << STOP_COMMAND + @notify << message rescue IOError # The server, in another thread, is shutting down + rescue RuntimeError => e + raise e unless e.message.include?('frozen') end + end + # Stops the acceptor thread and then causes the worker threads to finish + # off the request queue before finally exiting. + + def stop(sync=false) + notify_safely(STOP_COMMAND) @thread.join if @thread && sync end def halt(sync=false) - begin - @notify << HALT_COMMAND - rescue IOError - # The server, in another thread, is shutting down - end - + notify_safely(HALT_COMMAND) @thread.join if @thread && sync end def begin_restart - begin - @notify << RESTART_COMMAND - rescue IOError - # The server, in another thread, is shutting down - end + notify_safely(RESTART_COMMAND) end def fast_write(io, str) From 6ff43f9339f710f973ffa921e010a690ccbd1376 Mon Sep 17 00:00:00 2001 From: Nikolay Vashchenko Date: Mon, 6 Feb 2017 18:44:31 +0300 Subject: [PATCH 2/3] improving exception message handling --- lib/puma/server.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/puma/server.rb b/lib/puma/server.rb index 0a35a754f8..8a4f0e2a0d 100644 --- a/lib/puma/server.rb +++ b/lib/puma/server.rb @@ -898,9 +898,10 @@ def notify_safely(message) rescue IOError # The server, in another thread, is shutting down rescue RuntimeError => e - raise e unless e.message.include?('frozen') + raise e unless e.message.include?('IOError') end end + private :notify_safely # Stops the acceptor thread and then causes the worker threads to finish # off the request queue before finally exiting. From 838716fd6b7832590e3ce58fa46bf18016e6631b Mon Sep 17 00:00:00 2001 From: Nikolay Vashchenko Date: Thu, 9 Feb 2017 22:32:49 +0300 Subject: [PATCH 3/3] adding comment --- lib/puma/server.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/puma/server.rb b/lib/puma/server.rb index 8a4f0e2a0d..7ef517c1ba 100644 --- a/lib/puma/server.rb +++ b/lib/puma/server.rb @@ -898,6 +898,8 @@ def notify_safely(message) rescue IOError # The server, in another thread, is shutting down rescue RuntimeError => e + # The server, in another thread, has been shut down during the system call + # https://github.com/puma/puma/pull/1206 raise e unless e.message.include?('IOError') end end