Skip to content

Concurrent import of astropy occasionally fails with likely race condition on config files. #7290

Closed

Description

@rainwoodman

When trying to import astropy from an MPI application ( with 128 ranks), the application crashes during first import randomly with a file system error.

It appears that some astropy instances are trying to modify a configuration file while some others are trying to read it.

    from astropy.cosmology import Planck13, Planck15, WMAP5, WMAP7, WMAP9
  File "/dev/shm/local/lib/python3.6/site-packages/astropy/__init__.py", line 286, in <module>
    log = _init_log()
  File "/dev/shm/local/lib/python3.6/site-packages/astropy/logger.py", line 97, in _init_log
    log._set_defaults()
  File "/dev/shm/local/lib/python3.6/site-packages/astropy/logger.py", line 473, in _set_defaults
    self.setLevel(conf.log_level)
  File "/dev/shm/local/lib/python3.6/site-packages/astropy/config/configuration.py", line 273, in __get__
    return self()
  File "/dev/shm/local/lib/python3.6/site-packages/astropy/config/configuration.py", line 396, in __call__
    sec = get_config(self.module)
  File "/dev/shm/local/lib/python3.6/site-packages/astropy/config/configuration.py", line 530, in get_config
    cobj = configobj.ConfigObj(cfgfn, interpolation=False)
  File "/dev/shm/local/lib/python3.6/site-packages/astropy/extern/configobj/configobj.py", line 1228, in __init__
    self._load(infile, configspec)
  File "/dev/shm/local/lib/python3.6/site-packages/astropy/extern/configobj/configobj.py", line 1317, in _load
    raise error
astropy.extern.configobj.configobj.ParseError: Invalid line ('\x00\x00\x00\x00\x00\x00\x00\x00\x00
....

Activity

  1. rainwoodman commented on Mar 14, 2018

    @rainwoodman
    Author

    I'd add this seem to occur only when astropy is updated. Ideally a library shall not rely on any particular dot files in $HOME to properly function. The parts that rely on dot files are more or less like final, user facing applications. But in the present day the boundary between application and library is blurry.

    Anyhow, I suggest the following fix:

    1. Ship a default configuration file that is in the package directory
    2. ship a cli script that migrates the configuration file, e.g. astropy-config-migrate
    3. Try to interpret astropy/config/astropy.cfg.
    4. If it fails for either ENOENT or a broken syntax, or version compatibility check, issue a warning and use the default configuration file, instead of trying to migrate the configuration file, provide an example command-line that calls the migration tool.

    This way astropy doesn't try to automatically migrate the configuration file during import. Yet the migration functionality is preserved when it is needed.

  2. j-dr commented on Aug 14, 2018

    @j-dr

    Has there been any progress on this? It is a big issue when trying to run any large mpi python jobs that have astropy dependencies.

  3. pllim commented on Aug 17, 2018

    @pllim
    Member

    Maybe related to #5899 ?

  4. rainwoodman commented on Aug 17, 2018

    @rainwoodman
    Author

    Yes. It is related. Use cases in headless applications will be smoother if the automated loading of file based configurations is opted out by default.

    Would a two tier initialization (think matplotlib vs matplotlib.pyplot) be handy for this? After importing the top level package one can modify / load the configuration (think matplotlib.use), but in interactive application one directly import from another namespace which triggers automated backend initialization based on profiles in the file system.

  5. pllim commented on Aug 17, 2018

    @pllim
    Member
  6. rainwoodman commented on Mar 16, 2020

    @rainwoodman
    Author

    This is not fixed by #8237, and #8552 does not solve this issue.

    We still have a race condition, because multiple astropy instances still tries to write to the same files.

    Two possible solutions:

    1. What about adding a magic environment variable to instruct astropy to use a per-process directory for the config and caches.

    2. We can also try to make all write operations in the config module atomic (at least on POSIX). e.g. first write to a unique temporary file, then renamed to the target file.

  7. pllim commented on Mar 16, 2020

    @pllim
    Member

    @rainwoodman , have you tried this with the latest dev version of astropy? We have some caching problems reported but none related to config.

  8. rainwoodman commented on Mar 29, 2020

    @rainwoodman
    Author

    No, I have not tried with the dev version; we have been using a local workaround that first imports astropy with one python process per file system hosting the astropy config file.

    As the code is written currently there still is a race. Thus I am not very positive that the dev version will fix the issue.

    The race is between the write at:

    https://github.com/astropy/astropy/blob/master/astropy/config/configuration.py#L742

    and the read at:

    https://github.com/astropy/astropy/blob/master/astropy/config/configuration.py#L701

    The read sees a half written file when another process is writing. Without using a lock, the write shall first write to a temporary file on the same filesystem (e.g. the same folder), then do an "atomic rename" to replace the file.

    Or did I misread the meaning of cfgfn?

  9. pllim commented on Mar 30, 2020

    @pllim
  10. pllim commented on Mar 30, 2020

    @pllim
    Member

    @rainwoodman , would #10090 fix your problem by avoiding the loading of astropy.cfg altogether? Do you want to take it for a spin?

  11. dannygoldstein commented on Apr 27, 2020

    @dannygoldstein
    Contributor

    I am also experiencing this error and am a little confused about it. Why is there any config-file writing happening at all? Once I have a astropy.cfg and a astropy.version.cfg in my ~/.astropy/config directory, why does anything new need to be written?

  12. dannygoldstein commented on May 9, 2020

    @dannygoldstein
    Contributor

    @j-dr did you ever manage to fix this ?

  13. j-dr commented on May 11, 2020

    @j-dr

    I have a fix that works at NERSC for docker/shifter images. You just need to run the following at the end of your dockerfile:

    ENV XDG_CACHE_HOME=/srv/cache
    RUN mkdir -p $XDG_CACHE_HOME/astropy
    ENV XDG_CONFIG_HOME=/srv/config
    RUN mkdir -p $XDG_CONFIG_HOME/astropy
    RUN python -c "import astropy"
    RUN /sbin/ldconfig
    

    Not sure if this will work for any other use cases though.

  14. rainwoodman commented on May 12, 2020

    @rainwoodman
    Author
  15. dannygoldstein commented on May 12, 2020

    @dannygoldstein
    Contributor
  16. rainwoodman commented on May 12, 2020

    @rainwoodman
    Author
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions