datasig-ac-uk / RoughPy

Toolbox for working with streaming data as rough paths in Python
https://roughpy.org
BSD 3-Clause "New" or "Revised" License
29 stars 4 forks source link

Re-entrant import crash #121

Open inakleinbottle opened 1 week ago

inakleinbottle commented 1 week ago

Describe the bug In some very unique circumstances, RoughPy might be imported twice within a single process. This is usually not a problem because Python caches imported modules in sys.modules. However, if the import code is triggered twice, an error will be caused because the scalar types are already registered in the Python interface.

To Reproduce For example, the following code should cause the error

import roughpy as rp
import sys
del sys.module["roughpy"]
del rp
import roughpy as rp

You should not be messing with sys.modules in your own code, please don't do this

Expected behavior RoughPy should load as usual on second import

Desktop (please complete the following information): All, RoughPy 0.1.1

terrylyons commented 1 week ago

Wow, what triggered this. Terry

On 21 Oct 2024, at 18:04, Sam Morley @.***> wrote:



Describe the bug In some very unique circumstances, RoughPy might be imported twice within a single process. This is usually not a problem because Python caches imported modules in sys.modules. However, if the import code is triggered twice, an error will be caused because the scalar types are already registered in the Python interface.

To Reproduce For example, the following code should cause the error

import roughpy as rp import sys del sys.module["roughpy"] del rp import roughpy as rp

You should not be messing with sys.modules in your own code, please don't do this

Expected behavior RoughPy should load as usual on second import

Desktop (please complete the following information): All, RoughPy 0.1.1

— Reply to this email directly, view it on GitHubhttps://github.com/datasig-ac-uk/RoughPy/issues/121, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABQWN67RXZMHCSQULMV2FHDZ4UXZZAVCNFSM6AAAAABQKUBBYWVHI2DSMVQWIX3LMV43ASLTON2WKOZSGYYDGMJXGI4DMOA. You are receiving this because you are subscribed to this thread.Message ID: @.***>

inakleinbottle commented 1 week ago

This was triggered when the first import failed, because of an unfound library dependency, so it would have failed again anyway but I also got an additional exception triggered from the init function where it tries to register the standard scalar types. This behaviour will go away in the future anyway, because it will not be essential that scalars have singleton types.

This is exceptionally difficult to trigger, usually from some other error like I described above, or by doing something stupid (as in my "To Reproduce" steps).