Skip to content

gh-133485: Use interpreters.Interpreter in InterpreterPoolExecutor #133957

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

Conversation

ericsnowcurrently
Copy link
Member

@ericsnowcurrently ericsnowcurrently commented May 13, 2025

Most importantly, this resolves the issues with functions and types defined in __main__.
It also expands the number of supported objects.

@ericsnowcurrently ericsnowcurrently added needs backport to 3.14 bugs and security fixes and removed awaiting core review labels May 13, 2025
@ericsnowcurrently ericsnowcurrently force-pushed the interp-pool-executor-use-interp-call branch from 5340a57 to 62d7c2c Compare May 13, 2025 01:13
@ericsnowcurrently ericsnowcurrently marked this pull request as draft May 13, 2025 01:14
@ericsnowcurrently ericsnowcurrently force-pushed the interp-pool-executor-use-interp-call branch 9 times, most recently from b3c2477 to 7697c11 Compare May 27, 2025 16:31
Comment on lines +23 to +26
# InterpreterPoolInitializerTest.test_initializer fails
# if we don't have a LOAD_GLOBAL. (It could be any global.)
# We will address this separately.
INITIALIZER_STATUS
Copy link
Member Author

Choose a reason for hiding this comment

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

@markshannon, any ideas on why this is happening? It smells like a ceval bug, but it certainly could be something I've done wrong.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

@neonene neonene Jun 1, 2025

Choose a reason for hiding this comment

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

There seem to be related changes in inspect.getclosurevars() since 83ba8c2:

before:
ClosureVars(nonlocals={},
            globals={'INITIALIZER_STATUS': 'uninitialized'},
            builtins={}, unbound=set())
after:
ClosureVars(nonlocals={},
            globals={},
            builtins={}, unbound=set())
  • init() on main (without L26):
  3           RESUME                   0

  5           LOAD_FAST_BORROW         0 (x)
              STORE_GLOBAL             0 (INITIALIZER_STATUS)
              LOAD_CONST               0 (None)
              RETURN_VALUE
  • 3.3.5 (2014):
  5           0 LOAD_FAST                0 (x)
              3 STORE_GLOBAL             0 (INITIALIZER_STATUS)
              6 LOAD_CONST               0 (None)
              9 RETURN_VALUE

@ericsnowcurrently ericsnowcurrently force-pushed the interp-pool-executor-use-interp-call branch from 5ea2bb2 to 14a8eb9 Compare May 29, 2025 21:02
@ericsnowcurrently ericsnowcurrently force-pushed the interp-pool-executor-use-interp-call branch from 14a8eb9 to ccc135c Compare May 30, 2025 15:27
@ericsnowcurrently ericsnowcurrently marked this pull request as ready for review May 30, 2025 21:32
@ericsnowcurrently ericsnowcurrently added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label May 30, 2025
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @ericsnowcurrently for commit ccc135c 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F133957%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label May 30, 2025
@neonene
Copy link
Contributor

neonene commented May 31, 2025

The wasm32-wasi Non-Debug buildbot seems to be unused on merged commits and out of order on PRs. For example: https://buildbot.python.org/#/builders/1373/builds/507

@neonene
Copy link
Contributor

neonene commented Jun 1, 2025

Is the following usage invalid?

INITIALIZER_STATUS = 'uninitialized'

def init(x):
    global INITIALIZER_STATUS
    INITIALIZER_STATUS = x
    INITIALIZER_STATUS  # for now

def get_init_status():
    return INITIALIZER_STATUS

if __name__ == "__main__":
    from concurrent.futures import InterpreterPoolExecutor
    exe = InterpreterPoolExecutor(initializer=init, initargs=('initialized',))
    fut = exe.submit(get_init_status)
    assert fut.result() == 'initialized'  # fails
    exe.shutdown(wait=True)
    assert INITIALIZER_STATUS == 'uninitialized'

@neonene
Copy link
Contributor

neonene commented Jun 3, 2025

res.loaded = runpy_run_path(filename, run_modname);

I guess the failure case in my previous comment can be resolved if the runpy_run_path is called only once for __main__ by using/keeping ctx->main.cached->loaded or something across xi-sessions without being replaced. (My experiment was just putting a static local PyObject* var here ignoring the leak.)

@ericsnowcurrently ericsnowcurrently changed the title gh-133485: Use _interpreters.call() in InterpreterPoolExecutor gh-133485: Use interpreters.Interpreters in InterpreterPoolExecutor Jun 16, 2025
@ericsnowcurrently ericsnowcurrently changed the title gh-133485: Use interpreters.Interpreters in InterpreterPoolExecutor gh-133485: Use interpreters.Interpreter in InterpreterPoolExecutor Jun 16, 2025

numtasks = 10
futures = []
executor = self.executor_type()
Copy link
Contributor

@neonene neonene Jun 17, 2025

Choose a reason for hiding this comment

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

self.executor_type(max_workers=numtasks) or some better way not to get stuck?

@ericsnowcurrently
Copy link
Member Author

FWIW, I haven't been able to reproduce the failures locally. It certainly is consistent across all the CI workers though.

@ericsnowcurrently ericsnowcurrently force-pushed the interp-pool-executor-use-interp-call branch from dafd5a6 to 9a8dcdd Compare June 17, 2025 19:55
@ericsnowcurrently ericsnowcurrently force-pushed the interp-pool-executor-use-interp-call branch from bb8506b to 605c802 Compare June 17, 2025 20:20
@ericsnowcurrently ericsnowcurrently force-pushed the interp-pool-executor-use-interp-call branch from 32a8752 to cce4c75 Compare June 17, 2025 23:56
@ericsnowcurrently
Copy link
Member Author

Well, I figured out the problem. Apparently ThreadPoolExecutor only starts new workers if there aren't any idle ones (and max_workers hasn't been reached), which is normally reasonable.

However, when a task is submitted, there's a narrow window from the time the task is queued up to when a worker picks up the job. If a second task is submitted and the code reaches ThreadPoolExecutor._adjust_thread_count() before the worker created for the first task picks it up then a second worker will not be started in response to the second submit() call.

The new test (test_blocking), which was hanging, was written with the assumption that there would be one worker for each task. This lead to an effective deadlock when there were fewer workers started. As noted by @neonene, the same situation was reachable by restricting the number of workers.

The solution was to unblock the tasks that did get picked up by workers, so they could finish and the workers could pick up the remaining queued tasks.

@ericsnowcurrently ericsnowcurrently merged commit 725da50 into python:main Jun 18, 2025
39 checks passed
@miss-islington-app
Copy link

Thanks @ericsnowcurrently for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@ericsnowcurrently ericsnowcurrently deleted the interp-pool-executor-use-interp-call branch June 18, 2025 23:57
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 18, 2025
…tor (pythongh-133957)

Most importantly, this resolves the issues with functions and types defined in __main__.
It also expands the number of supported objects and simplifies the implementation.
(cherry picked from commit 725da50)

Co-authored-by: Eric Snow <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Jun 18, 2025

GH-135695 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Jun 18, 2025
ericsnowcurrently added a commit that referenced this pull request Jun 19, 2025
…utor (gh-135695)

Most importantly, this resolves the issues with functions and types defined in __main__.
It also expands the number of supported objects and simplifies the implementation.

(cherry picked from commit 725da50, AKA gh-133957)

Co-authored-by: Eric Snow <[email protected]>
lkollar pushed a commit to lkollar/cpython that referenced this pull request Jun 19, 2025
…tor (pythongh-133957)

Most importantly, this resolves the issues with functions and types defined in __main__.
It also expands the number of supported objects and simplifies the implementation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants