Skip to content

Commit c5a5a5d

Browse files
authored
fix: close event loops for async tasks (#8013)
1 parent 0cc1aea commit c5a5a5d

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

samcli/lib/utils/async_utils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ def run_async(self, default_executor: bool = True) -> list:
131131
List of result of the executions in order
132132
"""
133133
event_loop = new_event_loop()
134-
if not default_executor:
135-
with ThreadPoolExecutor() as self.executor:
136-
return run_given_tasks_async(self._async_tasks, event_loop, self.executor)
137-
return run_given_tasks_async(self._async_tasks, event_loop)
134+
try:
135+
if not default_executor:
136+
with ThreadPoolExecutor() as self.executor:
137+
return run_given_tasks_async(self._async_tasks, event_loop, self.executor)
138+
return run_given_tasks_async(self._async_tasks, event_loop)
139+
finally:
140+
# Close the event loop to prevent memory leaks
141+
event_loop.close()

0 commit comments

Comments
 (0)