Skip to content

gh-126483: disable warnings filters mutation in concurrent test_check… #132694

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

Conversation

graingert
Copy link
Contributor

@graingert graingert commented Apr 18, 2025

…_check_hostname_idn use by test_ssl_in_multiple_threads
@graingert graingert added 3.13 bugs and security fixes 3.14 bugs and security fixes needs backport to 3.13 bugs and security fixes and removed 3.14 bugs and security fixes 3.13 bugs and security fixes labels Apr 18, 2025
@graingert graingert marked this pull request as ready for review April 18, 2025 18:20
Comment on lines 3160 to 3179
with (
ThreadedEchoServer(context=server_context, chatty=True) as server,
warnings_helper.check_no_resource_warning(self)
if warnings_filters
else nullcontext(),
self.assertRaises(UnicodeError),
):
context.wrap_socket(socket.socket(), server_hostname='.pythontest.net')

with (
ThreadedEchoServer(context=server_context, chatty=True) as server,
warnings_helper.check_no_resource_warning(self)
if warnings_filters
else nullcontext(),
self.assertRaises(UnicodeDecodeError),
):
context.wrap_socket(
socket.socket(),
server_hostname=b'k\xf6nig.idn.pythontest.net',
)
Copy link
Member

Choose a reason for hiding this comment

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

I suggest the following:

def get_warnings_filter_context(self, warnings_filter):
    if warnings_filters:
        return warnings_helper.check_no_resource_warning(self)
    return nullcontext()

so that it can be used as follows:

        with (
            ThreadedEchoServer(context=server_context, chatty=True) as server,
            self.get_warnings_filter_context(warnings_filters),
            self.assertRaises(UnicodeError),
        ):
            context.wrap_socket(socket.socket(), server_hostname='.pythontest.net')

        with (
            ThreadedEchoServer(context=server_context, chatty=True) as server,
            self.get_warnings_filter_context(warnings_filters),
            self.assertRaises(UnicodeDecodeError),
        ):
            context.wrap_socket(
                socket.socket(),
                server_hostname=b'k\xf6nig.idn.pythontest.net',
            )

It took me a while to see the ternary if-else

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add some extra parenthesis, like ruff would

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

It's a good alternative. As I'm on mobile I'll let @gpshead do the merge.

@serhiy-storchaka serhiy-storchaka added the needs backport to 3.14 bugs and security fixes label May 8, 2025
@colesbury colesbury merged commit 40c8be0 into python:main Jun 4, 2025
40 checks passed
@miss-islington-app
Copy link

Thanks @graingert for the PR, and @colesbury for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 4, 2025
…pythonGH-132694)

The `test_ssl_in_multiple_threads` test failed because `test_check_hostname_idn()`
modified the global warnings filters via `warnings_helper.check_no_resource_warning()`.
Only check for warnings when the context aware warnings feature is enabled, which makes
the warnings filter context-local and thread-safe.
(cherry picked from commit 40c8be0)

Co-authored-by: Thomas Grainger <tagrain@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 4, 2025
…pythonGH-132694)

The `test_ssl_in_multiple_threads` test failed because `test_check_hostname_idn()`
modified the global warnings filters via `warnings_helper.check_no_resource_warning()`.
Only check for warnings when the context aware warnings feature is enabled, which makes
the warnings filter context-local and thread-safe.
(cherry picked from commit 40c8be0)

Co-authored-by: Thomas Grainger <tagrain@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Jun 4, 2025

GH-135131 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Jun 4, 2025
@bedevere-app
Copy link

bedevere-app bot commented Jun 4, 2025

GH-135132 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jun 4, 2025
colesbury pushed a commit that referenced this pull request Jun 4, 2025
GH-132694) (GH-135131)

The `test_ssl_in_multiple_threads` test failed because `test_check_hostname_idn()`
modified the global warnings filters via `warnings_helper.check_no_resource_warning()`.
Only check for warnings when the context aware warnings feature is enabled, which makes
the warnings filter context-local and thread-safe.
(cherry picked from commit 40c8be0)

Co-authored-by: Thomas Grainger <tagrain@gmail.com>
colesbury added a commit that referenced this pull request Jun 4, 2025
GH-132694) (GH-135132)

The `test_ssl_in_multiple_threads` test failed because `test_check_hostname_idn()`
modified the global warnings filters via `warnings_helper.check_no_resource_warning()`.
Disable the warnings check in the multi-threaded test because `warnings_helper` isn't
thread-safe in 3.13 or earlier.
(cherry picked from commit 40c8be0)

Co-authored-by: Thomas Grainger <tagrain@gmail.com>

* Fix for 3.13

---------

Co-authored-by: Thomas Grainger <tagrain@gmail.com>
Co-authored-by: Sam Gross <colesbury@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants