diff --git a/src/flb_engine.c b/src/flb_engine.c index d74cdd9deeb..b5408a2f0e1 100644 --- a/src/flb_engine.c +++ b/src/flb_engine.c @@ -1322,7 +1322,16 @@ int flb_engine_start(struct flb_config *config) if (config->http_server == FLB_TRUE) { config->http_ctx = flb_hs_create(config->http_listen, config->http_port, config); - flb_hs_start(config->http_ctx); + if (!config->http_ctx) { + flb_error("[engine] could not initialize HTTP server"); + return -1; + } + + ret = flb_hs_start(config->http_ctx); + if (ret != 0) { + flb_error("[engine] could not start HTTP server"); + return -1; + } } #endif diff --git a/tests/integration/scenarios/internal_http_server/tests/test_internal_http_server_001.py b/tests/integration/scenarios/internal_http_server/tests/test_internal_http_server_001.py index f1bf18a03f8..df20aeb24c6 100644 --- a/tests/integration/scenarios/internal_http_server/tests/test_internal_http_server_001.py +++ b/tests/integration/scenarios/internal_http_server/tests/test_internal_http_server_001.py @@ -1,8 +1,10 @@ import concurrent.futures import os +import socket import subprocess import pytest +from utils.fluent_bit_manager import FluentBitManager, FluentBitStartupError from utils.http_matrix import curl_supports_http2, run_curl_request from utils.test_service import FluentBitTestService @@ -146,3 +148,28 @@ def test_internal_http_server_http2_subset(): assert result["http_version"] == "2" finally: service.stop() + + +def test_internal_http_server_bind_failure_fails_startup(monkeypatch): + config_file = os.path.abspath( + os.path.join(os.path.dirname(__file__), "../config/internal_http_server.yaml") + ) + holder = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + holder.bind(("127.0.0.1", 0)) + port = holder.getsockname()[1] + fluent_bit = FluentBitManager(config_file) + + monkeypatch.setenv("FLUENT_BIT_HTTP_MONITORING_PORT", str(port)) + + def use_occupied_monitoring_port(_env_var_name, starting_port=0): + fluent_bit.http_monitoring_port = str(port) + + fluent_bit.set_http_monitoring_port = use_occupied_monitoring_port + + try: + with pytest.raises(FluentBitStartupError, match="exited early with code"): + fluent_bit.start() + assert fluent_bit.process.returncode != 0 + finally: + holder.close() + fluent_bit.stop()