8000 gh-126881: store `debug` flag for event loops as a boolean by picnixz · Pull Request #126901 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-126881: store debug flag for event loops as a boolean #126901

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix a crash when finalizing asyncio event loops.
  • Loading branch information
picnixz committed Nov 16, 2024
commit 45985d179056382797579a35b1d629b318bb9b5f
4 changes: 3 additions & 1 deletion Lib/asyncio/base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2052,7 +2052,9 @@ def get_debug(self):
return self._debug

def set_debug(self, enabled):
self._debug = enabled
# Storing a non-boolean 'debug' flag causes a crash upon finalization.
# See: https://github.com/python/cpython/issues/126881.
self._debug = enabled = bool(enabled)

if self.is_running():
self.call_soon_threadsafe(self._set_coroutine_origin_tracking, enabled)
32 changes: 32 additions & 0 deletions Lib/test/test_asyncio/test_base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import platform
import socket
import sys
import textwrap
import threading
import time
import unittest
Expand Down Expand Up @@ -393,6 +394,37 @@ def test_set_debug(self):
self.loop.set_debug(False)
self.assertFalse(self.loop.get_debug())

def test_set_debug_non_boolean(self):
# Assert that set_debug with a non-boolean value does not crash.
#
# In order to check that the crash does not happen anymore, we
# deliberately leave the loop unclosed. Note that the loop will
# be closed automatically when calling __del__() but a warning
# will be emitted.
#
# See: https://github.com/python/cpython/issues/126881.
code = textwrap.dedent("""
from asyncio.base_events import BaseEventLoop
loop = BaseEventLoop()
loop.set_debug(0.0005)
loop._run_forever_setup()
loop.__del__()
""")
_, stdout, stderr = assert_python_ok('-c', code)
self.assertEqual(stdout, b'')
self.assertIn(b'ResourceWarning: unclosed event loop', stderr)

code = textwrap.dedent("""
from asyncio.base_events import BaseEventLoop
loop = BaseEventLoop()
loop.set_debug([])
loop._run_forever_setup()
loop.__del__()
""")
_, stdout, stderr = assert_python_ok('-c', code)
self.assertEqual(stdout, b'')
self.assertIn(b'ResourceWarning: unclosed event loop', stderr)

def test__run_once_schedule_handle(self):
handle = None
processed = False
Expand Down
0