[py] Do not close externally provided log_output streams - #17204
Conversation
Track ownership of log_output and only close streams opened by the Service. This prevents closing user-provided streams such as sys.stdout or sys.stderr. Fixes SeleniumHQ#15629
Review Summary by QodoPrevent closing externally provided log_output streams
WalkthroughsDescription• Track ownership of log_output streams to prevent closing externally provided streams • Only close streams opened internally by Selenium (e.g., file paths) • Preserve user-provided streams like sys.stdout for reuse across multiple WebDriver instances • Add test verifying multiple WebDriver instances can use sys.stdout sequentially File Changes1. py/selenium/webdriver/common/service.py
|
Code Review by Qodo
1. Missing blank line before test
|
cgoldberg
left a comment
There was a problem hiding this comment.
Thanks... looks good... just a few minor things:
- Can you remove the blank lines in your test, so it looks like other tests in that file?
- The CI Lint job is going to fail on this due to some minor formatting issues. You can fix them by running
./scripts/format.sh(if you have bazel setup)... ortox -c ./py/tox.ini -e linting(pip install toxif you need it) - Since this affects the base
Serviceclass, we should probably test it on other browsers. Can you add a similar test to:./py/selenium/webdriver/edge/edge_service_tests.py./py/selenium/webdriver/firefox/firefox_service_tests.py./py/selenium/webdriver/safari/safari_service_tests.py
jit3pam
left a comment
There was a problem hiding this comment.
@cgoldberg I have added more tests and also followed linting suggestions using tox.
Please review and let me know if more changes are required.
Thank you in advance.
cgoldberg
left a comment
There was a problem hiding this comment.
few more tiny changes...
Also, I think it was better the way you were quitting the browser in a finally block in your previous version... Can you re-add that?
I have updated the test methods with |
|
The failure in CI is unrelated.. all the Python tests ran fine. Merging now. |


🔗 Related Issues
Fixes #15629
💥 What does this PR do?
This change fixes an issue where the Service.stop() method closes the stream provided via the log_output parameter even when the stream was not created by Selenium.
I could replicate the errors mentioned in #15629.
When users pass streams such as sys.stdout, the current implementation closes the global stream when the service stops. It causes error in new WebDriver instances that try to use the same stream.
The fix introduces tracking of the ownership of the log_output stream.
With this fix Selenium now closes the stream only if it was opened internally (for example when a file path is provided). Streams provided by the user, such as sys.stdout or other file-like objects, will not be closed by the stop method defined in Service class.
I have also added a test to verify that multiple WebDriver instances can be created sequentially using Service(log_output=sys.stdout) without failure.
🔧 Implementation Notes
💡 Additional Considerations
🔄 Types of changes