teardown logging in exec_cmd

for normal borg command invocation:
- logging is set up in Archiver.run
- the atexit handler calls logging.shutdown when process terminates

for tests:
- Archiver.run called by exec_cmd
- no atexit handler executed as process lives on
- borg.logger.teardown (calls shutdown and configured=False) now
  called in exec_cmd
This commit is contained in:
Thomas Waldmann 2023-05-26 12:47:31 +02:00
parent dac4609468
commit 746cef1cba
No known key found for this signature in database
GPG Key ID: 243ACFA951F78E01
2 changed files with 12 additions and 1 deletions

View File

@ -106,10 +106,17 @@ def _log_warning(message, category, filename, lineno, file=None, line=None):
def remove_handlers(logger):
for handler in logger.handlers[:]:
handler.flush()
handler.close()
logger.removeHandler(handler)
def teardown_logging():
global configured
logging.shutdown()
configured = False
def setup_logging(stream=None, conf_fname=None, env_var="BORG_LOGGING_CONF", level="info", is_serve=False, json=False):
"""setup logging module according to the arguments provided

View File

@ -21,6 +21,7 @@ from ...constants import * # NOQA
from ...helpers import Location
from ...helpers import EXIT_SUCCESS
from ...helpers import bin_to_hex
from ...logger import teardown_logging
from ...manifest import Manifest
from ...remote import RemoteRepository
from ...repository import Repository
@ -81,7 +82,10 @@ def exec_cmd(*args, archiver=None, fork=False, exe=None, input=b"", binary_outpu
except SystemExit as e:
output_text.flush()
return e.code, output.getvalue() if binary_output else output.getvalue().decode()
try:
ret = archiver.run(args)
finally:
teardown_logging() # usually done via atexit, but we do not exit here
output_text.flush()
return ret, output.getvalue() if binary_output else output.getvalue().decode()
finally: