Skip to content

_DummyThreads can be joined in -OO mode #106236

Closed
@sobolevn

Description

@sobolevn

Right now _DummyThread claims to not allow join:

cpython/Lib/threading.py

Lines 1453 to 1458 in fb0d9b9

def is_alive(self):
assert not self._is_stopped and self._started.is_set()
return True
def join(self, timeout=None):
assert False, "cannot join a dummy thread"

But, it can be easily changed with python -OO mode, which strips assert statements.

The easiest way to check this is:

# ex.py
import threading, _thread

def f(mutex):
    threading.current_thread()
    mutex.release()

mutex = threading.Lock()
mutex.acquire()

tid = _thread.start_new_thread(f, (mutex,))
mutex.acquire()
threading._active[tid].join()
print('done')

python ex.py results in:

Traceback (most recent call last):
  File "/Users/sobolev/Desktop/cpython/ex.py", line 12, in <module>
    threading._active[tid].join()
  File "/Users/sobolev/Desktop/cpython/Lib/threading.py", line 1458, in join
    assert False, "cannot join a dummy thread"
AssertionError: cannot join a dummy thread

But, python -OO ex.py results in:

done

This looks like an important behavior change. I propose to use explicit AssertionError / RuntimeError instead.

I have a PR ready.

Linked PRs

Metadata

Metadata

Assignees

Labels

stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions