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:
process 1 opens the file in write mode with open(_ini_path, 'w', encoding="utf-8") as f
process 2 reads the file _parser.read(_ini_path)
process 1 writes the config _parser.write(f)
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)?
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.
When using
pydna
in multiple processes (eg: in tests withpytest-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:
with open(_ini_path, 'w', encoding="utf-8") as f
_parser.read(_ini_path)
_parser.write(f)
resulting in this error:
(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.