BjornFJohansson / pydna

Clone with Python! Data structures for double stranded DNA & simulation of homologous recombination, Gibson assembly, cut & paste cloning.
Other
164 stars 44 forks source link

Race condition in ini writing with concurrent use #105

Closed JacobHayes closed 1 month ago

JacobHayes commented 1 year ago

When using pydna in multiple processes (eg: in tests with pytest-xdist), the ini file may attempt to be written multiple times or read before the default config is written.

Eg, this sequence will cause errors:

resulting in this error:

    from pydna.dseqrecord import Dseqrecord
../venv/lib/python3.10/site-packages/pydna/__init__.py:169: in <module>
    _mainsection = _parser["main"]
../python/lib/python3.10/configparser.py:964: in __getitem__
    raise KeyError(key)
E   KeyError: 'main'

(in process 2, which read the file before the contents were written)


As a fix, maybe it could use a tempfile.NamedTemporaryFile and then move it over at the end (which should be atomic on most platforms if we ensure the tempfile is in ini dir / same filesystem) or just skip creating the configuration file completely (eg: stop after _parser["main"] = default_ini)?

I'd be happy to PR either.

BjornFJohansson commented 1 year ago

Thanks for your input! I like the NamedTemporaryFile solution. If you make a PR against the "dev_bjorn" branch, Ill be happy to merge and make a new release.