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

Wait for All Callbacks in _emit to Finish, Add More Thread Tracing #415

Merged
merged 1 commit into from
Jun 12, 2024
Merged

Wait for All Callbacks in _emit to Finish, Add More Thread Tracing #415

merged 1 commit into from
Jun 12, 2024

Conversation

davidvonthenen
Copy link
Contributor

@davidvonthenen davidvonthenen commented Jun 12, 2024

Proposed changes

Addresses: #408, #409

This PR:

  • adds more logging to AsyncLiveClient and LiveClient to keep track of threads
  • for AsyncLiveClient._emit, we want to wait for all the callbacks to complete before exiting _emit. This isn't a bug, provided the user doesn't have a callback function that doesn't hang.

Types of changes

What types of changes does your code introduce to the community Python SDK?
Put an x in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update or tests (if none of the other choices apply)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have read the CONTRIBUTING doc
  • I have lint'ed all of my code using repo standards
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

NA

Summary by CodeRabbit

  • New Features
    • Enhanced threading support in live client to improve performance and stability.
  • Bug Fixes
    • Improved handling of event tasks to ensure smoother and more reliable operations.

Copy link
Contributor

coderabbitai bot commented Jun 12, 2024

Walkthrough

The AsyncLiveClient class in async_client.py was updated to improve thread management and debugging. This includes using the threading module, changing thread attributes to Union[asyncio.Task, None], adding debug statements, and modifying the _emit method to handle event tasks with asyncio.create_task and asyncio.gather.

Changes

File Path Change Summary
deepgram/clients/live/v1/async_client.py Updated _listen_thread and _keep_alive_thread to Union[asyncio.Task, None], added debug statements, and modified _emit method to use asyncio.create_task and asyncio.gather.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant AsyncLiveClient
    participant EventHandler
    participant asyncio

    Client->>AsyncLiveClient: Initialize
    AsyncLiveClient->>AsyncLiveClient: _listen_thread = None
    AsyncLiveClient->>AsyncLiveClient: _keep_alive_thread = None

    Client->>AsyncLiveClient: Start listening
    AsyncLiveClient->>AsyncLiveClient: Create _listen_thread (asyncio.create_task)
    AsyncLiveClient->>AsyncLiveClient: Log active threads

    Client->>AsyncLiveClient: Keep alive
    AsyncLiveClient->>AsyncLiveClient: Create _keep_alive_thread (asyncio.create_task)
    AsyncLiveClient->>AsyncLiveClient: Log active threads

    Client->>AsyncLiveClient: Emit event
    AsyncLiveClient->>EventHandler: Create event handler tasks (asyncio.create_task)
    EventHandler->>AsyncLiveClient: Return task
    AsyncLiveClient->>asyncio: Gather tasks (asyncio.gather)
    asyncio->>AsyncLiveClient: Return results
    AsyncLiveClient->>Client: Event handled
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@davidvonthenen davidvonthenen changed the title Add More Thread Tracing Wait for All Callbacks in _emit to Finish, Add More Thread Tracing Jun 12, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
deepgram/clients/live/v1/async_client.py (1)

8-8: Consider using a more specific import for threading if only certain functions are used.

This can help reduce the namespace clutter and potentially improve understanding of which threading features are being utilized.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 3177017 and f95d859.

Files selected for processing (2)
  • deepgram/clients/live/v1/async_client.py (7 hunks)
  • deepgram/clients/live/v1/client.py (5 hunks)
Files skipped from review due to trivial changes (1)
  • deepgram/clients/live/v1/client.py
Additional comments not posted (4)
deepgram/clients/live/v1/async_client.py (4)

53-54: The change to allow _listen_thread and _keep_alive_thread to be None is a good safety measure.

This modification enhances robustness by handling scenarios where these threads might not be initialized yet.


146-150: Adding debug statements to track thread activity before and after significant operations is a good practice.

This will help in diagnosing issues related to thread management and ensure that all threads are accounted for before proceeding.

Also applies to: 161-165


Line range hint 639-660: Properly handling the cancellation of threads during the finish method is crucial.

This ensures that all background activities are terminated gracefully, preventing any potential resource leaks or unfinished transactions.


212-229: Ensure that all tasks created within _emit are awaited properly.

This is crucial to prevent any tasks from being orphaned, which could lead to resource leaks or inconsistent application state.

SandraRodgers
SandraRodgers previously approved these changes Jun 12, 2024
@davidvonthenen davidvonthenen merged commit 195ed28 into deepgram:main Jun 12, 2024
3 checks passed
@davidvonthenen davidvonthenen deleted the wait-for-tasks-in-emit branch June 12, 2024 17:24
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f95d859 and 30aba29.

Files selected for processing (2)
  • deepgram/clients/live/v1/async_client.py (6 hunks)
  • deepgram/clients/live/v1/client.py (5 hunks)
Files skipped from review as they are similar to previous changes (2)
  • deepgram/clients/live/v1/async_client.py
  • deepgram/clients/live/v1/client.py

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.

2 participants