Skip to content

regrtest --parallel-threads shares TestCase._cleanups between threads #160543

Open

Description

@mpage

Bug report

regrtest's --parallel-threads runs the same test concurrently in multiple threads. To do so it creates independent copies of the tests using copy.copy():

cases = [copy.copy(self.test_case) for _ in range(self.num_threads)]

copy.copy() does a shallow copy, so the test's _cleanups list is shared between threads. Each thread runs the test's doCleanup method, popping and running methods from _cleanups until it's empty:

def doCleanups(self):
"""Execute all cleanup functions. Normally called for you after
tearDown."""
outcome = self._outcome or _Outcome()
while self._cleanups:
function, args, kwargs = self._cleanups.pop()
with outcome.testPartExecutor(self):
self._callCleanup(function, *args, **kwargs)
# return this for backwards compatibility
# even though we no longer use it internally
return outcome.success

Some cleanup functions may run more than once in a thread or may not run at all, because _cleanups is shared across all threads.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Oct 23, 2026
  2. self-assigned this
    on Oct 23, 2026
  3. added a commit that references this issue on Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

testsTests in the Lib/test dirtype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions