X-Git-Url: http://xvm.mit.edu/gitweb/invirt/packages/invirt-web.git/blobdiff_plain/0dad430c014913f47e900590cae35bf238a625e5..69d11ccb77e5d5583fa1aee318a6afaa77000826:/code/main.py
diff --git a/code/main.py b/code/main.py
index 77646c9..1f9189f 100755
--- a/code/main.py
+++ b/code/main.py
@@ -1,6 +1,8 @@
#!/usr/bin/python
"""Main CGI script for web interface"""
+from __future__ import with_statement
+
import base64
import cPickle
import cgi
@@ -10,6 +12,7 @@ import os
import random
import sha
import sys
+import threading
import time
import urllib
import socket
@@ -391,6 +394,7 @@ console will suffer artifacts.
atmulti = ajaxterm.Multiplex()
atsessions = {}
+ atsessions_lock = threading.Lock()
@cherrypy.expose
@cherrypy.tools.mako(filename="/terminal.mako")
@@ -407,26 +411,34 @@ console will suffer artifacts.
return d
@cherrypy.expose
+ @cherrypy.tools.require_POST()
+ @cherrypy.tools.gzip()
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 ''
+ 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.atmulti.create(
+ ["ssh", "-e","none", "-l", machine.name, config.console.hostname]
+ )
+ # Clear out old sessions when fd is reused
+ for key in self.atsessions:
+ if self.atsessions[key] == term:
+ del self.atsessions[key]
+ self.atsessions[machine_id] = term
+ 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 ''
machine = MachineView()