[Bug] List modification during iteration in wait_for_threads_to_finish skips threads
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
whileiteration, adding unnecessary delay) - In edge cases with the
maximumparameter, 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)
Hi, I’d like to work on this issue. I’m currently exploring the Nettacker codebase and planning to contribute.
Hi @Vinubr, I've already submitted a fix for this in PR #1471. Feel free to take a look!
Addressed in #1498