<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -244,6 +244,40 @@ private:
 		return pool;
 	}
 	
+	/**
+	 * Get a session from the application pool, similar to how
+	 * ApplicationPool::Interface::get() works. This method also checks whether
+	 * the helper server has crashed. If it did then it will wait a while (for
+	 * the watchdog to the restart the helper server) and then attempt to reconnect 
+	 * to the helper server.
+	 *
+	 * @throws SystemException
+	 * @throws IOException
+	 * @throws RuntimeException
+	 * @throws SecurityExcepion
+	 */
+	Application::SessionPtr getSession(const PoolOptions &amp;options) {
+		ApplicationPool::Client *pool = getApplicationPool();
+		try {
+			return pool-&gt;get(options);
+		} catch (const SystemException &amp;e) {
+			// Maybe the helper server crashed. First wait 100 ms.
+			/* usleep(100000);
+			
+			// Then try to reconnect to the helper server for the next
+			// 5 seconds.
+			time_t begin = time(NULL);
+			while (time(NULL)) */
+			
+			//Wait between 500 and 2500 ms,
+			// then reconnect again.
+			int r = rand(); // Don't care about thread-safety of this function.
+			usleep(500000 + r % 2000000);
+			pool = getApplicationPool();
+			return pool-&gt;get(options);
+		}
+	}
+	
 	bool hasModRewrite() {
 		if (m_hasModRewrite == UNKNOWN) {
 			if (ap_find_linked_module(&quot;mod_rewrite.c&quot;)) {
@@ -551,7 +585,7 @@ private:
 				);
 				options.environmentVariables = ptr(new EnvironmentVariablesStringListCreator(r));
 				
-				session = getApplicationPool()-&gt;get(options);
+				session = getSession(options);
 				P_TRACE(3, &quot;Forwarding &quot; &lt;&lt; r-&gt;uri &lt;&lt; &quot; to PID &quot; &lt;&lt; session-&gt;getPid());
 			} catch (const SpawnException &amp;e) {
 				r-&gt;status = 500;</diff>
      <filename>ext/apache2/Hooks.cpp</filename>
    </modified>
    <modified>
      <diff>@@ -132,7 +132,7 @@ public:
 		} catch (const IOException &amp;) {
 			silentDisconnect();
 			throw;
-		} catch (const boost::thread_interrupt &amp;) {
+		} catch (const boost::thread_interrupted &amp;) {
 			silentDisconnect();
 			throw;
 		}</diff>
      <filename>ext/common/MessageClient.h</filename>
    </modified>
    <modified>
      <diff>@@ -542,11 +542,22 @@ describe &quot;Apache 2 module&quot; do
 		end
 		
 		it &quot;is restarted if it crashes&quot; do
-			get('/welcome').should =~ /Welcome to MyCook/
+			# Make sure that all Apache worker processes have connected to
+			# the helper server.
+			10.times do
+				get('/welcome').should =~ /Welcome to MyCook/
+				sleep 0.1
+			end
+			
+			# Now kill the helper server.
 			instance = PhusionPassenger::AdminTools::ServerInstance.list.first
 			Process.kill('SIGKILL', instance.helper_server_pid)
-			retry_with_time_limit(10) do
-				get('/welcome') =~ /Welcome to MyCook/
+			
+			# Each worker process should detect that the old
+			# helper server has died, and should reconnect.
+			10.times do
+				get('/welcome').should =~ /Welcome to MyCook/
+				sleep 0.1
 			end
 		end
 	end</diff>
      <filename>test/integration_tests/apache2_tests.rb</filename>
    </modified>
    <modified>
      <diff>@@ -41,9 +41,9 @@ PassengerLogLevel 1
 
 TypesConfig &quot;&lt;%= @server_root %&gt;/mime.types&quot;
 StartServers          1
-ServerLimit          50
+ServerLimit          10
 MaxRequestsPerChild  50
-MaxClients           50
+MaxClients           10
 &lt;IfModule mpm_prefork_module&gt;
 	MinSpareServers       1
 	MaxSpareServers       1</diff>
      <filename>test/stub/apache2/httpd.conf.erb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>258e4b8bf191b7f5b1ccaf7a326f5f11d9462895</id>
    </parent>
  </parents>
  <author>
    <name>Hongli Lai (Phusion)</name>
    <email>hongli@phusion.nl</email>
  </author>
  <url>http://github.com/FooBarWidget/passenger/commit/b77bf782fff5de03cbc470d358a9ed6adba6afd1</url>
  <id>b77bf782fff5de03cbc470d358a9ed6adba6afd1</id>
  <committed-date>2009-09-17T08:36:00-07:00</committed-date>
  <authored-date>2009-09-17T08:36:00-07:00</authored-date>
  <message>Automatically reconnect to helper server if it crashed.</message>
  <tree>923335aea1fdb7c76e122ef32a1aa5a280ba94e2</tree>
  <committer>
    <name>Hongli Lai (Phusion)</name>
    <email>hongli@phusion.nl</email>
  </committer>
</commit>
