Skip to content

test(executorch): keep the sys.modules leak checks honest under PYTEST_ADDOPTS - #4918

Closed
shoumikhin wants to merge 1 commit into
pytorch:mainfrom
shoumikhin:executorch-leak-guard-env
Closed

shoumikhin wants to merge 1 commit into
pytorch:mainfrom
shoumikhin:executorch-leak-guard-env

Conversation

@shoumikhin

@shoumikhin shoumikhin commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

What is wrong today

Two tests check that another test leaves no copy of torch_tensorrt_executorch_runtime in sys.modules, since a later test in the same process would import that copy instead of the real package. Each one runs the other test in a child pytest process and then looks at the child's sys.modules:

  • test_the_example_loader_test_leaves_no_stand_in_behind
  • test_the_shim_test_leaves_no_delegate_module_behind

The child inherits PYTEST_ADDOPTS. If it contains -n 2, pytest-xdist runs the inner test in a separate worker process. The child's own sys.modules never gets the module, so both checks pass even when the leak is back. The -o addopts= flag does not help, because it clears only the ini setting, not the environment variable.

When the checks do fail, the output says only "1 passed" next to assert 1 == 0, which does not say what went wrong.

What this change does

  • Run the child without PYTEST_ADDOPTS.
  • Give the child a timeout, so a hung child fails the test instead of the whole job.
  • Exit with a message that says either that the inner test failed, with its exit code, or that it left the module in sys.modules.

What was tested

On Linux x86-64 with the pinned ExecuTorch nightly, with the two leak fixes from #4848 removed on purpose:

PYTEST_ADDOPTS unset PYTEST_ADDOPTS="-n 2"
before this change 2 failed 2 passed (the bug)
after this change 2 failed 2 failed

With the leak fixes in place, both checks pass either way, and test_python_runtime.py plus test_shared_runtime_workflow.py give 104 passed, the same as on main.

@meta-cla meta-cla Bot added the cla signed label Oct 22, 2026
@github-actions github-actions Bot added the component: tests Issues re: Tests label Oct 22, 2026
…T_ADDOPTS

Two tests run another test in a child pytest process and then check that it
left no copy of torch_tensorrt_executorch_runtime in sys.modules. The child
inherited PYTEST_ADDOPTS. With "-n 2" in it, the inner test ran in an xdist
worker, so the child's own sys.modules never held the module and both checks
passed even with the leak fixes removed. "-o addopts=" clears only the ini
value, not the variable.

Run the child without PYTEST_ADDOPTS, give it a timeout so a hung child
fails the test instead of the whole job, and make a failure say which of the
two things went wrong.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant