Skip to content

Commit cb3ff64

Browse files
authored
fix(commits): prevent broken pool process error [CM-702]
1 parent cf8be83 commit cb3ff64

3 files changed

Lines changed: 42 additions & 22 deletions

File tree

services/apps/git_integration/src/crowdgit/services/clone/clone_service.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,16 +256,16 @@ async def _calculate_batch_depth(self, repo_path: str, remote: str) -> int:
256256
total_branches_tags = len(total_branches_tags.splitlines())
257257
if total_branches_tags <= 200:
258258
# Small repo, get a decent amount of history
259-
calculated_depth = 500
259+
calculated_depth = 200
260260
elif total_branches_tags <= 1000:
261261
# Medium repo, get a moderate amount of history
262-
calculated_depth = 300
262+
calculated_depth = 100
263263
elif total_branches_tags <= 5000:
264264
# Large repo, get less history
265-
calculated_depth = 20
265+
calculated_depth = 10
266266
else:
267267
# Very large repo, get a minimal history
268-
calculated_depth = 10
268+
calculated_depth = 5
269269
self.logger.info(
270270
f"total_branches_tags={total_branches_tags}, calculated_depth={calculated_depth}"
271271
)

services/apps/git_integration/src/crowdgit/services/commit/commit_service.py

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import time
88
import uuid
99
from concurrent.futures import ProcessPoolExecutor
10+
from concurrent.futures.process import BrokenProcessPool
1011
from decimal import Decimal
1112
from typing import Any
1213

@@ -69,6 +70,7 @@ def _get_or_create_pool(self) -> ProcessPoolExecutor:
6970
def cleanup_process_pool(self):
7071
"""Cleanup process pool to prevent resource leaks"""
7172
if self.process_pool:
73+
self.logger.info("Cleaning up process pool")
7274
self.process_pool.shutdown(wait=False)
7375
self.process_pool = None
7476

@@ -659,26 +661,44 @@ async def _process_activities_from_commits(
659661

660662
executor = self._get_or_create_pool()
661663

662-
futures = [
663-
loop.run_in_executor(
664-
executor,
665-
CommitService.process_commits_chunk,
666-
chunk,
667-
repo_path,
668-
edge_commit_hash,
669-
remote,
670-
segment_id,
671-
integration_id,
672-
)
673-
for chunk in chunks
674-
]
664+
try:
665+
futures = [
666+
loop.run_in_executor(
667+
executor,
668+
CommitService.process_commits_chunk,
669+
chunk,
670+
repo_path,
671+
edge_commit_hash,
672+
remote,
673+
segment_id,
674+
integration_id,
675+
)
676+
for chunk in chunks
677+
]
678+
self.logger.info(f"Submitted {len(futures)} tasks to process pool")
679+
except Exception as e:
680+
if isinstance(e, BrokenProcessPool):
681+
self.logger.warning("BrokenProcessPool during task submission, cleaning up")
682+
self.cleanup_process_pool()
683+
raise
675684

676685
# Save each chunk's activities as they complete
686+
completed_chunks = 0
677687
for future in asyncio.as_completed(futures):
678-
chunk_activities_db, chunk_activities_queue = await future
679-
if chunk_activities_db and chunk_activities_queue:
680-
await batch_insert_activities(chunk_activities_db)
681-
await self.queue_service.send_batch_activities(chunk_activities_queue)
688+
try:
689+
chunk_activities_db, chunk_activities_queue = await future
690+
completed_chunks += 1
691+
self.logger.info(f"Chunk {completed_chunks}/{len(futures)} completed")
692+
if chunk_activities_db and chunk_activities_queue:
693+
await batch_insert_activities(chunk_activities_db)
694+
await self.queue_service.send_batch_activities(chunk_activities_queue)
695+
except Exception as e:
696+
if isinstance(e, BrokenProcessPool):
697+
self.logger.warning(
698+
f"BrokenProcessPool after {completed_chunks}/{len(futures)} chunks, cleaning up"
699+
)
700+
self.cleanup_process_pool()
701+
raise
682702

683703
@staticmethod
684704
def _validate_commit_structure(commit_lines: list[str]) -> bool:

services/apps/git_integration/src/crowdgit/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def load_env_var(key: str, required=True, default=None):
1818
REPOSITORY_UPDATE_INTERVAL_HOURS = int(
1919
load_env_var("REPOSITORY_UPDATE_INTERVAL_HOURS", default=24)
2020
)
21-
MAX_WORKER_PROCESSES = int(load_env_var("MAX_WORKER_PROCESSES", default=min(4, os.cpu_count())))
21+
MAX_WORKER_PROCESSES = int(load_env_var("MAX_WORKER_PROCESSES", default=min(3, os.cpu_count())))
2222
DEFAULT_TENANT_ID = load_env_var(
2323
"CROWD_SSO_LF_TENANT_ID", default="875c38bd-2b1b-4e91-ad07-0cfbabb4c49f"
2424
)

0 commit comments

Comments
 (0)