c9f42961046e744998bbaf939cfd3e7a37fd117f
max
  Tue Oct 14 02:58:59 2025 -0700
changing how hgcentral connection is treated when an API key is supplied, open a connection and close it right afterwards, to make sure that no sleeping CGIs clog mysql connections, refs #36498

diff --git src/hg/lib/hubSpaceKeys.c src/hg/lib/hubSpaceKeys.c
index 82a1bef0ac9..8cc29daee3e 100644
--- src/hg/lib/hubSpaceKeys.c
+++ src/hg/lib/hubSpaceKeys.c
@@ -114,25 +114,33 @@
 void hubSpaceKeysOutput(struct hubSpaceKeys *el, FILE *f, char sep, char lastSep) 
 /* Print out hubSpaceKeys.  Separate fields with sep. Follow last field with lastSep. */
 {
 if (sep == ',') fputc('"',f);
 fprintf(f, "%s", el->userName);
 if (sep == ',') fputc('"',f);
 fputc(sep,f);
 if (sep == ',') fputc('"',f);
 fprintf(f, "%s", el->apiKey);
 if (sep == ',') fputc('"',f);
 fputc(lastSep,f);
 }
 
 /* -------------------------------- End autoSql Generated Code -------------------------------- */
 
-char *userNameForApiKey(char *apiKey)
-/* Return userName associated with apiKey else NULL */
+char *userNameForApiKey(struct sqlConnection *conn, char *apiKey)
+/* Return userName associated with apiKey else NULL. If conn is NULL, will create a connection and free it. */
 {
 char *tableName = cfgOptionDefault("authTableName", AUTH_TABLE_DEFAULT);
-struct sqlConnection *conn = hConnectCentral();
+
+boolean doClose = FALSE;
+if (conn == NULL)
+    {
+    conn = hConnectCentral();
+    doClose = TRUE;
+    }
+
 struct dyString *query = sqlDyStringCreate("select userName from %s where apiKey = '%s'", tableName, apiKey);
 char *userName = sqlQuickString(conn, dyStringCannibalize(&query));
+if (doClose)
     hDisconnectCentral(&conn);
 return userName;
 }