Skip to content

sys.remote_exec() scripts run in the __main__ namespace #132859

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

Closed
godlygeek opened this issue Apr 23, 2025 · 0 comments
Closed

sys.remote_exec() scripts run in the __main__ namespace #132859

godlygeek opened this issue Apr 23, 2025 · 0 comments
Labels
3.14 new features, bugs and security fixes type-bug An unexpected behavior, bug, or error

Comments

@godlygeek
Copy link
Contributor

godlygeek commented Apr 23, 2025

Bug report

Bug description:

The scripts injected by sys.remote_exec run in the context of the __main__ module, meaning that they unintentionally overwrite variables used in the main script. For instance, given a loop_forever.py containing:

import os
print(os.getpid())

x = 1
while x == 1:
    pass

print(f"{x=}")

and an injected.py containing:

x = 42

Using sys.remote_exec() to inject injected.py into an interpreter running loop_forever.py results in x=42 being printed out and the script exiting.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

@godlygeek godlygeek added the type-bug An unexpected behavior, bug, or error label Apr 23, 2025
pablogsal pushed a commit that referenced this issue Apr 23, 2025
Run debugger scripts in their own namespaces

Previously scripts injected by `sys.remote_exec` were run with the
globals of the `__main__` module. Instead, run each injected script
with an empty set of globals. If someone really wants to use the
`__main__` module's namespace, they can always `import __main__`.
@ZeroIntensity ZeroIntensity added the 3.14 new features, bugs and security fixes label Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.14 new features, bugs and security fixes type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants