Skip to content

Conversation

maffoo
Copy link
Contributor

@maffoo maffoo commented Mar 12, 2024

When using grpc with asyncio from multiple threads, spurious PollerCompletionQueue errors are printed due to multiple event loops listening a socket to be notified of grpc events. More than one event loop may be woken when the completion queue writes a byte to the notification socket, but only one of the loops receives the data and the others raise a BlockingIOError. This doesn't actually cause a problem in the grpc still works with asyncio in multiple threads, but lots of spurious error messages are printed by the default exception handler. (See grpc/grpc#25364 for discussion of the issue.)

This configures the asyncio event loop used for grpc with an exception handler that ignores these BlockingIOErrors from the PollerCompletionQueue so that we don't spam the logs with scary-looking messages when using grpc with asyncio from multiple threads.

@maffoo maffoo requested review from a team, cduck, verult, vtomole and wcourtney as code owners March 12, 2024 05:19
@CirqBot CirqBot added the Size: XS <10 lines changed label Mar 12, 2024

@staticmethod
async def _main(loop_future: duet.AwaitableFuture) -> None:
def handle_exception(loop, context) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this function called somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, forgot to actually set this as the exception handler. Fixed.

@maffoo maffoo requested a review from verult March 12, 2024 17:51
@maffoo
Copy link
Contributor Author

maffoo commented Mar 12, 2024

Note: I filed grpc/grpc#36096 to fix these errors upstream. We'll see what comes of that, but in any case it will be a while before we can rely on that and so I think silencing these errors in our own event loops makes sense.

@github-actions github-actions bot added the Stale label Apr 20, 2024
@github-actions github-actions bot removed the Stale label May 6, 2024
@quantumlib quantumlib deleted a comment Jun 7, 2024
@github-actions github-actions bot added Stale and removed Stale labels Oct 11, 2024
@dstrain115
Copy link
Collaborator

@maffoo Is this still something we want to do? Should we close this old PR or push it over the finish line?

@maffoo
Copy link
Contributor Author

maffoo commented Jan 14, 2025

I still think this is nice to have since the error messages look quite scary when printed even though everything is working fine. The error messages only happen when using grpc from multiple threads, which is somewhat niche, but note that this can happen even for users who are not themselves using multiple threads, for example if they use something like a cloud library that itself makes grpc calls in a background thread.

If we decide to go ahead I will rebase this to get it up to date with main.

@pavoljuhas pavoljuhas added the priority/before-1.5 Finish before the Cirq 1.5 release label Apr 4, 2025
@pavoljuhas pavoljuhas self-assigned this Apr 4, 2025
Copy link
Collaborator

@pavoljuhas pavoljuhas left a comment

Choose a reason for hiding this comment

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

LGTM if still relevant. Thank you!

Copy link

codecov bot commented Apr 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.65%. Comparing base (02941bf) to head (be0bc43).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6492      +/-   ##
==========================================
- Coverage   98.65%   98.65%   -0.01%     
==========================================
  Files        1106     1106              
  Lines       95869    95871       +2     
==========================================
+ Hits        94581    94582       +1     
- Misses       1288     1289       +1     

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mhucka mhucka self-assigned this Apr 6, 2025
@mhucka mhucka added this pull request to the merge queue Apr 7, 2025
@pavoljuhas pavoljuhas removed this pull request from the merge queue due to a manual request Apr 7, 2025
For now skip test coverage of the new error handler as the
error happens in multi-threaded execution only.
@pavoljuhas pavoljuhas enabled auto-merge April 7, 2025 17:41
@pavoljuhas pavoljuhas added this pull request to the merge queue Apr 7, 2025
Merged via the queue into main with commit 9fd4751 Apr 7, 2025
42 checks passed
@pavoljuhas pavoljuhas deleted the u/maffoo/aio-exc branch April 7, 2025 18:12
BichengYing pushed a commit to BichengYing/Cirq that referenced this pull request Jun 20, 2025
…tumlib#6492)

For now skip test coverage of the new error handler as the
error happens in multi-threaded execution only.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/async area/google/protos priority/before-1.5 Finish before the Cirq 1.5 release Size: XS <10 lines changed

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

6 participants