Skip to content

[Bug] List modification during iteration in wait_for_threads_to_finish skips threads #1465

@juandiego-bmu

Description

@juandiego-bmu

Description

In nettacker/core/utils/common.py (lines 58-75), wait_for_threads_to_finish modifies the threads list while iterating over it:

def wait_for_threads_to_finish(threads, maximum=None, terminable=False, sub_process=False):
    while threads:
        try:
            for thread in threads:
                if not thread.is_alive():
                    threads.remove(thread)

When threads.remove(thread) is called inside the for thread in threads loop, the iterator shifts and the next element is skipped. For example, if threads at indices 2 and 3 both finish, removing index 2 shifts index 3 to index 2, but the iterator moves to index 3, skipping what was originally thread 3.

Impact

  • Some finished threads may not be removed from the list promptly (they get picked up in the next while iteration, adding unnecessary delay)
  • In edge cases with the maximum parameter, the thread count check on line 64 may be inaccurate because not all finished threads were removed

Suggested fix

Build a new list instead of modifying during iteration:

while threads:
    try:
        threads = [t for t in threads if t.is_alive()]
        if maximum and len(threads) < maximum:
            break
        time.sleep(0.01)

Or iterate over a copy:

for thread in threads[:]:  # iterate over a copy
    if not thread.is_alive():
        threads.remove(thread)

Metadata

Metadata

Assignees

Labels

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions