diff --git a/torch_xla/__init__.py b/torch_xla/__init__.py index b848cc34d88..de0bdea23a0 100644 --- a/torch_xla/__init__.py +++ b/torch_xla/__init__.py @@ -148,13 +148,17 @@ def _setup_tpu_vm_library_path() -> bool: def _prepare_to_exit(): try: + raise ValueError('fake error') _XLAC._prepare_to_exit() if int(os.environ.get('PT_XLA_DEBUG', '0')): _summarize_fn_tracker() except Exception as e: logging.error( "Caught an exception when exiting the process. Exception: ", exc_info=e) - os._exit(1) + # Due to https://bugs.python.org/issue27035, simply raising an exception in the atexit callback does not set the exit code correctly. That is why we need to set the exit code explicitly. + # Using `exit(1)` does not set a correct exit code because it is useful for the interactive interpreter shell and should not be used in programs and it works by raising an exception. (https://docs.python.org/3/library/constants.html#exit) + # sys.exit(1) does not set a correct exit code because it also raises an exception. + exit(1) def _init_xla_lazy_backend():