Skip to content

Commit 4d0d1c3

Browse files
authored
gh-110014: Remove PY_TIMEOUT_MAX from limited C API (#110217)
If the timeout is greater than PY_TIMEOUT_MAX, PyThread_acquire_lock_timed() uses a timeout of PY_TIMEOUT_MAX microseconds, which is around 280.6 years. This case is unlikely and limiting a timeout to 280.6 years sounds like a reasonable trade-off. The constant PY_TIMEOUT_MAX is not used in PyPI top 5,000 projects.
1 parent 732ad44 commit 4d0d1c3

File tree

8 files changed

+17
-20
lines changed

8 files changed

+17
-20
lines changed

Doc/data/stable_abi.dat

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Doc/whatsnew/3.13.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,3 +1290,6 @@ removed, although there is currently no date scheduled for their removal.
12901290
* :c:func:`PyThread_get_key_value`: use :c:func:`PyThread_tss_get`.
12911291
* :c:func:`PyThread_delete_key_value`: use :c:func:`PyThread_tss_delete`.
12921292
* :c:func:`PyThread_ReInitTLS`: no longer needed.
1293+
1294+
* Remove undocumented ``PY_TIMEOUT_MAX`` constant from the limited C API.
1295+
(Contributed by Victor Stinner in :gh:`110014`.)

Include/cpython/pythread.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
# error "this header file must not be included directly"
33
#endif
44

5+
// PY_TIMEOUT_MAX is the highest usable value (in microseconds) of PY_TIMEOUT_T
6+
// type, and depends on the system threading API.
7+
//
8+
// NOTE: this isn't the same value as `_thread.TIMEOUT_MAX`. The _thread module
9+
// exposes a higher-level API, with timeouts expressed in seconds and
10+
// floating-point numbers allowed.
11+
PyAPI_DATA(const long long) PY_TIMEOUT_MAX;
12+
513
#define PYTHREAD_INVALID_THREAD_ID ((unsigned long)-1)
614

715
#ifdef HAVE_PTHREAD_H

Include/pythread.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,18 @@ PyAPI_FUNC(int) PyThread_acquire_lock(PyThread_type_lock, int);
3333
#define WAIT_LOCK 1
3434
#define NOWAIT_LOCK 0
3535

36-
/* PY_TIMEOUT_T is the integral type used to specify timeouts when waiting
37-
on a lock (see PyThread_acquire_lock_timed() below).
38-
PY_TIMEOUT_MAX is the highest usable value (in microseconds) of that
39-
type, and depends on the system threading API.
40-
41-
NOTE: this isn't the same value as `_thread.TIMEOUT_MAX`. The _thread
42-
module exposes a higher-level API, with timeouts expressed in seconds
43-
and floating-point numbers allowed.
44-
*/
36+
// PY_TIMEOUT_T is the integral type used to specify timeouts when waiting
37+
// on a lock (see PyThread_acquire_lock_timed() below).
4538
#define PY_TIMEOUT_T long long
4639

47-
PyAPI_DATA(const long long) PY_TIMEOUT_MAX;
48-
4940

5041
/* If microseconds == 0, the call is non-blocking: it returns immediately
5142
even when the lock can't be acquired.
5243
If microseconds > 0, the call waits up to the specified duration.
5344
If microseconds < 0, the call waits until success (or abnormal failure)
5445
55-
microseconds must be less than PY_TIMEOUT_MAX. Behaviour otherwise is
56-
undefined.
46+
If *microseconds* is greater than PY_TIMEOUT_MAX, clamp the timeout to
47+
PY_TIMEOUT_MAX microseconds.
5748
5849
If intr_flag is true and the acquire is interrupted by a signal, then the
5950
call will return PY_LOCK_INTR. The caller may reattempt to acquire the

Lib/test/test_stable_abi_ctypes.py

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Remove undocumented ``PY_TIMEOUT_MAX`` constant from the limited C API.
2+
Patch by Victor Stinner.

Misc/stable_abi.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,10 +1843,6 @@
18431843
[function.PyThread_start_new_thread]
18441844
added = '3.2'
18451845

1846-
# Not mentioned in PEP 384, was implemented as a macro in Python <= 3.12
1847-
[data.PY_TIMEOUT_MAX]
1848-
added = '3.2'
1849-
18501846
# The following were added in PC/python3.def in Python 3.3:
18511847
# 7800f75827b1be557be16f3b18f5170fbf9fae08
18521848
# 9c56409d3353b8cd4cfc19e0467bbe23fd34fc92

PC/python3dll.c

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)