threading and iterator crashing interpreter

Janto Dreijer jantod at gmail.com
Sun Mar 11 06:32:04 EDT 2007


I have been having problems with the Python 2.4 and 2.5 interpreters
on both Linux and Windows crashing on me. Unfortunately it's rather
complex code and difficult to pin down the source.

So I've been trying to reduce the code. In the process it's started to
crash in different ways. I'm not sure if any of it is related. The
following is only crashing Python 2.5 (r25:51908, Sep 19 2006,
09:52:17) [MSC v.1310 32 bit (Intel)] on win32 in two different(?)
ways.

====================

Using the login1() function will throw a bunch of exceptions the most
interesting of which is:

Exception in thread Thread-34:
Traceback (most recent call last):
  File "C:\Python25\lib\threading.py", line 460, in __bootstrap
    self.run()
  File "C:\Python25\lib\threading.py", line 440, in run
    self.__target(*self.__args, **self.__kwargs)
  File "Copy of scratchpad.py", line 20, in login1
    random.choice(System.sessions).session_iter.next()
RuntimeError: instance.__dict__ not accessible in restricted mode

I'm not concerned with the Python exceptions, but about 40% of the
time it will launch the Visual Studio JIT Debugger with an "Unhandled
exception at 0x1e03973e in pythonw.exe: 0xC0000005: Access violation
reading location 0x00002024."

=================

Using the login2() function will sometimes (about 30% of the time)
cause the following to be printed out:
"""
Unhandled exception in thread started by
Error in sys.excepthook:

Original exception was:
"""

===============

Here's the code. Will it be easier to find the problem if I tried
compiling Python myself?

from threading import Thread
import random

def session_iter(f):
        # second interpreter
        while 1:
                yield len(f.__dict__)

class Session:
        def __init__(self):
                self.session_iter = session_iter(self)

class System:
        sessions = []

def login1():
        # first interpreter
        System.sessions.append(Session())
        while 1:
                random.choice(System.sessions).session_iter.next()

def login2():
        # first interpreter
        System.sessions.append(Session())
        System.sessions.pop().session_iter.next()

# main interpreter
threads = [Thread(target=login1) for t in range(2000)]
for thread in threads:
        thread.start()




More information about the Python-list mailing list