From 83e8a310c36ec2273ad8bd61aad3285ed6917ac5 Mon Sep 17 00:00:00 2001 From: Evan Hicks Date: Tue, 17 Nov 2026 15:03:44 -0500 Subject: [PATCH] feat(taskworker): Serve push health checks on a separate port Saturated push workers queue PushTask RPCs in the grpc executor with no limit, so health checks wait behind them, time out, and get the pod ejected. With --health-port set, health gets its own server and the push server rejects RPCs beyond --concurrency with RESOURCE_EXHAUSTED instead of queueing them. Co-Authored-By: Claude Opus 5.5 --- clients/python/src/taskbroker_client/cli.py | 11 ++++ .../src/taskbroker_client/worker/worker.py | 24 ++++++- clients/python/tests/worker/test_worker.py | 64 +++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) diff --git a/clients/python/src/taskbroker_client/cli.py b/clients/python/src/taskbroker_client/cli.py index e207f4b6..53db22a3 100644 --- a/clients/python/src/taskbroker_client/cli.py +++ b/clients/python/src/taskbroker_client/cli.py @@ -308,6 +308,15 @@ def pull( default=50052, type=int, ) +@click.option( + "--health-port", + help=( + "Serve gRPC health checks on this port, separate from the push server. When set, pushes " + "beyond --concurrency are rejected with RESOURCE_EXHAUSTED instead of queueing." + ), + default=None, + type=int, +) @click.option( "--grpc-max-message-size", help="Maximum gRPC send/receive message size, in bytes.", @@ -365,6 +374,7 @@ def push( health_check_file_path: str | None, health_check_sec_per_touch: float, grpc_port: int, + health_port: int | None, grpc_max_message_size: int, push_task_timeout: float, skip_awaiting_futures: bool, @@ -395,6 +405,7 @@ def push( health_check_file_path=health_check_file_path, health_check_sec_per_touch=health_check_sec_per_touch, grpc_port=grpc_port, + health_port=health_port, grpc_max_message_size=grpc_max_message_size, push_task_timeout=push_task_timeout, skip_awaiting_futures=skip_awaiting_futures, diff --git a/clients/python/src/taskbroker_client/worker/worker.py b/clients/python/src/taskbroker_client/worker/worker.py index 3afade85..a563170b 100644 --- a/clients/python/src/taskbroker_client/worker/worker.py +++ b/clients/python/src/taskbroker_client/worker/worker.py @@ -267,6 +267,7 @@ def __init__( health_check_file_path: str | None = None, health_check_sec_per_touch: float = DEFAULT_WORKER_HEALTH_CHECK_SEC_PER_TOUCH, grpc_port: int = 50052, + health_port: int | None = None, grpc_max_message_size: int = -1, push_task_timeout: float = 5, skip_awaiting_futures: bool = True, @@ -336,6 +337,7 @@ def __init__( self._processing_pool_name: str = processing_pool_name or "unknown" self._grpc_port = grpc_port + self._health_port = health_port self._grpc_max_message_size = grpc_max_message_size self._grpc_secrets = parse_rpc_secret_list(app.config["rpc_secret"]) self._push_task_timeout = push_task_timeout @@ -521,6 +523,7 @@ def start(self) -> int: self.worker_pool.start_spawn_children_thread() server: grpc.Server | None = None + health_server: grpc.Server | None = None server_started = False health_servicer: health.HealthServicer | None = None @@ -558,6 +561,11 @@ def signal_handler(*args: Any) -> None: ("grpc.max_receive_message_length", max_message_size), ("grpc.max_send_message_length", max_message_size), ], + # One RPC per handler thread, so excess pushes are rejected instead of queueing. + # The limit covers every service on the server, so it needs health on its own server. + maximum_concurrent_rpcs=( + self._concurrency if self._health_port is not None else None + ), ) taskbroker_pb2_grpc.add_WorkerServiceServicer_to_server( @@ -573,10 +581,17 @@ def signal_handler(*args: Any) -> None: server.add_insecure_port(f"[::]:{self._grpc_port}") + if self._health_port is not None: + health_server = grpc.server(ThreadPoolExecutor(max_workers=1)) + health_pb2_grpc.add_HealthServicer_to_server(health_servicer, health_server) + health_server.add_insecure_port(f"[::]:{self._health_port}") + # Don't accept connections we are about to drop. if self._shutdown_signal.is_set(): return 0 + if health_server is not None: + health_server.start() server.start() server_started = True @@ -593,7 +608,10 @@ def signal_handler(*args: Any) -> None: health_servicer.set("", health_pb2.HealthCheckResponse.SERVING) health_servicer.set(WORKER_SERVICE_NAME, health_pb2.HealthCheckResponse.SERVING) - logger.info("taskworker.grpc_server.started", extra={"port": self._grpc_port}) + logger.info( + "taskworker.grpc_server.started", + extra={"port": self._grpc_port, "health_port": self._health_port}, + ) self._start_health_check_thread() # Poll so a signal handler that only flipped a bool still gets us @@ -619,6 +637,10 @@ def signal_handler(*args: Any) -> None: if server is not None and server_started: server.stop(grace=5) + # Stopped after the push server so probes keep seeing NOT_SERVING during its grace period. + if health_server is not None and server_started: + health_server.stop(grace=None) + self.shutdown() return 0 diff --git a/clients/python/tests/worker/test_worker.py b/clients/python/tests/worker/test_worker.py index 9d8e9f67..caf9bc2d 100644 --- a/clients/python/tests/worker/test_worker.py +++ b/clients/python/tests/worker/test_worker.py @@ -1173,6 +1173,70 @@ def wait_for_termination(timeout: float | None = None) -> bool: pool_shutdown.assert_called_once_with() +def test_push_start_without_health_port_has_no_rpc_limit() -> None: + taskworker = _make_push_worker(concurrency=2, warmup_timeout=5) + taskworker._shutdown_signal.request() + + fake_health = mock.MagicMock() + fake_server = mock.MagicMock() + + with ( + _push_worker_grpc_mocks(taskworker, fake_server, fake_health), + mock.patch( + "taskbroker_client.worker.worker.grpc.server", return_value=fake_server + ) as grpc_server, + ): + taskworker.start() + + grpc_server.assert_called_once() + assert grpc_server.call_args.kwargs["maximum_concurrent_rpcs"] is None + + +def test_push_start_serves_health_on_separate_server() -> None: + taskworker = _make_push_worker(concurrency=2, warmup_timeout=5, health_port=50098) + handlers: dict[int, Callable[..., None]] = {} + + fake_health = mock.MagicMock() + fake_server = mock.MagicMock() + fake_health_server = mock.MagicMock() + + def wait_for_termination(timeout: float | None = None) -> bool: + handlers[signal.SIGTERM](signal.SIGTERM, None) + return True + + fake_server.wait_for_termination.side_effect = wait_for_termination + + with ( + _push_worker_grpc_mocks(taskworker, fake_server, fake_health, handlers), + mock.patch( + "taskbroker_client.worker.worker.grpc.server", + side_effect=[fake_server, fake_health_server], + ) as grpc_server, + mock.patch( + "taskbroker_client.worker.worker.health_pb2_grpc.add_HealthServicer_to_server" + ) as add_health, + mock.patch.object( + TaskWorkerProcessingPool, "ready_count", new_callable=mock.PropertyMock, return_value=2 + ), + ): + exitcode = taskworker.start() + + assert exitcode == 0 + push_call, health_call = grpc_server.call_args_list + assert push_call.kwargs["maximum_concurrent_rpcs"] == 2 + assert "maximum_concurrent_rpcs" not in health_call.kwargs + + # Health stays on the push server too, so probes on the old port keep working during rollout. + assert add_health.call_args_list == [ + mock.call(fake_health, fake_server), + mock.call(fake_health, fake_health_server), + ] + fake_health_server.add_insecure_port.assert_called_once_with("[::]:50098") + fake_health_server.start.assert_called_once_with() + fake_server.stop.assert_called_once_with(grace=5) + fake_health_server.stop.assert_called_once_with(grace=None) + + def test_push_start_keeps_serving_while_server_is_healthy() -> None: """ A healthy server must not end the serve loop.