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

Added running_in_another_thread() #8518

Closed

Conversation

radarhere
Copy link
Member

Possible way to help #8492. Alternative to #8501

I've created a Tests/helper.py function called running_in_another_thread() that uses a global variable and inspect to determine if a test has already started running in another thread, so that we can return early if we would only like the test to run in one thread at a time.

This is used here in two cases

  1. Running the tests in multiple threads fails because of thread-unsafe Python code #8492 has found that running a test that calls register_handler at the beginning, and removes the handler at the end, fails when the test is run across multiple threads. My thinking is that if it ok for register_handler to operate across threads, and that it doesn't need to register multiple handlers in parallel. So this PR returns early from the relevant test if it is already running in another thread.
  2. Found concurrency errors in Pillow tonybaloney/pytest-freethreaded#10 finds an error when running
    def test_set_alignment(self) -> None:
    for i in [1, 2, 4, 8, 16, 32]:
    Image.core.set_alignment(i)
    alignment = Image.core.get_alignment()
    assert alignment == i

    across multiple threads. I again think that Image.core.set_alignment doesn't need to be run across multiple threads simultaneously, so this PR returns early.

Comment on lines +176 to +179
if identifier in already_running:
return True

already_running.append(identifier)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that this introduces another possible race, where two or more threads all think that the test is not currently running elsewhere and add themselves to the list.

Copy link
Contributor

@lysnikolaou lysnikolaou left a comment

Choose a reason for hiding this comment

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

If I were to choose between this and #8501, I'd probably go with #8501. This introduces another piece of global state that we probably can avoid.

@radarhere radarhere closed this Nov 16, 2024
@radarhere radarhere deleted the running_in_another_thread branch November 16, 2024 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants