Skip to content
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

_DummyThreads can be joined in -OO mode #106236

Closed
sobolevn opened this issue Jun 29, 2023 · 1 comment
Closed

_DummyThreads can be joined in -OO mode #106236

sobolevn opened this issue Jun 29, 2023 · 1 comment
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

sobolevn commented Jun 29, 2023

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

@sobolevn sobolevn added type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir labels Jun 29, 2023
@sobolevn sobolevn self-assigned this Jun 29, 2023
sobolevn added a commit to sobolevn/cpython that referenced this issue Jun 29, 2023
gpshead pushed a commit that referenced this issue Jul 12, 2023
…y` (#106237)

Replace `assert` with `raise ` in `threading.py` so that -OO does not alter _DummyThread behavior.
@gpshead
Copy link
Member

gpshead commented Jul 12, 2023

fixed in main, thanks for the PR. I don't personally think this is important enough to put in a bugfix release, if someone finds a practical reason to do so, feel free to hit the backport buttons.

@gpshead gpshead closed this as completed Jul 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants