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 all commits
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
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash upon finalization when passing a non-boolean value to
:meth:`loop.set_debug <asyncio.loop.set_debug>`. Patch by Bénédikt Tran.
Loading