Skip to content

Commit aef18f2

Browse files
seer-by-sentry[bot]getsantry[bot]
authored andcommitted
fix(issue summary): Close database connections after issue summary generation (#97555)
Fixes [SENTRY-4BWM](https://sentry.io/organizations/sentry/issues/6793787515/). The issue was that: ThreadPoolExecutor's new threads request database connections, exhausting PostgreSQL's max_client_conn due to unreleased connections. - Wraps the `get_issue_summary` function call in a thread with a function `_get_issue_summary_with_cleanup` to ensure database connections are closed after the thread execution. - This prevents connection leaks when running in a thread. This fix was generated by Seer in Sentry, triggered by Rohan Agarwal. 👁️ Run ID: 734760 Not quite right? [Click here to continue debugging with Seer.](https://sentry.io/organizations/sentry/issues/6793787515/?seerDrawer=true) <!-- Sentry employees and contractors can delete or ignore the following. --> ### Legal Boilerplate Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms. --------- Co-authored-by: seer-by-sentry[bot] <157164994+seer-by-sentry[bot]@users.noreply.github.com> Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com>
1 parent 772e2af commit aef18f2

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/sentry/integrations/utils/issue_summary_for_alerts.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Any
44

55
import sentry_sdk
6+
from django.db import close_old_connections
67

78
from sentry import features, options
89
from sentry.issues.grouptype import GroupCategory
@@ -15,6 +16,19 @@
1516
logger = logging.getLogger(__name__)
1617

1718

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+
1832
def fetch_issue_summary(group: Group) -> dict[str, Any] | None:
1933
"""
2034
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:
5266
with sentry_sdk.start_span(op="ai_summary.fetch_issue_summary_for_alert"):
5367
with concurrent.futures.ThreadPoolExecutor() as executor:
5468
future = executor.submit(
55-
get_issue_summary, group, source=SeerAutomationSource.ALERT
69+
_get_issue_summary_with_cleanup, group, SeerAutomationSource.ALERT
5670
)
5771
summary_result, status_code = future.result(timeout=timeout)
5872

0 commit comments

Comments
 (0)