Skip to content

Race condition with tqdm.write #759

@orf

Description

@orf

tqdm.write() appears to have a race condition that triggers occasionally:

from tqdm import tqdm

def do_something(_):
    tqdm.write('test')

with ThreadPoolExecutor(max_workers=10) as pool:
    list(tqdm(pool.map(do_something, range(1000)))

This fixes it:

with ThreadPoolExecutor(max_workers=10) as pool:
    progress = tqdm(pool.map(do_something, range(1000))
    progress.get_lock()
    list(progress)

The issue appears to be in get_lock:

    @classmethod
    def get_lock(cls):
        """Get the global lock. Construct it if it does not exist."""
        if not hasattr(cls, '_lock'):
            cls._lock = TqdmDefaultWriteLock()
        return cls._lock

If two threads call this at the same time then the cls._lock will be overwritten, and cls._lock.release() at the end of external_write_mode will release the wrong lock.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions