python/cpython · error · ValueError

parties must be >= 1

Error message

parties must be >= 1

What it means

asyncio.Barrier requires at least one participating task; parties < 1 makes wait() unsatisfiable (the barrier could never fill), so __init__ raises ValueError('parties must be >= 1') immediately.

Source

Thrown at Lib/asyncio/locks.py:485

    FILLING = 'filling'
    DRAINING = 'draining'
    RESETTING = 'resetting'
    BROKEN = 'broken'


class Barrier(mixins._LoopBoundMixin):
    """Asyncio equivalent to threading.Barrier

    Implements a Barrier primitive.
    Useful for synchronizing a fixed number of tasks at known synchronization
    points. Tasks block on 'wait()' and are simultaneously awoken once they
    have all made their call.
    """

    def __init__(self, parties):
        """Create a barrier, initialised to 'parties' tasks."""
        if parties < 1:
            raise ValueError('parties must be >= 1')

        self._cond = Condition() # notify all tasks when state changes

        self._parties = parties
        self._state = _BarrierState.FILLING
        self._count = 0       # count tasks in Barrier

    def __repr__(self):
        res = super().__repr__()
        extra = f'{self._state.value}'
        if not self.broken:
            extra += f', waiters:{self.n_waiting}/{self.parties}'
        return f'<{res[1:-1]} [{extra}]>'

    async def __aenter__(self):
        # wait for the barrier reaches the parties number
        # when start draining release and return index of waited task
        return await self.wait()

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Validate/derive parties with max(1, n) only if 1-party semantics are acceptable, else reject the config earlier
  2. Fail fast with a descriptive config error before constructing the barrier
  3. Skip barrier coordination entirely when there are fewer than 2 participants (a 1-party barrier is a no-op)

Example fix

# before
barrier = asyncio.Barrier(cfg.worker_count)  # 0 -> ValueError

# after
if cfg.worker_count < 1:
    raise ConfigError('worker_count must be >= 1')
barrier = asyncio.Barrier(cfg.worker_count)
Defensive patterns

Strategy: validation

Validate before calling

if not isinstance(parties, int) or parties < 1:
    raise ValueError(f'barrier parties must be >= 1, got {parties!r}')
barrier = asyncio.Barrier(parties)

Type guard

def is_valid_barrier_parties(n) -> bool:
    return isinstance(n, int) and n >= 1

Prevention

When it happens

Trigger: asyncio.Barrier(0) or Barrier(-n); computing parties from a shard count, worker count, or config that can be zero (e.g. Barrier(num_replicas) with none configured).

Common situations: Config-driven fan-out where a component count of 0 is possible; deriving parties from len(empty_list); environment-specific deployments (local dev with 0 workers) passing 0 through.

Related errors


AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14). Data as JSON: /api/errors/7bbd5191eb2efe30. Report an issue: GitHub.