Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: FixFuture attached to different loop error on read_database_uri #21641

Merged
merged 8 commits into from
Mar 7, 2025

Conversation

nameexhaustion
Copy link
Collaborator

@nameexhaustion nameexhaustion commented Mar 7, 2025

@github-actions github-actions bot added fix Bug fix python Related to Python Polars rust Related to Rust Polars labels Mar 7, 2025
@ritchie46
Copy link
Member

Hmm.. we also use this logic in polars_cloud. What does nest-async-io different? In polars_cloud I want to do this without dependencies.

Copy link

codecov bot commented Mar 7, 2025

Codecov Report

Attention: Patch coverage is 65.82278% with 54 lines in your changes missing coverage. Please review.

Project coverage is 80.53%. Comparing base (36c49ed) to head (91ac2e2).
Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
py-polars/polars/_utils/nest_asyncio.py 64.70% 41 Missing and 13 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #21641      +/-   ##
==========================================
- Coverage   80.54%   80.53%   -0.01%     
==========================================
  Files        1603     1604       +1     
  Lines      231266   231395     +129     
  Branches     2650     2678      +28     
==========================================
+ Hits       186262   186348      +86     
- Misses      44387    44419      +32     
- Partials      617      628      +11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nameexhaustion
Copy link
Collaborator Author

Hmm.. we also use this logic in polars_cloud. What does nest-async-io different? In polars_cloud I want to do this without dependencies.

nest-asyncio maintains the original loop when executing a nested task. The one we currently have doesn't -

# 1.20
_run_async 1.20.0 asyncio.get_running_loop() is original_loop = True
OK
# 1.21
_run_async 1.21.0 asyncio.get_running_loop() is original_loop = False
ERR: Task <Task pending name='Task-3' coro=<test() running at /Users/nxs/git/polars/.env/y.py:17> cb=[_run_until_complete_cb() at /Users/nxs/.pyenv/versions/3.12.2/lib/python3.12/asyncio/base_events.py:181]> got Future <Task pending name='Task-2' coro=<sleep() running at /Users/nxs/.pyenv/versions/3.12.2/lib/python3.12/asyncio/tasks.py:653>> attached to a different loop

Script -

import polars as pl
import asyncio
from polars.io.database._utils import _run_async


async def main():
    loop = asyncio.get_running_loop()
    task = loop.create_task(asyncio.sleep(0))
    _run_async(test(loop, task))


async def test(original_loop, task):
    print(
        f"_run_async {pl.__version__} {asyncio.get_running_loop() is original_loop = }"
    )
    try:
        await task
    except Exception as e:
        print(f"ERR: {str(e)}")
    else:
        print("OK")


if __name__ == "__main__":
    asyncio.run(main())

The underlying async driver is probably also creating objects that reference the original loop before we enter the nested context.

@nameexhaustion nameexhaustion changed the title fix: Revert to use nest-asyncio fix: Fix error Future attached to different loop on read_database_uri Mar 7, 2025
@nameexhaustion nameexhaustion changed the title fix: Fix error Future attached to different loop on read_database_uri fix: FixFuture attached to different loop error on read_database_uri Mar 7, 2025
@nameexhaustion nameexhaustion marked this pull request as ready for review March 7, 2025 15:15
@ritchie46 ritchie46 merged commit 43989f8 into pola-rs:main Mar 7, 2025
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants