mhostetter / galois

A performant NumPy extension for Galois fields and their applications
https://mhostetter.github.io/galois/
MIT License
295 stars 27 forks source link

Creating GF fails if done from a separate thread #555

Closed semjon00 closed 3 days ago

semjon00 commented 6 days ago

Hello! Thank you for this nice library! Here is a bug report:

Currently, the databases are accessed using a singleton cursor. Due to the way the cursor is created, the database disallows accessing it from a thread that does not match the thread in which it was created. This may cause the creation of fields to fail if they are created from different threads.

Here is a code that reproduces the issue:

from threading import Thread
from galois import GF

if __name__ == '__main__':
    field = GF(7)

    t = Thread(target=lambda: GF(11), args=())
    t.start()
    t.join()

A possible fix would be to substitute the cls.conn = sqlite3.connect(cls.file) with cls.conn = sqlite3.connect(cls.file, check_same_thread=False) inside the constructor of galois._databases._interface.DatabaseInterface. I believe that this is a safe thing to do, since there are no write operations to the database.

mhostetter commented 5 days ago

Thanks for the report. This looks good to me. If you'd like to submit a PR, we can get it merged. Otherwise, I can apply the patch.

semjon00 commented 5 days ago

It turns out there are more issues that could arise due to using multiple threads.