3f9cadd805b5a6bf628aacff019a5b4fd7fac841
max
  Wed Mar 5 00:48:05 2014 -0800
fixing crash at first startup of new mirror by phoneHome, refs #148
diff --git src/hg/lib/web.c src/hg/lib/web.c
index 094ef56..1a676e3 100644
--- src/hg/lib/web.c
+++ src/hg/lib/web.c
@@ -16,46 +16,50 @@
 #include "hubConnect.h"
 #include "search.h"
 #ifndef GBROWSE
 #include "axtInfo.h"
 #include "wikiLink.h"
 #include "googleAnalytics.h"
 #include "jsHelper.h"
 #endif /* GBROWSE */
 /* phoneHome business */
 #include <utime.h>
 #include <htmlPage.h>
 #include <signal.h>
 #include "geoMirror.h"
 #include <regex.h>
 #include "trackHub.h"
+#include <unistd.h>
 /* phoneHome business */
 
 
 /* flag that tell if the CGI header has already been outputed */
 boolean webHeadAlreadyOutputed = FALSE;
 /* flag that tell if text CGI header hsa been outputed */
 boolean webInTextMode = FALSE;
 static char *dbCgiName = "db";
 static char *orgCgiName = "org";
 static char *cladeCgiName = "clade";
 static char *extraStyle = NULL;
 
 /* globals: a cart and db for use in error handlers. */
 static struct cart *errCart = NULL;
 static char *errDb = NULL;
 
+// forward declaration to keep function ordering
+static void phoneHome();
+
 void textVaWarn(char *format, va_list args)
 {
 vprintf(format, args);
 puts("\n");
 }
 
 void softAbort()
 {
 exit(0);
 }
 
 void webPushErrHandlers(void)
 /* Push warn and abort handler for errAbort(). */
 {
 if (webInTextMode)
@@ -885,44 +889,48 @@
 	time_t now = time(NULL);
 	char *localTime;
 	extern char *tzname[2];
 	struct tm *tm = localtime(&now);
 	localTime = sqlUnixTimeToDate(&now,FALSE); /* FALSE == localtime */
 	fprintf(f, "%s, %s, %s %s, %s\n", scriptName, ip, localTime,
 	    tm->tm_isdst ? tzname[1] : tzname[0], trashFile);
 	fclose(f);
 	chmod(trashFile, 0666);
 	pid_t pid0 = fork();
 	if (0 == pid0)	/* in child */
 	    {
 	    close(STDOUT_FILENO); /* do not hang up Apache finish for parent */
 	    expireSeconds = 0;	/* no error message from this exit */
 	    (void) signal(SIGALRM, cgiApoptosis);
-	    (void) alarm(6);	/* timeout here in 6 seconds */
+	    (void) alarm(30);	/* timeout here in 30 seconds */
 #include "versionInfo.h"
 	    char url[1024];
             char *browserName = "browser.v";
             if (hHostHasPrefix("browserbox"))
                 browserName = "browserBox.v";
 
 	    safef(url, sizeof(url), "%s%s%s%s%s%s%s", "http://",
 	"genomewiki.", "ucsc.edu/", "cgi-bin/useCount?", "version=", browserName,
 		CGI_VERSION);
 
-	    /* 6 second alarm will exit this page fetch if it does not work */
+	    /* 30 second alarm will exit this page fetch if it does not work */
 	    (void) htmlPageGetWithCookies(url, NULL); /* ignore return */
 
+            // make sure we don't exit if we complete before 30 seconds, as
+            // otherwise we will close the mysql connections that the parent is
+            // still using
+            sleep(30); 
             exit(0);
             }	/* child of fork has done exit(0) normally or via alarm */
 	}		/* trash file open OK */
     if (expireSeconds > 0)
 	{
 	(void) signal(SIGALRM, cgiApoptosis);
 	(void) alarm(expireSeconds);	/* CGI timeout */
 	}
     }			/* an actual CGI binary */
 }			/* phoneHome()	*/
 /* phoneHome business */
 
 void getDbGenomeClade(struct cart *cart, char **retDb, char **retGenome,
 		      char **retClade, struct hash *oldVars)
 /* Examine CGI and cart variables to determine which db, genome, or clade