<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -231,10 +231,14 @@ function spawn_or_use_existing(app_root):
 			count++
 			active++
 	else:
-		# There are no apps for this app root.
-		wait until
-		  (active &lt; max) and
-		  (max_per_app == 0 or app_instance_count[app_root] &lt; max_per_app)
+		# There are no apps for this app root. Wait until there's at
+		# least 1 idle instance, or until there's an empty slot in the
+		# pool, then restart this function. Restarting is necessary,
+		# because after waiting and reacquiring the lack, some other
+		# thread might already have spawned instances for this app root.
+		if (active &gt;= max):
+			wait until _active_ has changed
+			goto beginning of function
 		# FIXME: there's a possible concurrency issue here. After
 		# waiting and having reacquired the lock, the state might have
 		# been completely changed, and there may now exist an instance</diff>
      <filename>doc/ApplicationPool algorithm.txt</filename>
    </modified>
    <modified>
      <diff>@@ -449,11 +449,9 @@ private:
 					activeOrMaxChanged.notify_all();
 				}
 			} else {
-				while (!(
-					active &lt; max &amp;&amp;
-					(maxPerApp == 0 || appInstanceCount[appRoot] &lt; maxPerApp)
-				)) {
+				if (active &gt;= max) {
 					activeOrMaxChanged.wait(l);
+					goto beginning_of_function;
 				}
 				if (count == max) {
 					container = inactiveApps.front();
@@ -654,6 +652,7 @@ public:
 		appInstanceCount.clear();
 		count = 0;
 		active = 0;
+		activeOrMaxChanged.notify_all();
 	}
 	
 	virtual void setMaxIdleTime(unsigned int seconds) {</diff>
      <filename>ext/apache2/StandardApplicationPool.h</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>375f188a0410ef392f853fc6855b08d87f03d513</id>
    </parent>
  </parents>
  <author>
    <name>Hongli Lai (Phusion)</name>
    <email>hongli@phusion.nl</email>
  </author>
  <url>http://github.com/FooBarWidget/passenger/commit/1ad9d7930b4a91895c550579306f2369495e78b3</url>
  <id>1ad9d7930b4a91895c550579306f2369495e78b3</id>
  <committed-date>2008-12-05T02:10:33-08:00</committed-date>
  <authored-date>2008-12-05T02:10:33-08:00</authored-date>
  <message>Backport an ApplicationPool race condition and invariant violation fix.

Some people reported that 'count' can go over 'max'. This is the cause,
I suspect.</message>
  <tree>234662eb6a489f368d2c12cf1cd5521023141a15</tree>
  <committer>
    <name>Hongli Lai (Phusion)</name>
    <email>hongli@phusion.nl</email>
  </committer>
</commit>
