fix: async task leak in SlackAlerting

add_deployment (from scheduler) -> _update_general_settings calls this
function every 30s.

Each call starts a new while True: loop.

/debug/asyncio-tasks tells me there's thousands of active tasks with no names,
which impacts my ability to work out why the application is hung.

I don't think it's a big problem, but it's not helping with debuggability.
This commit is contained in:
Chris West (Faux) 2026-03-20 15:40:13 +00:00
parent b8f7d61400
commit 5e54ac6335
No known key found for this signature in database
GPG key ID: CFCD1136833DDDE4
2 changed files with 18 additions and 2 deletions

View file

@ -115,8 +115,9 @@ class SlackAlerting(CustomBatchLogger):
):
if alerting is not None:
self.alerting = alerting
asyncio.create_task(self.periodic_flush())
self.periodic_started = True
if not self.periodic_started:
asyncio.create_task(self.periodic_flush())
self.periodic_started = True
if alerting_threshold is not None:
self.alerting_threshold = alerting_threshold
if alert_types is not None:

View file

@ -173,6 +173,21 @@ class TestSlackAlerting(unittest.TestCase):
self.slack_alerting.update_values(alerting_args={"slack_alerting": "True"})
assert self.slack_alerting.periodic_started == True
# Repeated calls to update_values should not spawn additional periodic_flush tasks
@patch("asyncio.create_task")
def test_update_values_does_not_leak_periodic_tasks(self, mock_create_task):
mock_create_task.return_value = AsyncMock()
self.slack_alerting.update_values(alerting=["slack"])
assert self.slack_alerting.periodic_started == True
assert mock_create_task.call_count == 1
# Simulate repeated calls from add_deployment scheduler (runs every 30s)
self.slack_alerting.update_values(alerting=["slack"])
self.slack_alerting.update_values(alerting=["slack"])
self.slack_alerting.update_values(alerting_args={"slack_alerting": "True"})
assert mock_create_task.call_count == 1
@patch("litellm.integrations.SlackAlerting.slack_alerting.datetime")
def test_alert_type_in_formatted_message(self, mock_datetime):
# Setup mocks