[Ironpython-users] struct pack / unpack not thread safe?

John Dickinson jhcd.jhcd at gmail.com
Thu Sep 20 16:48:34 CEST 2012


I have at least two threads that end up calling struct.pack and
struct.unpack, and regularly get two exceptions: "SystemError: Object
reference not set to an instance of an object." and "SystemError: The
LinkedList node does not belong to current LinkedList.". The following code
reproduces the problem:

import threading
import struct
import time
import traceback
import sys

class TestThread(threading.Thread):

    def __init__(self):
        super(TestThread, self).__init__()
        self.setDaemon(True)

    def run(self):
        while True:
            try:
                struct.unpack('!H', '\x4f\x00')
                struct.pack('H', 20224)
                time.sleep(0.005)
            except Exception, e:
                print(str(e))
                traceback.print_exception(*sys.exc_info())

if __name__ == '__main__':

    thread_1 = TestThread()
    thread_2 = TestThread()

    thread_1.start()
    thread_2.start()

    while True:
        print(".")
        time.sleep(1)

(adding more TestThreads makes the error occur more regularly). Running the
same code in CPython (2.7) I get no errors (presumably because of the GIL).
It seems that struct isn't thread safe - is this a bug? I'm running
IronPython "2.7.3 (2.7.0.40) on .NET 4.0.30319.269 (32-bit)" on Windows 7.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20120920/38261d66/attachment.html>


More information about the Ironpython-users mailing list