Skip to content

Add tests for asyncio _FlowControlMixin exception handling in pause_writing #150361

@azibom

Description

@azibom

Add tests for exception handling in _FlowControlMixin._maybe_pause_protocol

The _FlowControlMixin._maybe_pause_protocol() method contains a defensive
exception handling path when calling protocol.pause_writing():

try:
    self._protocol.pause_writing()
except (SystemExit, KeyboardInterrupt):
    raise
except BaseException as exc:
    self._loop.call_exception_handler({
        'message': 'protocol.pause_writing() failed',
        'exception': exc,
        'transport': self,
        'protocol': self._protocol,
    })

Current situation

While normal flow-control behavior is covered by existing tests, the exception
handling branch (when pause_writing() raises an error) is not explicitly
tested.

Proposed test coverage

This issue proposes adding a unit test to ensure that:

  • protocol.pause_writing() is called when write buffer exceeds high-water mark

  • If pause_writing() raises an exception:

    • loop.call_exception_handler() is invoked
    • The exception object is correctly passed to the handler
    • The error message contains "protocol.pause_writing() failed"
    • The transport remains in _protocol_paused = True state

Why this matters

This behavior is part of asyncio’s flow control robustness and ensures that
protocol-level errors do not silently break transport state transitions.
Explicit testing prevents regressions in error-handling logic inside
_FlowControlMixin.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytestsTests in the Lib/test dirtopic-asyncio
    No fields configured for issues without a type.

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions