Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

cgoldberg
Copy link
Contributor

@cgoldberg cgoldberg commented Sep 8, 2025

User description

πŸ”— Related Issues

Fixes #16294

πŸ’₯ What does this PR do?

This PR makes the threads used for WebSockets into "daemon threads". This probably doesn't make much practical difference, but it will alleviate an annoying condition where you just want to exit a program, but for some reason the WebSocket connection or callback is hanging.

With the daemon flag, all threads will be abruptly killed on interpreter shutdown.

See: #16294 for more information

πŸ”„ Types of changes

  • Bug fix (backwards compatible)

PR Type

Bug fix


Description

  • Convert WebSocket threads to daemon threads for proper shutdown

  • Prevents hanging threads from blocking interpreter exit

  • Applies to both main WebSocket connection and callback threads


Diagram Walkthrough

flowchart LR
  A["WebSocket Connection"] --> B["Main Thread"]
  A --> C["Callback Threads"]
  B --> D["Daemon Thread"]
  C --> E["Daemon Threads"]
  D --> F["Clean Shutdown"]
  E --> F
Loading

File Walkthrough

Relevant files
Bug fix
websocket_connection.py
Convert WebSocket threads to daemon threadsΒ  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β 

py/selenium/webdriver/remote/websocket_connection.py

  • Added daemon=True parameter to main WebSocket thread creation
  • Added daemon=True parameter to callback thread creation
  • Ensures threads don't prevent interpreter shutdown
+2/-2Β  Β  Β 

@selenium-ci selenium-ci added the C-py Python Bindings label Sep 8, 2025
Copy link
Contributor

qodo-merge-pro bot commented Sep 8, 2025

PR Reviewer Guide πŸ”

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 πŸ”΅πŸ”΅βšͺβšͺβšͺ
πŸ§ͺΒ No relevant tests
πŸ”’Β No security concerns identified
⚑ Recommended focus areas for review

Behavior Change

Daemonizing the main WebSocket thread means it may terminate abruptly during interpreter shutdown, potentially dropping in-flight messages or leaving cleanup callbacks unexecuted. Confirm this is acceptable for all consumers and that shutdown paths do not rely on graceful thread joins.

self._ws_thread = Thread(target=run_socket, daemon=True)
self._ws_thread.start()
Callback Side Effects

Running callbacks on daemon threads can interrupt user code mid-execution at shutdown, which may cause partial state updates or resource leaks. Validate that callbacks are idempotent, short-lived, and do not require guaranteed completion or critical cleanup.

params = message["params"]
for callback in self.callbacks.get(message["method"], []):
    Thread(target=callback, args=(params,), daemon=True).start()

Copy link
Contributor

qodo-merge-pro bot commented Sep 8, 2025

PR Code Suggestions ✨

No code suggestions found for the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[πŸš€ Feature]: [py] Use daemonic threads in WebSocket connections
2 participants