|
3 | 3 | from typing import Any
|
4 | 4 |
|
5 | 5 | import sentry_sdk
|
| 6 | +from django.db import close_old_connections |
6 | 7 |
|
7 | 8 | from sentry import features, options
|
8 | 9 | from sentry.issues.grouptype import GroupCategory
|
|
15 | 16 | logger = logging.getLogger(__name__)
|
16 | 17 |
|
17 | 18 |
|
| 19 | +def _get_issue_summary_with_cleanup( |
| 20 | + group: Group, source: SeerAutomationSource |
| 21 | +) -> tuple[dict[str, Any], int]: |
| 22 | + """ |
| 23 | + Wrapper for get_issue_summary that ensures database connections are properly closed. |
| 24 | + This is needed when running in a thread to prevent connection leaks. |
| 25 | + """ |
| 26 | + try: |
| 27 | + return get_issue_summary(group, source=source) |
| 28 | + finally: |
| 29 | + close_old_connections() |
| 30 | + |
| 31 | + |
18 | 32 | def fetch_issue_summary(group: Group) -> dict[str, Any] | None:
|
19 | 33 | """
|
20 | 34 | Try to fetch an issue summary with a timeout of 5 seconds.
|
@@ -52,7 +66,7 @@ def fetch_issue_summary(group: Group) -> dict[str, Any] | None:
|
52 | 66 | with sentry_sdk.start_span(op="ai_summary.fetch_issue_summary_for_alert"):
|
53 | 67 | with concurrent.futures.ThreadPoolExecutor() as executor:
|
54 | 68 | future = executor.submit(
|
55 |
| - get_issue_summary, group, source=SeerAutomationSource.ALERT |
| 69 | + _get_issue_summary_with_cleanup, group, SeerAutomationSource.ALERT |
56 | 70 | )
|
57 | 71 | summary_result, status_code = future.result(timeout=timeout)
|
58 | 72 |
|
|
0 commit comments