From 53eb3232874d73d7df8a61cf877a5b32d99a5332 Mon Sep 17 00:00:00 2001 From: qananasikq Date: Fri, 17 Apr 2026 23:53:10 +0300 Subject: [PATCH] fix batch pool leak --- encar_scraper/encar.py | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/encar_scraper/encar.py b/encar_scraper/encar.py index cceaf75..783aa86 100644 --- a/encar_scraper/encar.py +++ b/encar_scraper/encar.py @@ -919,6 +919,41 @@ class EncarScraper: excluded_brands: set[str] | None = None, runtime_filters: FiltersConfig | None = None, probe_all_photos: bool = False, + ) -> dict[str, Any]: + """Публичная обёртка: гарантирует закрытие HTTP pool даже при ошибках.""" + try: + return self._sync_listing_impl( + limit=limit, + filters=filters, + lane=lane, + only_new=only_new, + batch_size=batch_size, + redis_client=redis_client, + allowed_brands=allowed_brands, + excluded_brands=excluded_brands, + runtime_filters=runtime_filters, + probe_all_photos=probe_all_photos, + ) + finally: + if self._batch_pool is not None: + try: + self._batch_pool.close() + except Exception: + logger.debug("Failed to close batch pool", exc_info=True) + self._batch_pool = None + + def _sync_listing_impl( + self, + limit: int | None = None, + filters: EncarFilters | None = None, + lane: str = "encar", + only_new: bool = False, + batch_size: int = 1000, + redis_client: Any | None = None, + allowed_brands: set[str] | None = None, + excluded_brands: set[str] | None = None, + runtime_filters: FiltersConfig | None = None, + probe_all_photos: bool = False, ) -> dict[str, Any]: """Полная синхронизация листинга Encar. @@ -1152,11 +1187,6 @@ class EncarScraper: self._clear_checkpoint(redis_client) - # Закрываем pool batch API - if self._batch_pool is not None: - self._batch_pool.close() - self._batch_pool = None - logger.info( "Full sync complete: %d shards, %d collected, %d synced, %d failed, %d marked sold", len(shards), items_collected, synced, failed, marked_sold,