Skip to content

Keep the message window around until all remaining threads complete.#16944

Merged
seanbudd merged 1 commit into
masterfrom
i16933-2
Aug 2, 2024
Merged

Keep the message window around until all remaining threads complete.#16944
seanbudd merged 1 commit into
masterfrom
i16933-2

Conversation

@michaelDCurran
Copy link
Copy Markdown
Member

@michaelDCurran michaelDCurran commented Aug 2, 2024

Link to issue number:

Improves fix for #16933
Improves upon pr #16934

Summary of the issue:

In PR #16934, it was ensured that NVDA's mutex would not be released until all remaining non-daemon threads were joined and completed. Otherwise, the NVDA process may stay around because of remaining background threads, such as the Braille auto detector worker thread.
However, the joining of the threads was done after NVDA's message window was destroyed, therefore making it impossible for a new instance of NVDA to locate and kill off the old NVDA if it truly was taking way too long.

Description of user facing changes

An old NvDA has more chance of being killed off if it is taking too long to exit when a new copy of NVDA is trying to start.

Description of development approach

Move the joining of the non-daemon threads out of nvda.pyw, and into the bottom of core.main. Also ensure that destroying the message window is the very last action taken. So the ordering of the end of core.main is now:

  • terminate all subsystems
  • Join threads
  • destroy the message window.

Testing strategy:

Reproduced steps in #16933.
Temporarily added a time.sleep(10) after joining the threads, and ensured that a new copy of NVDA could successfully kill off the old copy.

Known issues with pull request:

None known.

Code Review Checklist:

  • Documentation:
    • Change log entry
    • User Documentation
    • Developer / Technical Documentation
    • Context sensitive help for GUI changes
  • Testing:
    • Unit tests
    • System (end to end) tests
    • Manual testing
  • UX of all users considered:
    • Speech
    • Braille
    • Low Vision
    • Different web browsers
    • Localization in other languages / culture than English
  • API is compatible with existing add-ons.
  • Security precautions taken.

Summary by CodeRabbit

  • New Features

    • Enhanced shutdown process for the NVDA application, improving thread management and cleanup.
    • Adjusted sequence of operations for a more robust exit behavior.
  • Bug Fixes

    • Addressed potential issues with the NVDA process remaining active after shutdown due to non-daemon threads.
  • Chores

    • Simplified the application's shutdown logic by removing unnecessary threading checks.

@michaelDCurran michaelDCurran requested a review from a team as a code owner August 2, 2024 02:00
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 2, 2024

Walkthrough

The recent changes enhance the shutdown process of the NVDA application by adjusting thread management. In source/core.py, a loop was added to manage non-daemon threads during shutdown, preventing the application from remaining active post-exit. Conversely, source/nvda.pyw removed the logic for waiting on non-daemon threads, potentially simplifying the exit process but risking unintended behavior if threads remain active. These modifications aim to improve application robustness and resource management during termination.

Changes

Files Change Summary
source/core.py Improved thread management during shutdown by ensuring non-daemon threads are joined before exit.
source/nvda.pyw Removed threading logic that waited for non-daemon threads to finish, simplifying the shutdown process.

Recent review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a498a26 and 4a71e35.

Files selected for processing (2)
  • source/core.py (1 hunks)
  • source/nvda.pyw (2 hunks)
Additional context used
Path-based instructions (2)
source/nvda.pyw (1)

Pattern **/*: Focus on code smells, logic errors, edge cases, missing test cases, security flaws and serious issues. Avoid commenting on minor issues such as linting, formatting and style issues. This project uses tabs instead of spaces, do not suggest usage of spaces over tabs. Are there any 'red flags' in this code that might warrant closer investigation from a security standpoint? Explain what makes them suspicious. When providing code suggestions, particularly when requested, ensure GitHub's suggestion format is used, i.e.: suggestion <code changes>

source/core.py (2)

Pattern **/*: Focus on code smells, logic errors, edge cases, missing test cases, security flaws and serious issues. Avoid commenting on minor issues such as linting, formatting and style issues. This project uses tabs instead of spaces, do not suggest usage of spaces over tabs. Are there any 'red flags' in this code that might warrant closer investigation from a security standpoint? Explain what makes them suspicious. When providing code suggestions, particularly when requested, ensure GitHub's suggestion format is used, i.e.: suggestion <code changes>


Pattern **/*.py: _, pgettext, ngettext, and ngettext are defined globally, errors for this being undefined can be ignored.

Learnings (2)
Common learnings
Learnt from: michaelDCurran
PR: nvaccess/nvda#16934
File: source/nvda.pyw:0-0
Timestamp: 2024-07-31T08:16:00.304Z
Learning: When handling non-daemon threads before exiting NVDA, it is crucial to join all threads without a timeout to avoid instability and ensure that the process exits cleanly.
source/nvda.pyw (1)
Learnt from: michaelDCurran
PR: nvaccess/nvda#16934
File: source/nvda.pyw:0-0
Timestamp: 2024-07-31T08:16:00.304Z
Learning: When handling non-daemon threads before exiting NVDA, it is crucial to join all threads without a timeout to avoid instability and ensure that the process exits cleanly.
Additional comments not posted (2)
source/nvda.pyw (1)

Line range hint 1087-1099:
Ensure all non-daemon threads are properly handled before releasing the mutex.

The removal of the logic for joining non-daemon threads might lead to unintended behavior if threads remain active. The mutex should not be released until all non-daemon threads have completed. Verify that all non-daemon threads are properly handled in the new shutdown sequence.

Verification successful

Non-daemon threads are properly handled before releasing the mutex.

The logic for joining non-daemon threads is present in source/core.py, ensuring that all non-daemon threads are joined correctly before the shutdown process completes.

  • source/core.py, lines:
    for thr in threading.enumerate():
        if not thr.daemon and thr is not threading.current_thread():
            log.info(f"Waiting on {thr}...")
            thr.join()
            log.info(f"Thread {thr.name} complete")
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all non-daemon threads are properly handled before releasing the mutex.

# Test: Search for the function usage. Expect: Proper handling of non-daemon threads in the new shutdown sequence.
rg --type python -A 5 $'threading.enumerate()'

Length of output: 78


Script:

#!/bin/bash
# Description: Verify all non-daemon threads are properly handled before releasing the mutex.

# Test: Search for the function usage. Expect: Proper handling of non-daemon threads in the new shutdown sequence.
rg --type py -A 5 'threading.enumerate()'

Length of output: 932

source/core.py (1)

1087-1099: LGTM! Ensure thorough testing of the shutdown sequence.

The added logic ensures that all non-daemon threads are joined before releasing the mutex. This change improves the robustness of the shutdown sequence.

Ensure that the shutdown sequence is thoroughly tested to verify the proper handling of non-daemon threads.


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

@seanbudd seanbudd merged commit 5ecc243 into master Aug 2, 2024
@seanbudd seanbudd deleted the i16933-2 branch August 2, 2024 04:54
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