Skip to content

Conversation

@kmnhan
Copy link

@kmnhan kmnhan commented Apr 15, 2025

I've found that trying to load files with a different byte order in different threads leads to a race condition and fails, due to the PackedFileRecordHeader object being global.

The following code is a MWE that reproduces the race condition:

import threading
from igor2.packed import load


def worker(filename, thread_id):
    expt = None
    for bo in ('<', '>'):
        try:
            _, expt = load(filename, initial_byte_order=bo)
        except ValueError:
            pass
    if expt is None:
        raise ValueError("No experiment loaded")


threads = []
for i, fname in enumerate(["tests/data/packed-byteorder.pxt"] * 100):
    t = threading.Thread(target=worker, args=(fname, i))
    threads.append(t)
    t.start()

for t in threads:
    t.join()

This PR ensures thread safety by creating a new instance of the structure for each load instead of reusing the singleton.

@kmnhan kmnhan marked this pull request as ready for review April 15, 2025 09:23
@paulmueller
Copy link
Member

@kmnhan Thank you!
For some reason GH Actions is not shown here. Actions fail due to flake8: https://github.com/AFM-analysis/igor2/actions/runs/14465886033

Could you please fix those, then I will merge 🙏

@kmnhan
Copy link
Author

kmnhan commented Apr 15, 2025

I've fixed it in aad6c00, could you try running the workflow again? Thanks!

@paulmueller
Copy link
Member

Right, sorry. I'm just going to merge now. No idea why GH Actions did not run..

@paulmueller paulmueller merged commit 1ab895f into AFM-analysis:master Apr 15, 2025
@kmnhan kmnhan deleted the thread-safety branch April 15, 2025 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants