Changeset 2756 for package_branches


Ignore:
Timestamp:
Dec 21, 2009, 1:11:45 AM (14 years ago)
Author:
broder
Message:

Fix race condition in ajaxterm

File:
1 edited

Legend:

Unmodified
Added
Removed
  • package_branches/invirt-web/ajaxterm-rebased/code/main.py

    r2754 r2756  
    11#!/usr/bin/python
    22"""Main CGI script for web interface"""
     3
     4from __future__ import with_statement
    35
    46import base64
     
    1113import sha
    1214import sys
     15import threading
    1316import time
    1417import urllib
     
    392395        atmulti = ajaxterm.Multiplex()
    393396        atsessions = {}
     397        atsessions_lock = threading.Lock()
    394398
    395399        @cherrypy.expose
     
    411415        def at(self, machine_id, k=None, c=0, force=0):
    412416            machine = validation.Validate(cherrypy.request.login, cherrypy.request.state, machine_id=machine_id).machine
    413             if machine_id in self.atsessions:
    414                 term = self.atsessions[machine_id]
    415             else:
    416                 print >>sys.stderr, "spawning new session for terminal to ",machine_id
    417                 term = self.atsessions[machine_id] = self.atmulti.create(
    418                     ["ssh", "-e","none", "-l", machine.name, config.console.hostname]
    419                     )
    420             if k:
    421                 self.atmulti.proc_write(term,k)
    422             time.sleep(0.002)
    423             dump=self.atmulti.dump(term,c,int(force))
    424             cherrypy.response.headers['Content-Type']='text/xml'
    425             if isinstance(dump,str):
    426                 return dump
    427             else:
    428                 print "Removing session for", machine_id
    429                 del self.atsessions[machine_id]
    430                 return '<?xml version="1.0"?><idem></idem>'
     417            with self.atsessions_lock:
     418                if machine_id in self.atsessions:
     419                    term = self.atsessions[machine_id]
     420                else:
     421                    print >>sys.stderr, "spawning new session for terminal to ",machine_id
     422                    term = self.atsessions[machine_id] = self.atmulti.create(
     423                        ["ssh", "-e","none", "-l", machine.name, config.console.hostname]
     424                        )
     425                if k:
     426                    self.atmulti.proc_write(term,k)
     427                time.sleep(0.002)
     428                dump=self.atmulti.dump(term,c,int(force))
     429                cherrypy.response.headers['Content-Type']='text/xml'
     430                if isinstance(dump,str):
     431                    return dump
     432                else:
     433                    print "Removing session for", machine_id
     434                    del self.atsessions[machine_id]
     435                    return '<?xml version="1.0"?><idem></idem>'
    431436
    432437    machine = MachineView()
Note: See TracChangeset for help on using the changeset viewer.