I've implemented the infinite loop-catching suggestion by instrumenting 
the Python code.  I haven't adopted the techniques from the article Ian 
found because I'm lazy, and because my users are silly, not evil.  It 
took a bit of buggering about but the result is fairly simple.  Read on 
if you're interested.

1. I figure that the only Python construct that can easily loop 
infinitely is the while statement.  (for statements could also blow out, 
and I'll fix that if it becomes a problem.)

2. I can convert the code to be evaluated into a parse tree as follows:
   asList = parser.suite(code).tolist(True)  # We want original line 
numbers to be preserved

3. I can instrument this code by adding a call to loopCheck() at the 
beginning of each while loop: (This code assumes you're familiar with 
the structure of Python parse trees.)
   # Construct the parse tree for the call to loopCheck()
   _callLoopCheck = parser.suite('loopCheck()').tolist()[1]

   def _addLoopChecks(l):
     """Takes a parse tree and adds calls to loopCheck() to every loop."""
     if type(l) is list:
       for subL in l:
         _addLoopChecks(subL)
       if type(l[1]) is list:
         if symbol.sym_name[l[0]] == 'while_stmt':
           m = l[4]
           assert symbol.sym_name[m[0]] == 'suite'
           i = 1
           while type(m[i][1]) is not list:
             i = i + 1
           m.insert(i, _callLoopCheck)

4. Then I can recompile the patched parse tree:
   compiled = parser.sequence2ast(asList).compile()

5. And finally execute it:
   class _LoopCheck:
     def __init__(self):
      self._loopCount = 0
    def loopCheck(self):
      self._loopCount = self._loopCount + 1
      if self._loopCount == 500000:
        raise Exception, "Code has gone into an infinite loop."
   exec compiled in { 'loopCheck': _LoopCheck().loopCheck }

Thanks for all the help.


   Oliver

--
Richard Wheeler wrote:
> Ian Bicking <[EMAIL PROTECTED]> writes:
>   
>> This recipe might be of use: 
>> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496746
>>
>> I don't believe it is threadsafe, but I'm not sure.  I think it needs to 
>> run the code in the main thread, then puts an alarm in a subthread that 
>> interrupts the main thread if it takes too long.  I don't think other 
>> threads can be interrupted that way.
>>     
>
> That is a very good find Ian.  It is very similar to what I am suggesting.  
> In 
> my worker threads we used event object for signaling.  The main thread joins 
> the worker thread to set the event which worker thread then detects it is to 
> end.  Using that mechanism you would avoid the issue of how to kill the 
> thread 
> since the thread ends it self based on the event object allowing it to break 
> out of the loop.
>
> Since the recipe has the parsing logic already in place it shouldn't take 
> much 
> tweaking to scan for loop keywords and insert code.  I would log those code 
> fragments so that if the parsing logic misses a loop, you could examine the 
> code fragment and further refine the parsing logic.  Eventually it would 
> become bullet proof.
>
> Using this mechanism you can enforce the necessary logic to avoid infinite 
> loops.
>
> Oliver to solve your issue it is going to take some work given the nature of 
> your system that you have described.  With that recipe you have a lot of the 
> work already done.  If you are not sure how to setup worker threads and use 
> event objects I could post a code fragment here.  For that matter I think 
> ASPN 
> already has recipes for those concepts.
>
>
>  
>
>
>
>
>
> All the advantages of Linux Managed Hosting--Without the Cost and Risk!
> Fully trained technicians. The highest number of Red Hat certifications in
> the hosting industry. Fanatical Support. Click to learn more
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642
> _______________________________________________
> Webware-discuss mailing list
> Webware-discuss@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/webware-discuss
>
>   

All the advantages of Linux Managed Hosting--Without the Cost and Risk!
Fully trained technicians. The highest number of Red Hat certifications in
the hosting industry. Fanatical Support. Click to learn more
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642
_______________________________________________
Webware-discuss mailing list
Webware-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to