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

concurrent.futures.Executor.map does not cancel the currently waited on future #95166

Closed
graingert opened this issue Jul 23, 2022 · 1 comment
Labels
type-bug An unexpected behavior, bug, or error

Comments

@graingert
Copy link
Contributor

graingert commented Jul 23, 2022

Bug report

import contextlib
import functools
import concurrent.futures
import threading
import sys


def fn(num, stop_event):
    if num == 1:
        stop_event.wait()
        return "done 1"

    if num == 2:
        return "done 2"


def main():
    stop_event = threading.Event()
    log = []

    with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:

        def print_n_wait(ident):
            log.append(f"{ident=} started")
            try:
                stop_event.wait()
            finally:
                log.append(f"{ident=} stopped")

        fut = pool.submit(print_n_wait, ident="first")
        try:
            with contextlib.closing(pool.map(print_n_wait, ["second", "third"], timeout=1)) as gen:
                try:
                    next(gen)
                except concurrent.futures.TimeoutError:
                    print("timed out")
                else:
                    raise RuntimeError("timeout expected")
        finally:
            stop_event.set()

    assert log == ["ident='first' started", "ident='first' stopped"], f"{log=} is wrong"

if __name__ == "__main__":
    sys.exit(main())

result in:

timed out
Traceback (most recent call last):
  File "/home/graingert/projects/executor_map.py", line 45, in <module>
    sys.exit(main())
  File "/home/graingert/projects/executor_map.py", line 42, in main
    assert log == ["ident='first' started", "ident='first' stopped"], f"{log=} is wrong"
AssertionError: log=["ident='first' started", "ident='first' stopped", "ident='second' started", "ident='second' stopped"] is wrong

Your environment

  • CPython versions tested on:
  • Operating system and architecture:
@graingert graingert added the type-bug An unexpected behavior, bug, or error label Jul 23, 2022
graingert added a commit to graingert/cpython that referenced this issue Jul 23, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jul 28, 2022
Co-authored-by: Kumar Aditya <[email protected]>
(cherry picked from commit e16d4ed)

Co-authored-by: Thomas Grainger <[email protected]>
ambv pushed a commit that referenced this issue Jul 28, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jul 28, 2022
Co-authored-by: Kumar Aditya <[email protected]>
(cherry picked from commit e16d4ed)

Co-authored-by: Thomas Grainger <[email protected]>
ambv pushed a commit that referenced this issue Jul 28, 2022
Co-authored-by: Kumar Aditya <[email protected]>
(cherry picked from commit e16d4ed)

Co-authored-by: Thomas Grainger <[email protected]>
ambv pushed a commit to ambv/cpython that referenced this issue Jul 28, 2022
ambv added a commit that referenced this issue Jul 29, 2022
@ambv
Copy link
Contributor

ambv commented Jul 29, 2022

Fixed in 3.10 - 3.12. Thanks, Thomas! ✨ 🍰 ✨

@ambv ambv closed this as completed Jul 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants