From 2c73110cfa36b2417858c0a3f8e89ab26d5d1151 Mon Sep 17 00:00:00 2001 From: atharv20s Date: Sat, 28 Nov 2026 15:08:57 +0530 Subject: [PATCH] fix(config): reconnect ConfigurationWatcher when daprd stream drops (#1232) Signed-off-by: atharv20s --- dapr/clients/grpc/_response.py | 60 ++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/dapr/clients/grpc/_response.py b/dapr/clients/grpc/_response.py index c18794bfc..d39a25a18 100644 --- a/dapr/clients/grpc/_response.py +++ b/dapr/clients/grpc/_response.py @@ -17,6 +17,7 @@ import contextlib import json +import logging import threading from enum import Enum from typing import ( @@ -669,13 +670,20 @@ def items(self) -> Dict[Text, ConfigurationItem]: return self._items +logger = logging.getLogger(__name__) + + class ConfigurationWatcher: def __init__(self): self.store_name = None self.keys = None self.event: threading.Event = threading.Event() + self._stopped: threading.Event = threading.Event() self.id: str = '' + def stop(self): + self._stopped.set() + def watch_configuration( self, stub: api_service_v1.DaprStub, @@ -687,11 +695,11 @@ def watch_configuration( req = api_v1.SubscribeConfigurationRequest( store_name=store_name, keys=keys, metadata=config_metadata ) + self.keys = keys + self.store_name = store_name thread = threading.Thread(target=self._read_subscribe_config, args=(stub, req, handler)) thread.daemon = True thread.start() - self.keys = keys - self.store_name = store_name check = self.event.wait(timeout=5) if not check: print(f'Unable to get configuration id for keys {self.keys}') @@ -704,21 +712,39 @@ def _read_subscribe_config( req: api_v1.SubscribeConfigurationRequest, handler: Callable[[Text, ConfigurationResponse], None], ): - try: - responses: List[api_v1.SubscribeConfigurationResponse] = ( - stub.SubscribeConfigurationAlpha1(req) - ) - isFirst = True - for response in responses: - if isFirst: - self.id = response.id - self.event.set() - isFirst = False - if len(response.items) > 0: - handler(response.id, ConfigurationResponse(response.items)) - except Exception: - print(f'{self.store_name} configuration watcher for keys {self.keys} stopped.') - pass + while not self._stopped.is_set(): + try: + responses: List[api_v1.SubscribeConfigurationResponse] = ( + stub.SubscribeConfigurationAlpha1(req) + ) + isFirst = True + for response in responses: + if self._stopped.is_set(): + return + if isFirst: + self.id = response.id + self.event.set() + isFirst = False + if len(response.items) > 0: + try: + handler(response.id, ConfigurationResponse(response.items)) + except Exception: + logger.exception( + '%s configuration handler for keys %s raised an exception.', + self.store_name, + self.keys, + ) + except Exception: + if self._stopped.is_set(): + return + logger.warning( + '%s configuration watcher for keys %s disconnected; reconnecting.', + self.store_name, + self.keys, + exc_info=True, + ) + if self._stopped.wait(5): + return class BulkPublishResponseFailedEntry: