Skip to content

Fix async UDF event loop starvation in Jupyter#123

Open
kesmit13 wants to merge 16 commits into
mainfrom
fix/async-udf-event-loop-starvation
Open

Fix async UDF event loop starvation in Jupyter#123
kesmit13 wants to merge 16 commits into
mainfrom
fix/async-udf-event-loop-starvation

Conversation

@kesmit13
Copy link
Copy Markdown
Collaborator

@kesmit13 kesmit13 commented May 14, 2026

Summary

  • Async UDFs running under uvicorn in Jupyter notebooks become unresponsive under heavy load due to event loop starvation
  • Root cause: async UDFs ran directly in uvicorn's event loop via asyncio.create_task, competing with HTTP connection handling
  • Fix: dedicated event loop in a background thread for async UDF execution, submitted via run_coroutine_threadsafe() and awaited from the server loop
  • Sync UDFs are unchanged (already offloaded to threads)

Test plan

  • Existing test_ext_func.py tests pass
  • Manual: launch uvicorn from Jupyter, fire 50+ concurrent async UDF calls, confirm responsiveness
  • Verify cancellation: disconnect mid-execution, confirm UDF stops
  • Verify sync UDFs still work identically (regression)

🤖 Generated with Claude Code


Note

Medium Risk
Changes the external-function ASGI invoke path (threading, cancellation, teardown); CI/release behavior is lower risk but affects how packages ship.

Overview
Async external UDFs no longer run on the uvicorn event loop (or via nested asyncio.run in-thread). Every invoke is scheduled with to_thread and runs inside _run_with_graceful_shutdown, which uses a dedicated event loop per call, drains pending tasks/callbacks before close (avoids “Event loop is closed” from httpx/anyio), and wraps the coroutine in _cancellable_run so a threading.Event can cancel work on disconnect/timeout. cancel_event is set when the function task loses the race or in finally.

Release automation: publish.yml triggers on pre-release tag pushes, rewrites pyproject.toml version for test/alpha/beta/rc tags, creates a prerelease GitHub release with built wheels on tag push, uploads assets on release: published, and tightens when PyPI publish runs. fusion-docs.yml runs on release publish and uploads docs with github.event.release.tag_name.

Tests: new test_udf_event_loop.py for shutdown/cancellation; pandas alltypes expectations use runtime string dtype where applicable.

Reviewed by Cursor Bugbot for commit 9787e9b. Bugbot is set up for automated code reviews on this repo. Configure here.

Async UDFs were running directly in uvicorn's event loop via
asyncio.create_task, competing with connection handling under heavy
concurrent load. This caused unresponsiveness when running from Jupyter
notebooks where the event loop is shared.

The fix introduces a dedicated event loop in a background thread for
async UDF execution. Coroutines are submitted via
run_coroutine_threadsafe() and awaited from the server loop, isolating
UDF work from HTTP I/O while preserving cooperative async scheduling
between UDFs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Cancel the concurrent.futures.Future in the UDF loop on
disconnect/timeout so the coroutine is interrupted promptly,
not just at the next cancel_on_event row check.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Comment thread singlestoredb/functions/ext/asgi.py Outdated
…ture

asyncio.create_task() requires a coroutine but asyncio.wrap_future()
returns a Future. Use asyncio.ensure_future() which accepts both.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Newer pandas versions use StringDtype ('str') instead of 'object' for
string columns. Detect the actual dtype at import time and use it in
test assertions. Binary columns remain 'object'.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@mgiannakopoulos mgiannakopoulos left a comment

Choose a reason for hiding this comment

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

🚢 Ship IT!

Comment thread singlestoredb/functions/ext/asgi.py Outdated
Prevents UDF event loop thread leaks when run_udf_app() is called
repeatedly in Jupyter notebooks.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Comment thread singlestoredb/functions/ext/asgi.py Outdated
Tags matching v*-rc*, v*-test*, v*-alpha*, v*-beta* now trigger the
full wheel build pipeline and create a pre-release GitHub Release with
all wheels attached. Production releases also attach wheels to the
existing release before publishing to PyPI.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Comment thread singlestoredb/apps/_python_udfs.py Outdated
- Cancel udf_future when func_task is in pending set after asyncio.wait
- Cancel udf_future in finally block to ensure cleanup on any exit path
- Wrap post-construction code in try/except to call app.shutdown() if
  validation, config, or registration fails after Application is created

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
gh release create doesn't need a git repo when not generating notes.
Use --notes "" for empty release body with just assets attached.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Comment thread singlestoredb/functions/ext/asgi.py Outdated
Comment thread singlestoredb/functions/ext/asgi.py Outdated
kesmit13 and others added 2 commits May 14, 2026 12:00
Changed from push tag trigger (v*.*.*) to release event so it only
runs on published releases, not rc/test/alpha/beta tags.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Move udf_future initialization before input_handler['load']() to
  prevent NameError in finally block if parsing raises
- Lazily create UDF event loop on first async UDF invocation instead
  of unconditionally in __init__, avoiding wasted resources for
  sync-only or metadata-only usage
- Guard shutdown() against None loop/thread

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Comment thread singlestoredb/functions/ext/asgi.py Outdated
gh release create requires a git repo to determine the repository
context even without --generate-notes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
After stopping the event loop and joining the thread, set both
_udf_loop and _udf_thread back to None so that _get_udf_loop()
can safely recreate them if called after shutdown.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
kesmit13 added a commit that referenced this pull request May 15, 2026
Enable asset generation on test/rc/alpha/beta tag pushes without
publishing to PyPI. Also prevents fusion-docs from triggering on
pre-release tags.

Ref: PR #123

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The dedicated shared event loop still caused starvation under concurrent
async UDF calls. Switch to the same model used by sync UDFs: each request
gets its own thread with asyncio.run(), eliminating loop contention.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Comment thread singlestoredb/functions/ext/asgi.py
Wraps the inner coroutine in _cancellable_run which polls cancel_event
and raises CancelledError at the next await (~100ms), ensuring vector
UDFs respect disconnect/timeout signals without waiting for completion.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace asyncio.run() with _run_with_graceful_shutdown() that drains
pending callbacks before closing the loop, preventing RuntimeError from
httpx/anyio TLS cleanup in async UDFs calling OpenAI/LangChain APIs.

Add 17 unit tests covering graceful shutdown, cancellation timing,
exception propagation, context variable isolation, and concurrent safety.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default mode and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 2f819f5. Configure here.

if cancel_check in done:
task.cancel()
raise asyncio.CancelledError()
return task.result()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Successful UDF result discarded when cancel races completion

Medium Severity

_cancellable_run unconditionally prioritizes cancellation over a successful result when both tasks complete in the same event loop iteration. asyncio.wait(return_when=FIRST_COMPLETED) can return both task and cancel_check in done if they finish simultaneously. Because if cancel_check in done is checked without first checking whether task also succeeded, a completed UDF result is silently discarded and replaced with CancelledError. Checking task in done first (and returning its result) would preserve the successfully-computed value.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 2f819f5. Configure here.

Parses git tag suffixes (-test, -alpha, -beta, -rc) and patches
pyproject.toml with the corresponding PEP 440 version before building
wheels. Full releases (no suffix) are unaffected.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants