[前][次][番号順一覧][スレッド一覧]

ruby-changes:39150

From: nobu <ko1@a...>
Date: Tue, 14 Jul 2015 11:20:41 +0900 (JST)
Subject: [ruby-changes:39150] nobu:r51231 (trunk): webrick/server.rb: stop immediately

nobu	2015-07-14 11:20:16 +0900 (Tue, 14 Jul 2015)

  New Revision: 51231

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=51231

  Log:
    webrick/server.rb: stop immediately
    
    * lib/webrick/server.rb (WEBrick::GenericServer#start): flush
      shutdown pipe.
    * lib/webrick/server.rb (WEBrick::GenericServer#stop): request the
      server to stop immediately by sending data via shutdown pipe.

  Modified files:
    trunk/ChangeLog
    trunk/lib/webrick/server.rb
    trunk/test/webrick/test_server.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 51230)
+++ ChangeLog	(revision 51231)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Jul 14 11:20:13 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/webrick/server.rb (WEBrick::GenericServer#start): flush
+	  shutdown pipe.
+
+	* lib/webrick/server.rb (WEBrick::GenericServer#stop): request the
+	  server to stop immediately by sending data via shutdown pipe.
+
 Mon Jul 13 23:58:08 2015  Stefano Tortarolo  <stefano.tortarolo@g...>
 
 	* lib/webrick/httpproxy.rb (WEBrick::HTTPProxyServer#do_CONNECT):
Index: lib/webrick/server.rb
===================================================================
--- lib/webrick/server.rb	(revision 51230)
+++ lib/webrick/server.rb	(revision 51231)
@@ -172,8 +172,13 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/lib/webrick/server.rb#L172
         begin
           while @status == :Running
             begin
-              if svrs = IO.select([shutdown_pipe[0], *@listeners], nil, nil, 2.0)
-                if svrs[0].include? shutdown_pipe[0]
+              sp = shutdown_pipe[0]
+              if svrs = IO.select([sp, *@listeners], nil, nil, 2.0)
+                if svrs[0].include? sp
+                  # swallow shutdown pipe
+                  buf = String.new
+                  nil while String ===
+                            sp.read_nonblock([sp.nread, 8].max, buf, exception: false)
                   break
                 end
                 svrs[0].each{|svr|
@@ -221,6 +226,8 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/lib/webrick/server.rb#L226
       if @status == :Running
         @status = :Shutdown
       end
+
+      alarm_shutdown_pipe {|f| f.write_nonblock("\0")}
     end
 
     ##
@@ -230,15 +237,7 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/lib/webrick/server.rb#L237
     def shutdown
       stop
 
-      shutdown_pipe = @shutdown_pipe # another thread may modify @shutdown_pipe.
-      if shutdown_pipe
-        if !shutdown_pipe[1].closed?
-          begin
-            shutdown_pipe[1].close
-          rescue IOError # closed by another thread.
-          end
-        end
-      end
+      alarm_shutdown_pipe {|f| f.close}
     end
 
     ##
@@ -343,6 +342,18 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/lib/webrick/server.rb#L342
       }
     end
 
+    def alarm_shutdown_pipe
+      _, pipe = @shutdown_pipe # another thread may modify @shutdown_pipe.
+      if pipe
+        if !pipe.closed?
+          begin
+            yield pipe
+          rescue IOError # closed by another thread.
+          end
+        end
+      end
+    end
+
     def cleanup_listener
       @listeners.each{|s|
         if @logger.debug?
Index: test/webrick/test_server.rb
===================================================================
--- test/webrick/test_server.rb	(revision 51230)
+++ test/webrick/test_server.rb	(revision 51231)
@@ -137,8 +137,12 @@ class TestWEBrickServer < Test::Unit::Te https://github.com/ruby/ruby/blob/trunk/test/webrick/test_server.rb#L137
         flunk "unexpected log: #{msg.inspect}"
       end
     end
+    client_thread = nil
+    wakeup = -> {client_thread.wakeup}
     warn_flunk = WEBrick::Log.new(log, WEBrick::BasicLog::WARN)
     server = WEBrick::HTTPServer.new(
+      :StartCallback => wakeup,
+      :StopCallback => wakeup,
       :BindAddress => '0.0.0.0',
       :Port => 0,
       :Logger => warn_flunk)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]