Skip to content

Adapt to plumpy's greenlet async bridge#7188

Closed
khsrali wants to merge 11 commits into
aiidateam:mainfrom
khsrali:greenlet
Closed

Adapt to plumpy's greenlet async bridge#7188
khsrali wants to merge 11 commits into
aiidateam:mainfrom
khsrali:greenlet

Conversation

@khsrali
Copy link
Copy Markdown
Collaborator

@khsrali khsrali commented Jan 28, 2026

This accompanies the plumpy PR that replaces nest_asyncio with greenlet-based bridging.
AiiDA has several places where synchronous code needs to call async operations while the event loop is running — process execution, the kill path, and async transport operations. Each needs adapting to work within the greenlet model.

Additionally, dropping nest_asyncio breaks Jupyter notebooks --there's absolutely no way not to break it!--:
The kernel has a running event loop, there's no greenlet context.

The only backward incompatible change, In notebooks, users write:

result = await multiply.run_async(x, y)
result = await engine.run_async(builder)

Since Jupyter supports top-level await, this works naturally. The async path calls run_in_greenlet internally, so all the nested execution machinery works correctly. That means no need for asyncio.run(), etc from the user side.
We solve this by providing an async API in line with sync ones. I believe this is a more decent solution over the patching or working around the event loop, as it was before.

Anyways, in scripts (where the loop is NOT running), the synchronous API still works as before:

result = multiply(x, y)
result = engine.run(builder)

@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 28, 2026

Codecov Report

❌ Patch coverage is 34.53237% with 91 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.48%. Comparing base (be364ad) to head (74984a2).
⚠️ Report is 122 commits behind head on main.

Files with missing lines Patch % Lines
src/aiida/engine/processes/functions.py 16.67% 45 Missing ⚠️
src/aiida/engine/runners.py 15.79% 32 Missing ⚠️
src/aiida/engine/launch.py 33.34% 12 Missing ⚠️
src/aiida/transports/transport.py 92.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7188      +/-   ##
==========================================
- Coverage   79.66%   79.48%   -0.17%     
==========================================
  Files         565      565              
  Lines       43637    43769     +132     
==========================================
+ Hits        34759    34786      +27     
- Misses       8878     8983     +105     

☔ 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.

@khsrali
Copy link
Copy Markdown
Collaborator Author

khsrali commented Jan 28, 2026

I think the tests are failing because if yet another nested loop in transport:

src/aiida/transports/transport.py:180: in __enter__
    self.open()
src/aiida/transports/transport.py:1838: in open
    return self.run_command_blocking(self.open_async)
src/aiida/transports/transport.py:1835: in run_command_blocking
    return loop.run_until_complete(func(*args, **kwargs))
src/aiida/engine/runners.py:160: in run_until_complete
    return self._loop.run_until_complete(future)

I hope we can hand this over to plumpy, otherwise we may need to greenlet it inside aiida-core
to be investigated...

EDIT:
Problem solved

@khsrali
Copy link
Copy Markdown
Collaborator Author

khsrali commented Jan 29, 2026

Ok, it works now! 🎊

@khsrali khsrali changed the title greenlet integration Adapt to plumpy's greenlet async bridge Feb 2, 2026
@khsrali khsrali marked this pull request as ready for review February 2, 2026 10:44
@khsrali khsrali removed the request for review from unkcpz February 2, 2026 10:45
@khsrali
Copy link
Copy Markdown
Collaborator Author

khsrali commented Feb 17, 2026

closed in favour of #7206

@khsrali khsrali closed this Feb 17, 2026
@khsrali khsrali deleted the greenlet branch February 17, 2026 15:07
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.

1 participant