Skip to content

Asyncio exception handling breaks convention by logging callback parameter values #112997

Closed
@CendioZeijlon

Description

@CendioZeijlon

Bug report

Bug description:

My understanding is that Python usually does not attempt to log or show parameter and variable values when errors occur. It seems uncharacteristic that asyncio does so, e.g. when it handles uncaught exceptions.

This type of logging can also occur when asyncio reports on tasks with too slow execution times, but then only in debug mode.

When not in debug mode, parameter values are logged for uncaught exceptions, but not for slow execution times. I don't see why these two cases behave differently. If you think that this logging is necessary, perhaps it can be limited to debug mode only?

Input:

import asyncio
import functools

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

def test_callback(a, b, c, *, x, y, z):
    raise RuntimeError('Some uncaught error')

loop.call_soon(
    functools.partial(
        test_callback,
        {'key': 'value'},
        ['value'],
        ('value',),
        x = {'key': 'value'},
        y = ['value'],
        z = ('value',)
    )
)
loop.call_soon(loop.stop)

res = ''
try:
    loop.run_forever()
finally:
    loop.close()

Output:

Exception in callback test_callback({'key': 'value'}, ['value'], ('value',), x={'key': 'value'}, y=['value'], z=('value',))() at /home/aleze/Documents/scenario.py:7
handle: <Handle test_callback({'key': 'value'}, ['value'], ('value',), x={'key': 'value'}, y=['value'], z=('value',))() at /home/aleze/Documents/scenario.py:7>
Traceback (most recent call last):
  File "/usr/lib64/python3.11/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/home/aleze/Documents/scenario.py", line 8, in test_callback
    raise RuntimeError('Some uncaught error')
RuntimeError: Some uncaught error

Extra:

The function that actually creates the output is format_helpers._format_args_and_kwargs().

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions