Nettacker icon indicating copy to clipboard operation
Nettacker copied to clipboard

[Bug] List modification during iteration in wait_for_threads_to_finish skips threads

Open juandiego-bmu opened this issue 8 months ago • 3 comments

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)

juandiego-bmu avatar Mar 28 '26 14:03 juandiego-bmu

Hi, I’d like to work on this issue. I’m currently exploring the Nettacker codebase and planning to contribute.

Vinubr avatar Mar 31 '26 10:03 Vinubr

Hi @Vinubr, I've already submitted a fix for this in PR #1471. Feel free to take a look!

juandiego-bmu avatar Mar 31 '26 11:03 juandiego-bmu

Addressed in #1498

securestep9 avatar Aug 21 '26 07:08 securestep9