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
Python crash on macOS when CWD is invalid #80417
Comments
CPython crashes with "pymain_compute_path0: memory allocation failed" when attempting to execute it with a library module from a previously deleted directory. To reproduce: cd ~/tmp/python_crash rm -rf ~/tmp/python_crash python3.7 -m pdb Current thread 0x000000011060e5c0 (most recent call first): |
This is happening because _Py_wgetcwd returns NULL (although the underliying getcwd system call populates the
and its caller, pymain_run_python, interprets a failure in _PyPathConfig_ComputeArgv0 as a memory problem: PyObject *path0 = _PyPathConfig_ComputeArgv0(config->argc,
config->argv);
if (path0 == NULL) {
err = _Py_INIT_NO_MEMORY();
goto done;
} |
_Py_wgetcwd() call has been introduced by the following commit: commit ee37845
I don't think that it's correct to fail with a fatal error if the current directory no longer exist. Would it be possible to not add it to sys.path if it doesn't exist? Silently ignore the error. @nick: What do you think? |
Omitting it from sys.path in that case makes sense to me, but I'm not sure what sys.argv[0] should be set to. |
I propose to use ".". It would be consistent with platforms which doesn't implement realpath: if (have_module_arg) {
#if defined(HAVE_REALPATH) || defined(MS_WINDOWS)
_Py_wgetcwd(fullpath, Py_ARRAY_LENGTH(fullpath));
argv0 = fullpath;
n = wcslen(argv0);
#else
argv0 = L".";
n = 1;
#endif
} And it defers the error handler to later. Example of Python 3 running in a removed directory: $ python3
Python 3.7.2 (default, Jan 16 2019, 19:49:22)
>>> import os
>>> os.getcwd()
FileNotFoundError: [Errno 2] No such file or directory
>>> os.path.abspath('.')
Traceback (most recent call last):
File "/usr/lib64/python3.7/posixpath.py", line 383, in abspath
cwd = os.getcwd()
FileNotFoundError: [Errno 2] No such file or directory I would prefer "." than "-m". |
If we set argv0 to ".": if (_Py_wgetcwd(fullpath, Py_ARRAY_LENGTH(fullpath))) {
argv0 = fullpath;
n = wcslen(argv0);
}
else {
argv0 = L".";
n = 1;
} then the caller does not have any way of knowing if the returned argv0 is due What do you think is the best way to signal pymain_run_python that the current directory |
Oh. It seems like I misunderstood the issue. I read "argv0" as sys.argv[0], but _PyPathConfig_ComputeArgv0 is used to insert a value at the start of sys.path. That's different... If the current directory has been removed, sys.path should just be left unchanged, no? |
Yup. But what is the best way to signal the caller that _PyPathConfig_ComputeArgv0 has failed because _Py_wgetcwd has failed without changing the API massively? Right now if _PyPathConfig_ComputeArgv0 returns null is assumed that is due to a memory error when calling PyUnicode_FromWideChar. So either we stop returning _Py_INIT_NO_MEMORY() and then skip appending to sys_path or we change the API to signal different problems to the caller. Also, notice that the same function is used in sysmodule.c in PySys_SetArgvEx: If argv[0] is not '-c' nor '-m', prepend argv[0] to sys.path. |
Can someone please on macOS to confirm that the bug is fixed? |
It seems to work: ❯ uname -a ❯ pwd /tmp/check /tmp/check Debug the Python program given by pyfile. Alternatively, Initial commands are read from .pdbrc files in your home directory To let the script run until an exception occurs, use "-c continue". |
Pablo: is Python 3.7 affected by the issue? |
Yup: ~ /tmp /tmp /tmp/check /tmp/check Current thread 0x000000011d9405c0 (most recent call first): |
Pablo: Oh ok, thanks for testing. I fixed 3.7 as well. Would you mind to validate my fix? |
The fix for 3.7 works too: no more crashing. Thanks, everyone! |
I planned to test, but I had no access to macOS last years. Thanks for testing Ned. Good to hear that the bug is now fixed in 3.7 and master. |
Thanks for sorting this out, Victor! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: