Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-132775: Fix Interpreter.call() __main__ Visibility #135595

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 Jun 16, 2025

As noted in the new tests, there are a few situations we must carefully accommodate
for functions that get pickled during interp.call(). We do so by running the script
from the main interpreter's __main__ module in a hidden module in the other
interpreter. That hidden module is used as the function __globals__.

@ericsnowcurrently ericsnowcurrently marked this pull request as ready for review June 16, 2025 23:46
@ericsnowcurrently ericsnowcurrently added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Jun 16, 2025
@bedevere-bot
Copy link

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

Results will be shown at:

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

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

@bedevere-bot bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Jun 16, 2025
finally:
Py_DECREF(msgobj);
return res;
assert(PyImport_GetModule(&_Py_ID(__main__)) == main->cached.module);
Copy link
Contributor

Choose a reason for hiding this comment

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

The refleaks in test_crossinterp.py come from PyImport_GetModule() in the assertion.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

assert(main->cached.module != NULL);
assert(main->cached.loaded != NULL);
PyObject *exc = _PyErr_GetRaisedException(tstate);
assert(PyImport_GetModule(&_Py_ID(__main__)) == main->cached.loaded);
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

}
finally:
if (exc != NULL) {
ctx->main.cached.failed = _PyErr_GetRaisedException(tstate);
Copy link
Contributor

Choose a reason for hiding this comment

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

ctx->main.cached.failed can be set by *_isolated_main() above via sync_module_capture_exc(), in which case test_func_in___main___invalid fails with refleaks.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

Comment on lines 125 to 128
if (_Py_CheckMainModule(mod) < 0) {
// This is probably unrecoverable, so don't bother caching the error.
assert(_PyErr_Occurred(tstate));
return -1;
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that Py_XDECREF(mod) does not need to follow all _Py_CheckMainModule() in this file on error, unlike in ceval.c.

This is a little off topic and maybe unlikely, but I'd prefer _Py_CheckMainModule() to decref the error message, as the function can be used without _Py_GetMainModule():

cpython/Python/pystate.c

Lines 1142 to 1145 in 8dd8b5c

PyObject *msg = PyUnicode_FromString("invalid __main__ module");
if (msg != NULL) {
(void)PyErr_SetImportError(msg, &_Py_ID(__main__), NULL);
}

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@ericsnowcurrently
Copy link
Member Author

Once again, @neonene, thanks for taking the time to track down these subtle details!

@ericsnowcurrently ericsnowcurrently added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Jun 17, 2025
@bedevere-bot
Copy link

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

Results will be shown at:

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

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

@bedevere-bot bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Jun 17, 2025
@ericsnowcurrently
Copy link
Member Author

!buildbot AMD64 CentOS9 NoGIL Refleaks

@bedevere-bot
Copy link

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

Results will be shown at:

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

The command will test the builders whose names match following regular expression: AMD64 CentOS9 NoGIL Refleaks

The builders matched are:

  • AMD64 CentOS9 NoGIL Refleaks PR

@ericsnowcurrently ericsnowcurrently merged commit 269e19e into python:main Jun 17, 2025
52 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 fix-interp-call-main-visibility branch June 17, 2025 19:17
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 17, 2025
…-135595)

As noted in the new tests, there are a few situations we must carefully accommodate
for functions that get pickled during interp.call().  We do so by running the script
from the main interpreter's __main__ module in a hidden module in the other
interpreter.  That hidden module is used as the function __globals__.
(cherry picked from commit 269e19e)

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

bedevere-app bot commented Jun 17, 2025

GH-135638 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 17, 2025
ericsnowcurrently added a commit that referenced this pull request Jun 17, 2025
As noted in the new tests, there are a few situations we must carefully accommodate
for functions that get pickled during interp.call().  We do so by running the script
from the main interpreter's __main__ module in a hidden module in the other
interpreter.  That hidden module is used as the function __globals__.

(cherry picked from commit 269e19e, AKA gh-135595)

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

As noted in the new tests, there are a few situations we must carefully accommodate
for functions that get pickled during interp.call().  We do so by running the script
from the main interpreter's __main__ module in a hidden module in the other
interpreter.  That hidden module is used as the function __globals__.
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.

3 participants