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_concurrent_futures/test_shutdown.py: test_cancel_futures_wait_false flaky #116682

Closed
colesbury opened this issue Mar 12, 2024 · 3 comments
Closed
Assignees
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error

Comments

@colesbury
Copy link
Contributor

colesbury commented Mar 12, 2024

The test looks like:

t = ThreadPoolExecutor()
t.submit(sleep_and_print, .1, "apple")
t.shutdown(wait=False, cancel_futures=True)
""")
# Errors in atexit hooks don't change the process exit code, check
# stderr manually.
self.assertFalse(err)
self.assertEqual(out.strip(), b"apple")

The problem is that if the worker thread starts executing after the shutdown() call, then the task will be canceled (because cancel_futures=True) and nothing will be printed. This happens sometimes with the GIL and more frequently in the free-threaded build with the GIL disabled.

You can reliably trigger the problematic behavior by adding a short sleep at the beginning of the worker thread:

def _worker(executor_reference, work_queue, initializer, initargs):
if initializer is not None:

i.e., add a time.sleep(0.2) before the if statement.

This test has been reported failing previously, but those issues were closed:

Linked PRs

@colesbury colesbury added the tests Tests in the Lib/test dir label Mar 12, 2024
@colesbury colesbury self-assigned this Mar 12, 2024
@colesbury colesbury added type-bug An unexpected behavior, bug, or error 3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes labels Mar 12, 2024
colesbury added a commit to colesbury/cpython that referenced this issue Mar 12, 2024
If the shutdown() call happens before the worker thread starts executing
the task then nothing will be printed to stdout.
colesbury added a commit to colesbury/cpython that referenced this issue Mar 12, 2024
If the shutdown() call happens before the worker thread starts executing
the task then nothing will be printed to stdout.
colesbury added a commit to colesbury/cpython that referenced this issue Mar 12, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 13, 2024
pythonGH-116683)

If the `shutdown()` call happens before the worker thread starts executing
the task, then nothing will be printed to stdout.
(cherry picked from commit 7d1abe9)

Co-authored-by: Sam Gross <colesbury@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 13, 2024
pythonGH-116683)

If the `shutdown()` call happens before the worker thread starts executing
the task, then nothing will be printed to stdout.
(cherry picked from commit 7d1abe9)

Co-authored-by: Sam Gross <colesbury@gmail.com>
tekicat pushed a commit to python-base/cpython that referenced this issue Mar 13, 2024
python#116683)

If the `shutdown()` call happens before the worker thread starts executing
the task, then nothing will be printed to stdout.
colesbury added a commit that referenced this issue Mar 13, 2024
…se (GH-116683) (#116693)

If the `shutdown()` call happens before the worker thread starts executing
the task, then nothing will be printed to stdout.
(cherry picked from commit 7d1abe9)

Co-authored-by: Sam Gross <colesbury@gmail.com>
colesbury added a commit that referenced this issue Mar 13, 2024
…se (GH-116683) (#116692)

If the `shutdown()` call happens before the worker thread starts executing
the task, then nothing will be printed to stdout.
(cherry picked from commit 7d1abe9)

Co-authored-by: Sam Gross <colesbury@gmail.com>
@sobolevn
Copy link
Member

This might be related? https://github.com/python/cpython/actions/runs/8261299493/job/22598342550

 ======================================================================
FAIL: test_check_output_timeout (test.test_subprocess.ProcessTestCaseNoPoll.test_check_output_timeout)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/runner/work/cpython/cpython/Lib/test/test_subprocess.py", line 286, in test_check_output_timeout
    self.assertEqual(c.exception.output, b'BDFL')
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: None != b'BDFL'

======================================================================
FAIL: test_check_output_timeout (test.test_subprocess.RunFuncTestCase.test_check_output_timeout)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/runner/work/cpython/cpython/Lib/test/test_subprocess.py", line 1694, in test_check_output_timeout
    self.assertEqual(c.exception.output, b'BDFL')
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: None != b'BDFL'

----------------------------------------------------------------------

@colesbury
Copy link
Contributor Author

@sobolevn I think the underlying cause is different:

  • test_check_output_timeout failure is due to starting a subprocess taking too long (>3 seconds) on a heavily loaded machine
  • test_cancel_futures_wait_false failure was due to a race condition involving threads

They are similar in that sometimes the output is empty and the tests results are inconsistent.

@colesbury
Copy link
Contributor Author

Let's open a separate issue for test_check_output_timeout

vstinner pushed a commit to vstinner/cpython that referenced this issue Mar 20, 2024
python#116683)

If the `shutdown()` call happens before the worker thread starts executing
the task, then nothing will be printed to stdout.
adorilson pushed a commit to adorilson/cpython that referenced this issue Mar 25, 2024
python#116683)

If the `shutdown()` call happens before the worker thread starts executing
the task, then nothing will be printed to stdout.
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
python#116683)

If the `shutdown()` call happens before the worker thread starts executing
the task, then nothing will be printed to stdout.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants