Skip to content

Logging enabled caching can cause TypeError in unit tests #11778

@meehand

Description

@meehand

Describe the bug

In web_protocol.py, log_access checks for the following:
if self.access_logger is not None and self.access_logger.enabled
which guards against this code
self._loop.time() - time.
It is possible to get here in unit tests with time == None, causing a TypeError

self.access_logger.enabled is a property that can change if the logging level changes, but time is set based on
start = loop.time() if self._logging_enabled else None

And self._logging_enabled is set on init only

Usually this is fine since log levels will be set way before the RequestHandler init, but in unit tests the log level can instead be set with a call to self.assertLogs(), which could be called far after the RequestHandler init

To Reproduce

  1. Create a generic server with a single endpoint
  2. Separately, create a test against that server using AioHTTPTestCase, but do not set up any logging. Call the endpoint in that case within a with self.assertLogs() block
  3. See this line assert on time being None
    self.access_logger.log(request, response, self._loop.time() - time)

Expected behavior

RequestHandler correctly handles log levels changing post-init

Logs/tracebacks

.

Python Version

$ python --version
Python 3.11.3

aiohttp Version

$ python -m pip show aiohttp
3.13.2

multidict Version

$ python -m pip show multidict
6.7.0

propcache Version

$ python -m pip show propcache
0.4.1

yarl Version

$ python -m pip show yarl
1.22.0

OS

Ubuntu 20.04

Related component

Server

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions