From 2071a83d893946df808c4e07fa6c06f22a711fd0 Mon Sep 17 00:00:00 2001
From: Danny Auble <da@llnl.gov>
Date: Wed, 12 Jul 2006 23:40:49 +0000
Subject: [PATCH] svn merge -r8546:8549
 https://eris.llnl.gov/svn/slurm/branches/slurm-1.1

---
 NEWS                         |  6 ++++++
 src/api/step_client_io.c     | 23 ++++++++++++++++++++++-
 src/common/env.c             |  9 ++++-----
 src/slurmd/slurmstepd/mgr.c  |  6 +++++-
 src/slurmd/slurmstepd/task.c |  3 +++
 src/srun/allocate.c          | 36 +++++++++++++++++++++++-------------
 src/srun/srun.c              |  2 +-
 7 files changed, 64 insertions(+), 21 deletions(-)

diff --git a/NEWS b/NEWS
index 97e575996af..687ae9a190a 100644
--- a/NEWS
+++ b/NEWS
@@ -216,6 +216,12 @@ documents those changes that are of interest to users and admins.
     srun under a bash shell in an emacs *shell* buffer.
  -- Fix srun race condition that occasionally causes segfaults at shutdown
  -- Fix obscure locking issues in log.c code.
+ -- Explicitly close IO related sockets.  If an srun gets "stuck", possibly
+    because of unkillable tasks in its job step, it will not hold many TCP
+    sockets in the CLOSE_WAIT state.
+ -- Increase the SLURM protocol timeout from 5 seconds to 10 seconds.
+    (In 1.2 there will be a slurm.conf parameter for this, rather than having
+    it hardcoded.)
 
 * Changes in SLURM 1.0.14
 =========================
diff --git a/src/api/step_client_io.c b/src/api/step_client_io.c
index 7dfb750b860..8096c3e429b 100644
--- a/src/api/step_client_io.c
+++ b/src/api/step_client_io.c
@@ -182,6 +182,10 @@ _listening_socket_readable(eio_obj_t *obj)
 {
 	debug3("Called _listening_socket_readable");
 	if (obj->shutdown == true) {
+		if (obj->fd != -1) {
+			close(obj->fd);
+			obj->fd = -1;
+		}
 		debug2("  false, shutdown");
 		return false;
 	}
@@ -257,7 +261,18 @@ _server_readable(eio_obj_t *obj)
 		return true;	
 	}
 
-	debug4("  false");
+	if (obj->shutdown) {
+		if (obj->fd != -1) {
+			close(obj->fd);
+			obj->fd = -1;
+			s->in_eof = true;
+			s->out_eof = true;
+		}
+		debug3("  false, shutdown");
+		return false;
+	}
+
+	debug3("  false");
 	return false;
 }
 
@@ -280,7 +295,10 @@ _server_read(eio_obj_t *obj, List objs)
 		n = io_hdr_read_fd(obj->fd, &s->header);
 		if (n <= 0) { /* got eof or error on socket read */
 			debug3(  "got eof or error on _server_read header");
+			close(obj->fd);
+			obj->fd = -1;
 			s->in_eof = true;
+			s->out_eof = true;
 			list_enqueue(s->cio->free_outgoing, s->in_msg);
 			s->in_msg = NULL;
 			return SLURM_SUCCESS;
@@ -316,7 +334,10 @@ _server_read(eio_obj_t *obj, List objs)
 		}
 		if (n <= 0) { /* got eof or unhandled error */
 			debug3(  "got eof on _server_read body");
+			close(obj->fd);
+			obj->fd = -1;
 			s->in_eof = true;
+			s->out_eof = true;
 			list_enqueue(s->cio->free_outgoing, s->in_msg);
 			s->in_msg = NULL;
 			return SLURM_SUCCESS;
diff --git a/src/common/env.c b/src/common/env.c
index 8636912ce36..71ed7f4d85f 100644
--- a/src/common/env.c
+++ b/src/common/env.c
@@ -254,9 +254,8 @@ int setup_env(env_t *env)
 		error("Unable to set SLURM_CPUS_PER_TASK");
 		rc = SLURM_FAILURE;
 	} 
-
-	if (env->distribution 
-	    && env->distribution != SLURM_DIST_UNKNOWN) {
+	
+	if (env->distribution != SLURM_DIST_UNKNOWN) {
 		switch(env->distribution) {
 		case SLURM_DIST_CYCLIC:
 			dist = "cyclic";
@@ -268,14 +267,14 @@ int setup_env(env_t *env)
 			dist = "arbitrary";
 			break;
 		default:
-			dist = "unknown";
+			dist = "cyclic";
 		}
 		
 		if (setenvf(&env->env, "SLURM_DISTRIBUTION", "%s", dist)) {
 			error("Can't set SLURM_DISTRIBUTION env variable");
 			rc = SLURM_FAILURE;
 		}
-	}
+	} 
 	
 	if (env->cpu_bind_type) {
 		int setstat = 0;
diff --git a/src/slurmd/slurmstepd/mgr.c b/src/slurmd/slurmstepd/mgr.c
index 48a8002bea6..e1d4e365d77 100644
--- a/src/slurmd/slurmstepd/mgr.c
+++ b/src/slurmd/slurmstepd/mgr.c
@@ -1095,12 +1095,16 @@ _wait_for_any_task(slurmd_job_t *job, bool waitflag)
 			job->envtp->env = job->env;
 			job->envtp->procid = job->task[i]->gtid;
 			job->envtp->localid = job->task[i]->id;
+			
+			/* need to take this out in 1.2 */
+			job->envtp->distribution = SLURM_DIST_UNKNOWN;
 			setup_env(job->envtp);
 			job->env = job->envtp->env;
 			if (job->task_epilog) {
 				run_script("user task_epilog", 
 					   job->task_epilog, 
-					   job->jobid, job->uid, 2, job->env);
+					   job->jobid, job->uid, 
+					   2, job->env);
 			}
 			if (conf->task_epilog) {
 				char *my_epilog;
diff --git a/src/slurmd/slurmstepd/task.c b/src/slurmd/slurmstepd/task.c
index 6a3dbb97cc8..aa1d3198e3c 100644
--- a/src/slurmd/slurmstepd/task.c
+++ b/src/slurmd/slurmstepd/task.c
@@ -272,7 +272,10 @@ exec_task(slurmd_job_t *job, int i, int waitfd)
 	job->envtp->mem_bind = xstrdup(job->mem_bind);
 	job->envtp->mem_bind_type = job->mem_bind_type;
 
+	/* need to take this out in 1.2 */
+	job->envtp->distribution = SLURM_DIST_UNKNOWN;
 	setup_env(job->envtp);
+	
 	job->env = job->envtp->env;
 	job->envtp->env = NULL;
 	xfree(job->envtp->task_count);
diff --git a/src/srun/allocate.c b/src/srun/allocate.c
index 1c6f75b31c4..57dddc24bda 100644
--- a/src/srun/allocate.c
+++ b/src/srun/allocate.c
@@ -74,7 +74,9 @@ allocate_test(void)
 {
 	int rc;
 	job_desc_msg_t *j = job_desc_msg_create_from_opts (NULL);
-
+	if(!j)
+		return SLURM_ERROR;
+	
 	rc = slurm_job_will_run(j);
 	job_desc_msg_destroy(j);
 	return rc;
@@ -89,7 +91,9 @@ allocate_nodes(void)
 	sigset_t oset;
 	resource_allocation_response_msg_t *resp = NULL;
 	job_desc_msg_t *j = job_desc_msg_create_from_opts (NULL);
-
+	if(!j)
+		return NULL;
+	
 	oquitf = xsignal(SIGQUIT, _intr_handler);
 	ointf  = xsignal(SIGINT,  _intr_handler);
 	otermf = xsignal(SIGTERM, _intr_handler);
@@ -424,6 +428,12 @@ job_desc_msg_create_from_opts (char *script)
 			}
 		}
 	}
+	if(opt.distribution == SLURM_DIST_ARBITRARY
+	   && !j->req_nodes) {
+		error("With Arbitrary distribution you need to "
+		      "specify a nodelist or hostfile with the -w option");
+		return NULL;
+	}
 	j->exc_nodes      = opt.exc_nodes;
 	j->partition      = opt.partition;
 	j->min_nodes      = opt.min_nodes;
@@ -541,25 +551,25 @@ _step_req_create(srun_job_t *j)
 	r->name       = xstrdup(opt.job_name);
 	r->relative   = false;      /* XXX fix this oneday */
 	
-	/* CJM - why are "UNKNOWN" and "default" behaviours different? */
-	/*       why do we even HAVE the SLURM_DIST_UNKNOWN state?? */
 	switch (opt.distribution) {
-	case SLURM_DIST_UNKNOWN:
-		r->task_dist = (opt.nprocs <= j->nhosts) ? SLURM_DIST_CYCLIC
-			                                 : SLURM_DIST_BLOCK;
-		break;
 	case SLURM_DIST_CYCLIC:
 		r->task_dist = SLURM_DIST_CYCLIC;
 		break;
+	case SLURM_DIST_BLOCK:
+		r->task_dist = SLURM_DIST_BLOCK;
+		break;
 	case SLURM_DIST_ARBITRARY:
 		r->task_dist = SLURM_DIST_ARBITRARY;
 		break;
-	case SLURM_DIST_BLOCK:
+	case SLURM_DIST_UNKNOWN:
 	default:
-		r->task_dist = SLURM_DIST_BLOCK;
+		r->task_dist = (opt.nprocs <= j->nhosts) ? SLURM_DIST_CYCLIC
+			                                 : SLURM_DIST_BLOCK;
 		break;
 	}
-
+	/* make sure we set the env correctly */
+	opt.distribution = r->task_dist;
+	
 	if (slurmctld_comm_addr.port) {
 		r->host = xstrdup(slurmctld_comm_addr.hostname);
 		r->port = slurmctld_comm_addr.port;
@@ -591,11 +601,11 @@ create_job_step(srun_job_t *job,
 	job->step_layout = step_layout_create(alloc_resp, resp, req);
 	if(!job->step_layout) {
 		error("step_layout not created correctly");
-		return 1;
+		return -1;
 	}
 	if(task_layout(job->step_layout) != SLURM_SUCCESS) {
 		error("problem with task layout");
-		return 1;
+		return -1;
 	}
 	
 	/*
diff --git a/src/srun/srun.c b/src/srun/srun.c
index 39609a08420..2718aa0c5b9 100644
--- a/src/srun/srun.c
+++ b/src/srun/srun.c
@@ -392,7 +392,7 @@ int srun(int ac, char **av)
 	
 	if (slurm_mpi_exit () < 0)
 		; /* eh, ignore errors here */
-		
+
 	_run_srun_epilog(job);
 
 	/* 
-- 
GitLab