Index: package_branches/invirt-web/ajaxterm-rebased/code/main.py
===================================================================
--- package_branches/invirt-web/ajaxterm-rebased/code/main.py	(revision 2755)
+++ package_branches/invirt-web/ajaxterm-rebased/code/main.py	(revision 2756)
@@ -1,4 +1,6 @@
 #!/usr/bin/python
 """Main CGI script for web interface"""
+
+from __future__ import with_statement
 
 import base64
@@ -11,4 +13,5 @@
 import sha
 import sys
+import threading
 import time
 import urllib
@@ -392,4 +395,5 @@
         atmulti = ajaxterm.Multiplex()
         atsessions = {}
+        atsessions_lock = threading.Lock()
 
         @cherrypy.expose
@@ -411,22 +415,23 @@
         def at(self, machine_id, k=None, c=0, force=0):
             machine = validation.Validate(cherrypy.request.login, cherrypy.request.state, machine_id=machine_id).machine
-            if machine_id in self.atsessions:
-                term = self.atsessions[machine_id]
-            else:
-                print >>sys.stderr, "spawning new session for terminal to ",machine_id
-                term = self.atsessions[machine_id] = self.atmulti.create(
-                    ["ssh", "-e","none", "-l", machine.name, config.console.hostname]
-                    )
-            if k:
-                self.atmulti.proc_write(term,k)
-            time.sleep(0.002)
-            dump=self.atmulti.dump(term,c,int(force))
-            cherrypy.response.headers['Content-Type']='text/xml'
-            if isinstance(dump,str):
-                return dump
-            else:
-                print "Removing session for", machine_id
-                del self.atsessions[machine_id]
-                return '<?xml version="1.0"?><idem></idem>'
+            with self.atsessions_lock:
+                if machine_id in self.atsessions:
+                    term = self.atsessions[machine_id]
+                else:
+                    print >>sys.stderr, "spawning new session for terminal to ",machine_id
+                    term = self.atsessions[machine_id] = self.atmulti.create(
+                        ["ssh", "-e","none", "-l", machine.name, config.console.hostname]
+                        )
+                if k:
+                    self.atmulti.proc_write(term,k)
+                time.sleep(0.002)
+                dump=self.atmulti.dump(term,c,int(force))
+                cherrypy.response.headers['Content-Type']='text/xml'
+                if isinstance(dump,str):
+                    return dump
+                else:
+                    print "Removing session for", machine_id
+                    del self.atsessions[machine_id]
+                    return '<?xml version="1.0"?><idem></idem>'
 
     machine = MachineView()
