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

test_thread should clean threads after each test #74542

Closed
vstinner opened this issue May 12, 2017 · 13 comments
Closed

test_thread should clean threads after each test #74542

vstinner opened this issue May 12, 2017 · 13 comments
Labels
3.7 (EOL) end of life tests Tests in the Lib/test dir

Comments

@vstinner
Copy link
Member

BPO 30357
Nosy @vstinner, @serhiy-storchaka, @grzgrzgrz3
PRs
  • bpo-30357 each test in test_thread waits until all spawned threads finish #1583
  • bpo-30357: test_thread now uses threading_cleanup() #1592
  • [3.6] bpo-30357: test_thread now uses threading_cleanup() (#1592) #1622
  • [3.5] bpo-30357: test_thread now uses threading_cleanup() (#1592) #1623
  • bpo-30387: Fix warning in test_threading #1634
  • [3.6] bpo-30387: Fix warning in test_threading (#1634) #1636
  • [3.5] bpo-30387: Fix warning in test_threading (#1634) #1637
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2017-05-17.00:14:25.666>
    created_at = <Date 2017-05-12.22:35:04.470>
    labels = ['3.7', 'tests']
    title = 'test_thread should clean threads after each test'
    updated_at = <Date 2017-05-17.21:49:45.972>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2017-05-17.21:49:45.972>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-05-17.00:14:25.666>
    closer = 'vstinner'
    components = ['Tests']
    creation = <Date 2017-05-12.22:35:04.470>
    creator = 'vstinner'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 30357
    keywords = []
    message_count = 13.0
    messages = ['293586', '293643', '293661', '293678', '293713', '293718', '293724', '293731', '293732', '293825', '293876', '293891', '293893']
    nosy_count = 3.0
    nosy_names = ['vstinner', 'serhiy.storchaka', 'grzgrzgrz3']
    pr_nums = ['1583', '1592', '1622', '1623', '1634', '1636', '1637']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue30357'
    versions = ['Python 2.7', 'Python 3.5', 'Python 3.6', 'Python 3.7']

    @vstinner
    Copy link
    Member Author

    http://buildbot.python.org/all/builders/AMD64%20Debian%20root%202.7/builds/154/steps/test/logs/stdio

    0:04:59 [312/402/1] test_thread failed
    Traceback (most recent call last):
      File "/root/buildarea/2.7.angelico-debian-amd64/build/Lib/test/test_thread.py", line 133, in task
        raise SyntaxError
    SyntaxError: None
    test test_thread failed -- Traceback (most recent call last):
      File "/root/buildarea/2.7.angelico-debian-amd64/build/Lib/test/test_thread.py", line 150, in test_save_exception_state_on_error
        self.assertIn("Traceback", stderr.getvalue())
    AssertionError: 'Traceback' not found in 'Unhandled exception in thread started by <function task at 0x7f06c77ab1b0>\n'

    @vstinner vstinner added the tests Tests in the Lib/test dir label May 12, 2017
    @serhiy-storchaka
    Copy link
    Member

    I just encountered with this failure. It is random, I can reproduce it only when run test_thread repeatedly in parallel with running other tests (maybe needed high load factor).

    $ ./python -m test -uall -F test_thread test_thread test_thread test_thread 
    Run tests sequentially
    0:00:00 [  1] test_thread
    0:00:00 [  2] test_thread
    Traceback (most recent call last):
      File "/home/serhiy/py/cpython2.7/Lib/test/test_thread.py", line 133, in task
        raise SyntaxError
    SyntaxError: None
    test test_thread failed -- Traceback (most recent call last):
      File "/home/serhiy/py/cpython2.7/Lib/test/test_thread.py", line 150, in test_save_exception_state_on_error
        self.assertIn("Traceback", stderr.getvalue())
    AssertionError: 'Traceback' not found in 'Unhandled exception in thread started by <function task at 0xb6a89ee4>\n'

    1 test OK.
    1 test failed:
    test_thread

    Total duration: 432 ms
    Tests result: FAILURE
    [50664 refs]

    @grzgrzgrz3
    Copy link
    Mannequin

    grzgrzgrz3 mannequin commented May 14, 2017

    Problem is with test test_thread.ThreadRunningTests.test_save_exception_state_on_error when other tests leave threads runnig.

    test_save_exception_state_on_error relay on thread._get_count(), if this value decrease test assume thread is finished with is not always correct (other threads finish - started by different test).

    Fix is to make sure each test wait for all threads to finsh.

    @serhiy-storchaka
    Copy link
    Member

    The solution in PR 1583 looks unsafe to me. If some threads hang the loop while self.thread_count != thread._count() will never finished. If some dangling threads created in other tests or by regrtest itself are finished during running the test_thread test, that loop will never finished too.

    @vstinner
    Copy link
    Member Author

    Using 5 terminals to run 5 tests in parallel.

    I'm unable to reproduce the bug if I only run the test alone:

    ./python -m test -uall -F -m test_save_exception_state_on_error test_thread

    But I'm able to reproduce the bug if I run the full test_thread.py:

    ./python -m test -uall -F test_thread

    So I'm now more confident that grzgrzgrz3's patch can fix the issue. test_thread uses the low-level thread.start_new_thread() function to spawn threads, but pthread_join() is not used to wait for the thread exit. So multiple test_thread tests can "leak" threads which can have random effect on following tests.

    @vstinner
    Copy link
    Member Author

    New changeset 79ef7f8 by Victor Stinner in branch 'master':
    bpo-30357: test_thread now uses threading_cleanup() (bpo-1592)
    79ef7f8

    @grzgrzgrz3
    Copy link
    Mannequin

    grzgrzgrz3 mannequin commented May 15, 2017

    I think this do not solve this issue yet. There is still posibillity that different tests/testrunners spawn threads and 'fool' testcase. I think we should not relay on thread._count value where it's possible.

    For master branch thread._set_sentinel() can be used. For 2.7 i don't know simple solution.

    I would like to know your opinion it is worth changing?

    @vstinner
    Copy link
    Member Author

    There is still posibillity that different tests/testrunners spawn threads
    and 'fool' testcase.

    If you run multiple test files in parallel, each test runner process runs a
    single test at the same time: test methods are run sequentially to prevent
    side effects.

    @vstinner
    Copy link
    Member Author

    New changeset 6924ed5 by Victor Stinner (grzgrzgrz3) in branch '2.7':
    bpo-30357 each test in test_thread waits until all spawned threads finish (bpo-1583)
    6924ed5

    @vstinner vstinner changed the title test_thread.test_save_exception_state_on_error(): Unhandled exception in thread: AMD64 Debian root 2.7 test_thread should clean threads after each test May 16, 2017
    @vstinner
    Copy link
    Member Author

    Backported to 3.5 (aeb6447) and 3.6 (6b5b85a).

    I now close the issue, I consider that the change should fix the randomly failing test. If the failure comes back, I will reopen the issue.

    Thank you very much Grzegorz Grzywacz for your first contribution!

    @vstinner vstinner added the 3.7 (EOL) end of life label May 17, 2017
    @vstinner
    Copy link
    Member Author

    New changeset f8d05b3 by Victor Stinner in branch 'master':
    bpo-30387: Fix warning in test_threading (bpo-1634)
    f8d05b3

    @vstinner
    Copy link
    Member Author

    New changeset 44944b6 by Victor Stinner in branch '3.6':
    bpo-30387: Fix warning in test_threading (bpo-1634) (bpo-1636)
    44944b6

    @vstinner
    Copy link
    Member Author

    New changeset f5633e0 by Victor Stinner in branch '3.5':
    bpo-30387: Fix warning in test_threading (bpo-1634) (bpo-1637)
    f5633e0

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life tests Tests in the Lib/test dir
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants