Skip to content

Fix asyncpg event loop bug with startup wrapper patch - #23

Merged
openshift-merge-bot[bot] merged 1 commit into
openstack-k8s-operators:mainfrom
omkarjoshi0304:fix/asyncpg-event-loop-workaround
Jul 21, 2026
Merged

Fix asyncpg event loop bug with startup wrapper patch#23
openshift-merge-bot[bot] merged 1 commit into
openstack-k8s-operators:mainfrom
omkarjoshi0304:fix/asyncpg-event-loop-workaround

Conversation

@omkarjoshi0304

@omkarjoshi0304 omkarjoshi0304 commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

The first request to llama-stack after a pod start fails with RuntimeError because asyncpg connections created during StackApp init are bound to a temporary event loop. This adds a Python wrapper script that patches StackApp.init to lazily recreate SQL engines on uvicorn's event loop, matching the upstream fix (PR #5837) that hasn't been built into the container image yet.

Remove when the container image includes the upstream fix.

@openshift-ci
openshift-ci Bot requested review from lpiwowar and umago July 16, 2026 12:08
@omkarjoshi0304
omkarjoshi0304 force-pushed the fix/asyncpg-event-loop-workaround branch from c2c40d7 to 27b0220 Compare July 16, 2026 12:23
@omkarjoshi0304

Copy link
Copy Markdown
Collaborator Author

/retest

@omkarjoshi0304
omkarjoshi0304 marked this pull request as draft July 16, 2026 13:13
@omkarjoshi0304 omkarjoshi0304 changed the title Fix asyncpg event loop bug with startup wrapper monkey-patch Fix asyncpg event loop bug with startup wrapper patch Jul 16, 2026
@omkarjoshi0304
omkarjoshi0304 force-pushed the fix/asyncpg-event-loop-workaround branch from 27b0220 to 3f98be8 Compare July 16, 2026 13:21
@omkarjoshi0304
omkarjoshi0304 marked this pull request as ready for review July 16, 2026 13:29
@openshift-ci
openshift-ci Bot requested a review from Akrog July 16, 2026 13:29
@omkarjoshi0304
omkarjoshi0304 force-pushed the fix/asyncpg-event-loop-workaround branch from 3f98be8 to 378cb0c Compare July 16, 2026 16:11
umago
umago previously approved these changes Jul 17, 2026

@umago umago left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks Omkar, tested it locally and it works.

The code does look "black magic"-ish but this is a temporary workaround so, I don't think we need to spend too much time on it.

Thanks! I will not merge it immediately in case someone else from the team wants to take a look as people may have different opinions on this but, if nobody else review this, I'm happy to merge it later too. Let me know!

@lpiwowar lpiwowar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Overall LGTM, thank you for fixing this:). It's nice this will get fixed.

I have just two questions I personally need answered before I would feel comfortable with /lgtm. Both feel important to me. So I'm marking this with "Request changes". But once they are answered I'm 💯 OK with merging immediately:).

Comment thread internal/controller/assets/llama_startup_wrapper.py
Comment thread internal/controller/assets/llama_startup_wrapper.py
@omkarjoshi0304
omkarjoshi0304 force-pushed the fix/asyncpg-event-loop-workaround branch from 378cb0c to 25f6848 Compare July 20, 2026 09:49
@omkarjoshi0304

Copy link
Copy Markdown
Collaborator Author

/retest

@omkarjoshi0304
omkarjoshi0304 force-pushed the fix/asyncpg-event-loop-workaround branch from 25f6848 to dafdda9 Compare July 20, 2026 13:06
@omkarjoshi0304
omkarjoshi0304 force-pushed the fix/asyncpg-event-loop-workaround branch from dafdda9 to 335b87f Compare July 20, 2026 16:06
@openshift-ci openshift-ci Bot removed the approved label Jul 20, 2026
@openshift-ci

openshift-ci Bot commented Jul 20, 2026

Copy link
Copy Markdown

@omkarjoshi0304: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
ci/prow/openstack-lightspeed-kuttl-4-18 335b87f link true /test openstack-lightspeed-kuttl-4-18

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

StackApp.__init__() runs Stack.initialize() in a temporary event loop
via ThreadPoolExecutor + asyncio.run(). Any asyncpg connections created
during init get bound to this temp loop, causing the first request on
uvicorn's real event loop to fail with:
  RuntimeError: Task got Future attached to a different loop

The upstream fix (llama-stack PR #5837) adds reset_engine() to null out
_engine and async_session after init, relying on the existing
_ensure_engine() method to lazily recreate them on the correct loop.

The current container image has neither reset_engine() nor
_ensure_engine() — the engine and session are created once in __init__
and never checked again. Setting async_session to None alone crashes
with TypeError because there is no lazy recreation path. The
SessionMaker class provides that missing lazy recreation externally —
on the first call it recreates the engine on the correct event loop,
then replaces itself with the real session maker.

Remove this workaround when the container image includes PR #5837.
@omkarjoshi0304
omkarjoshi0304 force-pushed the fix/asyncpg-event-loop-workaround branch from 335b87f to 52e77fb Compare July 21, 2026 08:48
@lpiwowar
lpiwowar self-requested a review July 21, 2026 09:10

@lpiwowar lpiwowar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

/lgtm

Thank you:), let's see what the jobs think.

@openshift-ci

openshift-ci Bot commented Jul 21, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: lpiwowar, omkarjoshi0304

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-merge-bot
openshift-merge-bot Bot merged commit 903638a into openstack-k8s-operators:main Jul 21, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants