Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race condition in ini writing with concurrent use #105

Open
JacobHayes opened this issue Mar 6, 2023 · 1 comment
Open

Race condition in ini writing with concurrent use #105

JacobHayes opened this issue Mar 6, 2023 · 1 comment

Comments

@JacobHayes
Copy link

JacobHayes commented Mar 6, 2023

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)?

I'd be happy to PR either.

@BjornFJohansson
Copy link
Owner

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.

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

No branches or pull requests

2 participants