classification
Title: Python 3.3 fails when starting from read-only FS
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.3, Python 3.4
process
Status: closed Resolution: duplicate
Dependencies: Superseder: most failures to write byte-compiled file no longer suppressed
View: 15833
Assigned To: Nosy List: andrewjcg, brett.cannon, neologix, pitrou, sbt, vstinner
Priority: normal Keywords:

Created on 2012-10-04 23:53 by andrewjcg, last changed 2012-10-05 19:18 by neologix. This issue is now closed.

Messages (6)
msg172029 - (view) Author: Andrew Gallagher (andrewjcg) Date: 2012-10-04 23:52
This occurs when python is installed on a read-only mount AND all the .pyc files are out-of-date.  Therefore, when python starts and attempts to write a new .pyc file, _write_atomic in "Lib/importlib/_bootstrap.py" throws an OSError with an errno of EROFS, which is not handled (and ignored) and kills the interpreter.

$ python
Fatal Python error: Py_Initialize: Unable to get the locale encoding
Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 1558, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1525, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 586, in _check_name_wrapper
  File "<frozen importlib._bootstrap>", line 1023, in load_module
  File "<frozen importlib._bootstrap>", line 1004, in load_module
  File "<frozen importlib._bootstrap>", line 562, in module_for_loader_wrapper
  File "<frozen importlib._bootstrap>", line 854, in _load_module
  File "<frozen importlib._bootstrap>", line 990, in get_code
  File "<frozen importlib._bootstrap>", line 1051, in _cache_bytecode
  File "<frozen importlib._bootstrap>", line 1074, in set_data
  File "<frozen importlib._bootstrap>", line 128, in _write_atomic
OSError: [Errno 30] Read-only file system: '<read-only-mount-path>/lib/python3.3/encodings/__pycache__/__init__.cpython-33.pyc.139872939267056'
Aborted (core dumped)

The following (hacky) patch fixes the issue for me:

--- a/Python-3.Lib/importlib/_bootstrap.py
+++ b/Python-3.Lib/importlib/_bootstrap.py
@@ -1070,6 +1070,10 @@ class SourceFileLoader(FileLoader, SourceLoader):
                 # If can't get proper access, then just forget about writing
                 # the data.
                 return
+            except OSError as e:
+                if e.errno != 30:  # ignore EROFS
+                    raise
+                return
         try:
             _write_atomic(path, data, _mode)
             _verbose_message('created {!r}', path)
@@ -1077,6 +1081,9 @@ class SourceFileLoader(FileLoader, SourceLoader):
             # Don't worry if you can't write bytecode or someone is writing
             # it at the same time.
             pass
+        except OSError as e:
+            if e.errno != 30:  # ignore EROFS
+                raise
msg172032 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2012-10-05 00:16
Python 3.2 logs an error to stderr (if Python is started in verbose mode) if the directory and/or the pyc file cannot be created, and continue.

#ifdef MS_WINDOWS
    if (_mkdir(cpathname) < 0 && errno != EEXIST) {
#else
    if (mkdir(cpathname, dirmode) < 0 && errno != EEXIST) {
#endif
        *dirpath = saved;
        if (Py_VerboseFlag)
            PySys_WriteStderr(
                "# cannot create cache dir %s\n", cpathname);
        return;
    }
    *dirpath = saved;

    fp = open_exclusive(cpathname, mode);
    if (fp == NULL) {
        if (Py_VerboseFlag)
            PySys_WriteStderr(
                "# can't create %s\n", cpathname);
        return;
    }
msg172033 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-10-05 00:21
Hmm... I guess maybe we should trap all OSErrors after all (and print the error when in verbose mode).
msg172049 - (view) Author: Charles-Fran├žois Natali (neologix) * (Python committer) Date: 2012-10-05 05:52
> Hmm... I guess maybe we should trap all OSErrors after all (and print the error when in verbose mode).

Yes.
msg172062 - (view) Author: Richard Oudkerk (sbt) * (Python committer) Date: 2012-10-05 09:09
This is more or less a duplicate of #15833 (although the errno mentioned there is EIO instead of the more sensible EROFS).
msg172118 - (view) Author: Charles-Fran├žois Natali (neologix) * (Python committer) Date: 2012-10-05 19:18
> This is more or less a duplicate of #15833

Indeed, closing as duplicate.
History
Date User Action Args
2012-10-05 19:18:36neologixsetstatus: open -> closed
superseder: most failures to write byte-compiled file no longer suppressed
messages: + msg172118

type: crash -> behavior
resolution: duplicate
stage: resolved
2012-10-05 09:09:32sbtsetnosy: + sbt
messages: + msg172062
2012-10-05 05:52:07neologixsetmessages: + msg172049
2012-10-05 00:21:34pitrousetnosy: + neologix, pitrou
messages: + msg172033
2012-10-05 00:16:30vstinnersetnosy: + vstinner
messages: + msg172032
2012-10-04 23:57:37r.david.murraysetnosy: + brett.cannon

type: behavior -> crash
versions: + Python 3.4
2012-10-04 23:53:00andrewjcgcreate