diff --git a/ansible_mitogen/runner.py b/ansible_mitogen/runner.py index b60e537ce..5d0489172 100644 --- a/ansible_mitogen/runner.py +++ b/ansible_mitogen/runner.py @@ -367,10 +367,15 @@ def _setup_cwd(self): """ For situations like sudo to a non-privileged account, CWD could be $HOME of the old account, which could have mode go=, which means it is - impossible to restore the old directory, so don't even try. + impossible to restore the old directory. Fallback to a neutral temp if so. """ if self.cwd: - os.chdir(self.cwd) + try: + os.chdir(self.cwd) + except OSError: + LOG.debug('%r: could not CHDIR to %r fallback to %r', + self, self.cwd, self.good_temp_dir) + os.chdir(self.good_temp_dir) def _setup_environ(self): """ diff --git a/docs/changelog.rst b/docs/changelog.rst index 3adaec58c..883d09d2f 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -21,6 +21,7 @@ To avail of fixes in an unreleased version, please download a ZIP file Unreleased ---------- +* :gh:issue:`636` os.chdir fails if the sudo/become user lacks adequate permissions to chdir prior to task * :gh:issue:`1127` :mod:`mitogen`: Consolidate mitogen backward compatibility fallbacks and polyfills into :mod:`mitogen.core` * :gh:issue:`1127` :mod:`ansible_mitogen`: Remove backward compatibility