Index: fedd/federation/access.py
===================================================================
--- fedd/federation/access.py	(revision 574055e590f85dcf54b75569993e1dba1b0c7428)
+++ fedd/federation/access.py	(revision b770aa0b5b27f82398612086c0ef5e304282aa89)
@@ -1279,4 +1279,7 @@
 	configs = set(('hosts', 'ssh_pubkey', 'ssh_secretkey'))
 
+	err = None  # Any service_error generated after tmpdir is created
+	rv = None   # Return value from segment creation
+
 	try:
 	    req = req['StartSegmentRequestBody']
@@ -1307,54 +1310,58 @@
             raise service_error(service_error.internal, "Cannot create tmp dir")
 
-	sw = set()
-	for e in topo.elements:
-	    for s in getattr(e, 'software', []):
-		sw.add(s.location)
-	if len(sw) > 0:
-	    os.mkdir(softdir)
-	for s in sw:
-	    get_url(s, certfile, softdir)
-
-	for a in attrs:
-	    if a['attribute'] in configs:
-		get_url(a['value'], certfile, tmpdir)
-	    if a['attribute'] == 'ssh_pubkey':
-		pubkey_base = a['value'].rpartition('/')[2]
-	    if a['attribute'] == 'ssh_secretkey':
-		secretkey_base = a['value'].rpartition('/')[2]
-	    if a['attribute'] == 'experiment_name':
-		ename = a['value']
-
-	proj = None
-	user = None
-	self.state_lock.acquire()
-	if self.allocation.has_key(aid):
-	    proj = self.allocation[aid].get('project', None)
-	    if not proj: 
-		proj = self.allocation[aid].get('sproject', None)
-	    user = self.allocation[aid].get('user', None)
-	    self.allocation[aid]['experiment'] = ename
-	    self.write_state()
-	self.state_lock.release()
-
-	if not proj:
-	    raise service_error(service_error.internal, 
-		    "Can't find project for %s" %aid)
-
-	if not user:
-	    raise service_error(service_error.internal, 
-		    "Can't find creation user for %s" %aid)
-
-	expfile = "%s/experiment.tcl" % tmpdir
-
-	self.generate_portal_configs(topo, pubkey_base, 
-		secretkey_base, tmpdir, master)
-	self.generate_ns2(topo, expfile, 
-		"/proj/%s/software/%s/" % (proj, ename), master)
-	starter = self.start_segment(keyfile=self.ssh_privkey_file, 
-		debug=self.create_debug)
-	rv = starter(self, ename, proj, user, expfile, tmpdir)
-
-        self.log.debug("[StartSegment]: removing %s" % tmpdir)
+	# Try block alllows us to clean up temporary files.
+	try:
+	    sw = set()
+	    for e in topo.elements:
+		for s in getattr(e, 'software', []):
+		    sw.add(s.location)
+	    if len(sw) > 0:
+		os.mkdir(softdir)
+	    for s in sw:
+		get_url(s, certfile, softdir)
+
+	    for a in attrs:
+		if a['attribute'] in configs:
+		    get_url(a['value'], certfile, tmpdir)
+		if a['attribute'] == 'ssh_pubkey':
+		    pubkey_base = a['value'].rpartition('/')[2]
+		if a['attribute'] == 'ssh_secretkey':
+		    secretkey_base = a['value'].rpartition('/')[2]
+		if a['attribute'] == 'experiment_name':
+		    ename = a['value']
+
+	    proj = None
+	    user = None
+	    self.state_lock.acquire()
+	    if self.allocation.has_key(aid):
+		proj = self.allocation[aid].get('project', None)
+		if not proj: 
+		    proj = self.allocation[aid].get('sproject', None)
+		user = self.allocation[aid].get('user', None)
+		self.allocation[aid]['experiment'] = ename
+		self.write_state()
+	    self.state_lock.release()
+
+	    if not proj:
+		raise service_error(service_error.internal, 
+			"Can't find project for %s" %aid)
+
+	    if not user:
+		raise service_error(service_error.internal, 
+			"Can't find creation user for %s" %aid)
+
+	    expfile = "%s/experiment.tcl" % tmpdir
+
+	    self.generate_portal_configs(topo, pubkey_base, 
+		    secretkey_base, tmpdir, master)
+	    self.generate_ns2(topo, expfile, 
+		    "/proj/%s/software/%s/" % (proj, ename), master)
+	    starter = self.start_segment(keyfile=self.ssh_privkey_file, 
+		    debug=self.create_debug)
+	    rv = starter(self, ename, proj, user, expfile, tmpdir)
+	except service_error, e:
+	    err = e
+
+	self.log.debug("[StartSegment]: removing %s" % tmpdir)
         # Walk up tmpdir, deleting as we go
         for path, dirs, files in os.walk(tmpdir, topdown=False):
@@ -1367,4 +1374,7 @@
 	if rv:
 	    return { 'allocID': req['allocID'] }
+	elif err:
+	    raise service_error(service_error.federant,
+		    "Swapin failed: %s" % err)
 	else:
 	    raise service_error(service_error.federant, "Swapin failed")
