I created 100 identical pgagent jobs, with one step that simply does "SELECT pg_sleep(10)". I then forced them all to run immediately, with "UPDATE pgagent.pga_job SET jobnextrun=now();". pgagent crashed.

What happened is that the when all those jobs are launched at the same time, the server ran into the max_connections limit, and pgagent didn't handle that too well. JobThread::JobThread constructor does not check for NULL result from DBConn::Get(), and passes a NULL connection to Job::Job, which tries to reference it, leading to a segfault.

I propose the attached patch.

--
  Heikki Linnakangas
  EnterpriseDB   http://www.enterprisedb.com
diff --git a/job.cpp b/job.cpp
index 6361e7c..defae70 100644
--- a/job.cpp
+++ b/job.cpp
@@ -351,11 +351,13 @@ JobThread::JobThread(const wxString &jid)
 	jobid = jid;
 
 	DBconn *threadConn = DBconn::Get(DBconn::GetBasicConnectString(), serviceDBname);
-	job = new Job(threadConn, jobid);
-
-	if (job->Runnable())
-		runnable = true;
+	if (threadConn)
+	{
+		job = new Job(threadConn, jobid);
 
+		if (job->Runnable())
+			runnable = true;
+	}
 }
 
 
diff --git a/pgAgent.cpp b/pgAgent.cpp
index ce15e08..2872a75 100644
--- a/pgAgent.cpp
+++ b/pgAgent.cpp
@@ -109,6 +109,11 @@ int MainRestartLoop(DBconn *serviceConn)
 					jt->Run();
 					foundJobToExecute = true;
 				}
+				else
+				{
+					// A thread object that's started will destroy itself when it's finished, but one that never starts we'll have to destory ourselves.
+					delete jt;
+				}
 				res->MoveNext();
 
 			}
-- 
Sent via pgadmin-hackers mailing list (pgadmin-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgadmin-hackers

Reply via email to