Skip to content

gh-88750: Remove the PYTHONTHREADDEBUG env var support. #92509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions Doc/using/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -999,15 +999,6 @@ conflict.
Debug-mode variables
~~~~~~~~~~~~~~~~~~~~

.. envvar:: PYTHONTHREADDEBUG

If set, Python will print threading debug info into stdout.

Need a :ref:`debug build of Python <debug-build>`.

.. deprecated-removed:: 3.10 3.12


.. envvar:: PYTHONDUMPREFS

If set, Python will dump objects and reference counts still alive after
Expand Down
1 change: 0 additions & 1 deletion Doc/using/configure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ Effects of a debug build:
* Add ``d`` to :data:`sys.abiflags`.
* Add :func:`sys.gettotalrefcount` function.
* Add :option:`-X showrefcount <-X>` command line option.
* Add :envvar:`PYTHONTHREADDEBUG` environment variable.
* Add support for the ``__lltrace__`` variable: enable low-level tracing in the
bytecode evaluation loop if the variable is defined.
* Install :ref:`debug hooks on memory allocators <default-memory-allocators>`
Expand Down
10 changes: 0 additions & 10 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,16 +945,6 @@ def noop(): pass
threading.Thread(target=noop).start()
# Thread.join() is not called

@unittest.skipUnless(Py_DEBUG, 'need debug build (Py_DEBUG)')
def test_debug_deprecation(self):
# bpo-44584: The PYTHONTHREADDEBUG environment variable is deprecated
rc, out, err = assert_python_ok("-Wdefault", "-c", "pass",
PYTHONTHREADDEBUG="1")
msg = (b'DeprecationWarning: The threading debug '
b'(PYTHONTHREADDEBUG environment variable) '
b'is deprecated and will be removed in Python 3.12')
self.assertIn(msg, err)

def test_import_from_another_thread(self):
# bpo-1596321: If the threading module is first import from a thread
# different than the main thread, threading._shutdown() must handle
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The deprecated debug build only ``PYTHONTHREADDEBUG`` environment variable
no longer does anything.
3 changes: 0 additions & 3 deletions Misc/python.man
Original file line number Diff line number Diff line change
Expand Up @@ -560,9 +560,6 @@ can be set to the callable of your debugger of choice.
Setting these variables only has an effect in a debug build of Python, that is,
if Python was configured with the
\fB\--with-pydebug\fP build option.
.IP PYTHONTHREADDEBUG
If this environment variable is set, Python will print threading debug info.
The feature is deprecated in Python 3.10 and will be removed in Python 3.12.
.IP PYTHONDUMPREFS
If this environment variable is set, Python will dump objects and reference
counts still alive after shutting down the interpreter.
Expand Down
5 changes: 0 additions & 5 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1091,8 +1091,6 @@ pyinit_main_reconfigure(PyThreadState *tstate)
static PyStatus
init_interp_main(PyThreadState *tstate)
{
extern void _PyThread_debug_deprecation(void);

assert(!_PyErr_Occurred(tstate));

PyStatus status;
Expand Down Expand Up @@ -1194,9 +1192,6 @@ init_interp_main(PyThreadState *tstate)
#endif
}

// Warn about PYTHONTHREADDEBUG deprecation
_PyThread_debug_deprecation();

assert(!_PyErr_Occurred(tstate));

return _PyStatus_OK();
Expand Down
38 changes: 0 additions & 38 deletions Python/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,57 +42,19 @@

#endif /* _POSIX_THREADS */


#ifdef Py_DEBUG
static int thread_debug = 0;
# define dprintf(args) (void)((thread_debug & 1) && printf args)
#else
# define dprintf(args)
#endif

static int initialized;

static void PyThread__init_thread(void); /* Forward */

void
PyThread_init_thread(void)
{
#ifdef Py_DEBUG
const char *p = Py_GETENV("PYTHONTHREADDEBUG");

if (p) {
if (*p)
thread_debug = atoi(p);
else
thread_debug = 1;
}
#endif /* Py_DEBUG */
if (initialized)
return;
initialized = 1;
dprintf(("PyThread_init_thread called\n"));
PyThread__init_thread();
}

void
_PyThread_debug_deprecation(void)
{
#ifdef Py_DEBUG
if (thread_debug) {
// Flush previous dprintf() logs
fflush(stdout);
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"The threading debug (PYTHONTHREADDEBUG environment "
"variable) is deprecated and will be removed "
"in Python 3.12",
0))
{
_PyErr_WriteUnraisableMsg("at Python startup", NULL);
}
}
#endif
}

#if defined(_POSIX_THREADS)
# define PYTHREAD_NAME "pthread"
# include "thread_pthread.h"
Expand Down
25 changes: 3 additions & 22 deletions Python/thread_nt.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,6 @@ PyThread_start_new_thread(void (*func)(void *), void *arg)
unsigned threadID;
callobj *obj;

dprintf(("%lu: PyThread_start_new_thread called\n",
PyThread_get_thread_ident()));
if (!initialized)
PyThread_init_thread();

Expand All @@ -209,14 +207,10 @@ PyThread_start_new_thread(void (*func)(void *), void *arg)
* too many threads".
*/
int e = errno;
dprintf(("%lu: PyThread_start_new_thread failed, errno %d\n",
PyThread_get_thread_ident(), e));
threadID = (unsigned)-1;
HeapFree(GetProcessHeap(), 0, obj);
}
else {
dprintf(("%lu: PyThread_start_new_thread succeeded: %p\n",
PyThread_get_thread_ident(), (void*)hThread));
CloseHandle(hThread);
}
return threadID;
Expand Down Expand Up @@ -257,7 +251,6 @@ PyThread_get_thread_native_id(void)
void _Py_NO_RETURN
PyThread_exit_thread(void)
{
dprintf(("%lu: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
if (!initialized)
exit(0);
_endthreadex(0);
Expand All @@ -273,22 +266,17 @@ PyThread_allocate_lock(void)
{
PNRMUTEX aLock;

dprintf(("PyThread_allocate_lock called\n"));
if (!initialized)
PyThread_init_thread();

aLock = AllocNonRecursiveMutex() ;

dprintf(("%lu: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock));

return (PyThread_type_lock) aLock;
}

void
PyThread_free_lock(PyThread_type_lock aLock)
{
dprintf(("%lu: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock));

FreeNonRecursiveMutex(aLock) ;
}

Expand Down Expand Up @@ -333,9 +321,6 @@ PyThread_acquire_lock_timed(PyThread_type_lock aLock,
milliseconds = INFINITE;
}

dprintf(("%lu: PyThread_acquire_lock_timed(%p, %lld) called\n",
PyThread_get_thread_ident(), aLock, microseconds));

if (aLock && EnterNonRecursiveMutex((PNRMUTEX)aLock,
(DWORD)milliseconds) == WAIT_OBJECT_0) {
success = PY_LOCK_ACQUIRED;
Expand All @@ -344,9 +329,6 @@ PyThread_acquire_lock_timed(PyThread_type_lock aLock,
success = PY_LOCK_FAILURE;
}

dprintf(("%lu: PyThread_acquire_lock(%p, %lld) -> %d\n",
PyThread_get_thread_ident(), aLock, microseconds, success));

return success;
}
int
Expand All @@ -358,10 +340,9 @@ PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
void
PyThread_release_lock(PyThread_type_lock aLock)
{
dprintf(("%lu: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock));

if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock)))
dprintf(("%lu: Could not PyThread_release_lock(%p) error: %ld\n", PyThread_get_thread_ident(), aLock, GetLastError()));
if (aLock) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, we have a special case to do nothing if the lock is not initialized? Only for the Windows implementation and only for PyThread_release_lock()? That's weird. I suggest to replace the if() with an assert(), but done in a separated PR. (I can do it if I don't forget.)

It's weird because the pthread implement has no special case for PyThread_release_lock() and so the code is not portable. Or maybe I misunderstood something.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be surprised to ever see this called with aLock == NULL, I just figured I should preserve the exact semantics when removing the debug print. Agreed on the assert, but that does make more sense in a followup PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I created #92586

(void)LeaveNonRecursiveMutex((PNRMUTEX) aLock);
}
}

/* minimum/maximum thread stack sizes supported */
Expand Down
19 changes: 0 additions & 19 deletions Python/thread_pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ PyThread_start_new_thread(void (*func)(void *), void *arg)
size_t tss;
#endif

dprintf(("PyThread_start_new_thread called\n"));
if (!initialized)
PyThread_init_thread();

Expand Down Expand Up @@ -358,7 +357,6 @@ PyThread_get_thread_native_id(void)
void _Py_NO_RETURN
PyThread_exit_thread(void)
{
dprintf(("PyThread_exit_thread called\n"));
if (!initialized)
exit(0);
pthread_exit(0);
Expand All @@ -376,7 +374,6 @@ PyThread_allocate_lock(void)
sem_t *lock;
int status, error = 0;

dprintf(("PyThread_allocate_lock called\n"));
if (!initialized)
PyThread_init_thread();

Expand All @@ -392,7 +389,6 @@ PyThread_allocate_lock(void)
}
}

dprintf(("PyThread_allocate_lock() -> %p\n", (void *)lock));
return (PyThread_type_lock)lock;
}

Expand All @@ -403,7 +399,6 @@ PyThread_free_lock(PyThread_type_lock lock)
int status, error = 0;

(void) error; /* silence unused-but-set-variable warning */
dprintf(("PyThread_free_lock(%p) called\n", lock));

if (!thelock)
return;
Expand Down Expand Up @@ -435,8 +430,6 @@ PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds,
int status, error = 0;

(void) error; /* silence unused-but-set-variable warning */
dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) called\n",
lock, microseconds, intr_flag));

_PyTime_t timeout; // relative timeout
if (microseconds >= 0) {
Expand Down Expand Up @@ -544,8 +537,6 @@ PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds,
success = PY_LOCK_FAILURE;
}

dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) -> %d\n",
lock, microseconds, intr_flag, success));
return success;
}

Expand All @@ -556,7 +547,6 @@ PyThread_release_lock(PyThread_type_lock lock)
int status, error = 0;

(void) error; /* silence unused-but-set-variable warning */
dprintf(("PyThread_release_lock(%p) called\n", lock));

status = sem_post(thelock);
CHECK_STATUS("sem_post");
Expand All @@ -573,7 +563,6 @@ PyThread_allocate_lock(void)
pthread_lock *lock;
int status, error = 0;

dprintf(("PyThread_allocate_lock called\n"));
if (!initialized)
PyThread_init_thread();

Expand All @@ -599,7 +588,6 @@ PyThread_allocate_lock(void)
}
}

dprintf(("PyThread_allocate_lock() -> %p\n", (void *)lock));
return (PyThread_type_lock) lock;
}

Expand All @@ -610,7 +598,6 @@ PyThread_free_lock(PyThread_type_lock lock)
int status, error = 0;

(void) error; /* silence unused-but-set-variable warning */
dprintf(("PyThread_free_lock(%p) called\n", lock));

/* some pthread-like implementations tie the mutex to the cond
* and must have the cond destroyed first.
Expand All @@ -632,9 +619,6 @@ PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds,
pthread_lock *thelock = (pthread_lock *)lock;
int status, error = 0;

dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) called\n",
lock, microseconds, intr_flag));

if (microseconds == 0) {
status = pthread_mutex_trylock( &thelock->mut );
if (status != EBUSY)
Expand Down Expand Up @@ -694,8 +678,6 @@ PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds,
}

if (error) success = PY_LOCK_FAILURE;
dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) -> %d\n",
lock, microseconds, intr_flag, success));
return success;
}

Expand All @@ -706,7 +688,6 @@ PyThread_release_lock(PyThread_type_lock lock)
int status, error = 0;

(void) error; /* silence unused-but-set-variable warning */
dprintf(("PyThread_release_lock(%p) called\n", lock));

status = pthread_mutex_lock( &thelock->mut );
CHECK_STATUS_PTHREAD("pthread_mutex_lock[3]");
Expand Down