Skip to content

A lightweight cooperative timeout mechanism for synchronous code with timeout(seconds): #151989

Description

@zang-langyan

Feature or enhancement

Proposal:

As a follow up on https://discuss.python.org/t/a-lightweight-cooperative-timeout-mechanism-for-synchronous-code-with-timeout-seconds/107811

>>> with timeout(0.2):
...     while True:
...         pass
...
Traceback (most recent call last):
  File "<python-input-1>", line 3, in <module>
    pass
TimeoutError: Timeout

>>> with timeout(0.2):
...     re.match(r"(a+)+b", "a" * 30 + "c")
...
Traceback (most recent call last):
  File "<python-input-2>", line 2, in <module>
    re.match(r"(a+)+b", "a" * 30 + "c")
    ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../cpython/Lib/re/__init__.py", line 166, in prefixmatch
    return _compile(pattern, flags).prefixmatch(string)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
TimeoutError: Timeout

Key Proporties:
Tested on my Macbook Pro M4

  1. No extra overhead outside a timeout block (i.e. timeout_block == NULL timeout check is completely bypassed).
  2. Py_CheckTimeOut
  • fast path (with skip interval ≥ 8) takes 1.1~1.3ns per call;
  • slow path (with skip interval < 8) takes 2.0~6.3ns per call;
  • full calling (without skip interval) takes 10.2ns +- 1.2ns per call.
  1. re module integration
    By tapping into the existing signal‑check counter inside _sre, timeout checks add no measurable overhead to normal regexps while allowing catastrophic backtracking to be safely interrupted within the specified deadline.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/a-lightweight-cooperative-timeout-mechanism-for-synchronous-code-with-timeout-seconds/107811

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedtype-featureA feature request or enhancement
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions