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 result source was never awaited if clients disconnect fast #3687

Merged

Conversation

DoctorJohn
Copy link
Member

@DoctorJohn DoctorJohn commented Nov 1, 2024

Description

While working on #3685, pytest showed me a warning stating RuntimeWarning: coroutine 'Schema.subscribe' was never awaited while testing WebSockets. The source of the issue lies in the following sequence:

  1. create an awaitable (result_source)
  2. yield from the event loop
  3. await result_source in a task

If the WebSocket client disconnects after we yield from the event loop and before we await result_source, the task will be cancelled and the awaitable will never be awaited. This PR fixes this issue by not yielding anymore between creating and awaiting the awaitable.

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

Summary by Sourcery

Fix coroutine awaiting issue in WebSocket protocol handlers by ensuring awaitables are awaited even if clients disconnect quickly.

Bug Fixes:

  • Fix issue where coroutines in WebSocket protocol handlers were not awaited if clients disconnected shortly after starting an operation.

Documentation:

  • Add a release note indicating a patch release to fix coroutine awaiting issue in WebSocket protocol handlers.

Copy link
Contributor

sourcery-ai bot commented Nov 1, 2024

Reviewer's Guide by Sourcery

This PR fixes a race condition in WebSocket protocol handlers where coroutines could remain unawaited if clients disconnect quickly. The implementation restructures the operation handling flow to ensure that awaitables are handled immediately after creation, preventing any potential gaps where cancellation could leave coroutines unawaited.

Sequence diagram for WebSocket operation handling

sequenceDiagram
    actor Client
    participant Handler as WebSocketHandler
    participant Schema

    Client->>Handler: Send SubscribeMessage
    Handler->>Handler: Create Operation
    Handler->>Schema: Subscribe/Execute Query
    Schema-->>Handler: Return Awaitable
    Handler->>Handler: Await Result Source
    alt Client disconnects quickly
        Handler->>Handler: Cancel Task
    else Client remains connected
        Handler->>Client: Send Results
    end
Loading

File-Level Changes

Change Details Files
Refactored GraphQL Transport WS protocol handler to prevent unawaited coroutines
  • Moved operation execution logic into a separate run_operation method
  • Enhanced Operation class with additional fields to store query details
  • Eliminated the yield gap between creating and awaiting result_source
  • Consolidated operation creation and task scheduling
strawberry/subscriptions/protocols/graphql_transport_ws/handlers.py
Restructured GraphQL WS protocol handler to prevent unawaited coroutines
  • Merged result source creation and handling into a single method
  • Simplified the handle_async_results method signature
  • Moved schema.subscribe call directly into the handler method
strawberry/subscriptions/protocols/graphql_ws/handlers.py
Added release notes for the bugfix
  • Created release notes documenting the WebSocket protocol handler coroutine fix
RELEASE.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @DoctorJohn - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟡 Documentation: 1 issue found

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

RELEASE.md Show resolved Hide resolved
@botberry
Copy link
Member

botberry commented Nov 1, 2024

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


This release fixes the issue that some coroutines in the WebSocket protocol handlers were never awaited if clients disconnected shortly after starting an operation.

Here's the tweet text:

🆕 Release (next) is out! Thanks to @NucleonJohn for the PR 👏

Get it here 👉 https://strawberry.rocks/release/(next)

Copy link

codecov bot commented Nov 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.01%. Comparing base (6839371) to head (9f94cbf).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3687      +/-   ##
==========================================
+ Coverage   96.99%   97.01%   +0.01%     
==========================================
  Files         503      503              
  Lines       33548    33550       +2     
  Branches     5634     5634              
==========================================
+ Hits        32541    32548       +7     
- Misses        795      796       +1     
+ Partials      212      206       -6     

@DoctorJohn DoctorJohn requested a review from patrick91 November 1, 2024 18:00
Copy link

codspeed-hq bot commented Nov 1, 2024

CodSpeed Performance Report

Merging #3687 will not alter performance

Comparing DoctorJohn:fix-result-source-memory-leak (9f94cbf) with main (6839371)

Summary

✅ 15 untouched benchmarks

@DoctorJohn DoctorJohn requested a review from bellini666 November 3, 2024 19:50
@DoctorJohn DoctorJohn merged commit 3561948 into strawberry-graphql:main Nov 5, 2024
112 of 113 checks passed
@DoctorJohn DoctorJohn deleted the fix-result-source-memory-leak branch November 20, 2024 15:58
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