Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion aws_lambda_powertools/utilities/batch/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,16 @@ async def async_process_closure():
# whether we create an event loop (Lambda) or schedule it as usual (non-Lambda)
coro = async_process_closure()
if os.getenv(constants.LAMBDA_TASK_ROOT_ENV):
loop = asyncio.get_event_loop() # NOTE: this might return an error starting in Python 3.12 in a few years
# In Python 3.12+, asyncio.get_event_loop() raises a warning when called without a running loop
# In Python 3.14+, it will raise an exception if no event loop is running
# Use try/except to handle both the current behavior and future Python versions
try:
loop = asyncio.get_running_loop()
except RuntimeError:
# No running loop, create a new one for Lambda environment
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
Comment on lines +138 to +143
Copy link
Preview

Copilot AI Sep 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic is incorrect. asyncio.get_running_loop() is used to get an already running event loop, but in a Lambda environment without an active loop, you should directly create a new one. The correct approach is to use asyncio.get_event_loop() replacement pattern with asyncio.new_event_loop() and asyncio.set_event_loop() or use asyncio.run() for the coroutine execution.

Copilot uses AI. Check for mistakes.

Comment on lines +138 to +143
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this code block is basically doing nothing with this try/except: trying to get a loop running or create a new one, but since Lambda doesn't have an active loop, it mostly falls into the "except" block, right? I haven't tested it yet, but I'm not sure if this is the correct implementation for it.


task_instance = loop.create_task(coro)
return loop.run_until_complete(task_instance)

Expand Down