From 48c478707fb7d7211c5258df5f66725cfda14301 Mon Sep 17 00:00:00 2001 From: Andreas Maier Date: Tue, 14 Nov 2023 18:13:30 +0100 Subject: [PATCH] Added handling of exceptions when starting the HTTP server Details: * Added handling of IOError raised by start_http_server() in the HTTP case (it was already handled in the HTTPS case). * For the HTTPS case, broadened the catching of IOError to now catch Exception, in order to make double sure that we catch everything, in order to investigate the issue with occasional ssl.SSLEOFError exceptions. * Simplified and corrected the error message used for re-raising an exception raised by start_http_server() in the HTTPS case. Signed-off-by: Andreas Maier --- docs/changes.rst | 3 +++ .../zhmc_prometheus_exporter.py | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/docs/changes.rst b/docs/changes.rst index 58006c74..d746422d 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -65,6 +65,9 @@ Released: not yet * Test: Circumvented a pip-check-reqs issue by excluding its version 2.5.0. +* Added handling of exceptions raised by the built-in HTTP server during + its startup, for the HTTP case. (related to issue #397) + **Enhancements:** * Added support for Python 3.12. Had to increase the minimum versions of diff --git a/zhmc_prometheus_exporter/zhmc_prometheus_exporter.py b/zhmc_prometheus_exporter/zhmc_prometheus_exporter.py index fd801214..9e07aaf3 100755 --- a/zhmc_prometheus_exporter/zhmc_prometheus_exporter.py +++ b/zhmc_prometheus_exporter/zhmc_prometheus_exporter.py @@ -1868,12 +1868,20 @@ def main(): keyfile=server_key_file, client_cafile=ca_cert_file, client_auth_required=(ca_cert_file is not None)) - except IOError as exc: + # pylint_ disable=broad-exception + except Exception as exc: + # We catch Exception for now in order to investigate the + # issue that with ssl.SSLEOFError being raised occasionally. raise ImproperExit( - "Issues with server certificate, key, or CA certificate " - "files: {}: {}".format(exc.__class__.__name__, exc)) + "Cannot start HTTPS server: {}: {}". + format(exc.__class__.__name__, exc)) else: - start_http_server(port=port) + try: + start_http_server(port=port) + except IOError as exc: + raise ImproperExit( + "Cannot start HTTP server: {}: {}". + format(exc.__class__.__name__, exc)) logprint(logging.INFO, PRINT_ALWAYS, "Exporter is up and running on port {}".format(port))