Skip to content

gh-106236: Replace assert with raise RuntimeError in threading.py #106237

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 4 commits into from
Jul 12, 2023

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Jun 29, 2023

I went with AssertionError so we maintain 100% backward compatibility.

Copy link
Member

@terryjreedy terryjreedy left a comment

Choose a reason for hiding this comment

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

Assert statements should only be used to catch internal errors, for debugging, not user errors. So I consider the use of assert and raising of AssertionError in _DummyThread to be an error from the beginning. To fully fix this, another error should be raised instead. (Yes, get other opinions.) Which can be decided once agreed on not AssertionError.

.is_alive may be a different case. It checks the value of private variables, and unless the user can change those private variables through public interfaces, (which theorhetically should not be possible), rather than directly, the assert there would seem proper as is. IE, an AssertionError would indicate a CPython bug rather than a user bug.

The base problem here is that _DummyThread is a Thread- rather than a Thread+.

Lib/threading.py Outdated

def join(self, timeout=None):
assert False, "cannot join a dummy thread"
raise AssertionError("cannot join a dummy thread")
Copy link
Member

Choose a reason for hiding this comment

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

I think another error should be raised. See review comment.

Lib/threading.py Outdated
return True
if not self._is_stopped and self._started.is_set():
return True
raise AssertionError("thread is not alive")
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure that any change is needed here. If so, I think another error. See review comment.

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 think it is needed. _DummyThread is assumed to always be alive, but as I showed in the test case, it is easy to kill it (sort of). So, adding an error about reaching an unwanted state seems like a good thing to do.

I agree that AssertionError is probably not the best way to raise an error, will change it to RuntimeError.

Copy link
Member Author

Choose a reason for hiding this comment

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

Again, the counter example would be:

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]._started.clear()
threading._active[tid].is_alive()

With asserts on main it will raise AssertionError
With -OO on main it will return True

I think that this is not fine.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be put in the comfy chair!

@sobolevn
Copy link
Member Author

@terryjreedy thanks a lot for the quick review! Addressed all your comments :)

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@terryjreedy: please review the changes made to this pull request.

@bedevere-bot bedevere-bot requested a review from terryjreedy June 30, 2023 08:37
@AlexWaygood AlexWaygood changed the title gh-106236: Replace assert with raise AssertionError in threading.py gh-106236: Replace assert with raise RuntimeError in threading.py Jun 30, 2023
@gpshead gpshead merged commit e4b88c1 into python:main Jul 12, 2023
@terryjreedy
Copy link
Member

@gpshead The issue was marked as a bug because using assert caused code to misbehave. If you agree, this should be backported.

@gpshead gpshead self-assigned this Jul 12, 2023
@gpshead
Copy link
Member

gpshead commented Jul 12, 2023

If we were still in the beta period i'd have just hit the backport button for 3.12. But the next release is 3.12rc1 and I'd rather reserve changes I merge there to important things. This one doesn't seem like an important enough bug fix to bother on (it has existed forever and I'm not aware of anyone actually noticing it in an actual application).

If someone sees a need to backport it, no objection from me, I'm not going to bother myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants