Docstring of `Bounded.State` (Std.Sync.Channel) lists three invariants that fail between critical sections

Aperta Adatta ai principianti
#16,886 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
1/5
Tempo stimato
1-3 ore
Idoneità per principianti
86/100
Tipo di issue
Documentazione
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Ambito
documentation

Direzione di ricerca

Leggi il docstring di Bounded.State in src/Std/Sync/Channel.lean, in particolare gli invarianti intorno alle righe 370–377, insieme al comportamento di risveglio in trySend' e tryRecv' intorno alle righe 474 e 531. Aggiorna la formulazione per tenere conto del fatto che i processi in attesa risvegliati riprovano in sezioni critiche successive, usando come guida la riformulazione proposta nell’issue. Il lavoro è completato quando il docstring descrive quando valgono questi invarianti e i casi indicati in cui possono essere temporaneamente violati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Prerequisites
  • Check that your issue is not already filed:
    https://github.com/leanprover/lean4/issues
  • Reduce the issue to a minimal, self-contained, reproducible test case.
    Avoid dependencies to Mathlib or Batteries.
  • Test your test case against the latest nightly release, for example on
    https://live.lean-lang.org/#project=lean-nightly
    (You can also use the settings there to switch to “Lean nightly”)
  • When reporting a perceived soundness bug: Does it affect lake check --paranoid?
    If in doubt, please reach out on [email protected] for responsible disclosure.
Description

The docstring of the bounded channel's state (Channel.lean#L370-L377) lists these invariants:

2. `0 < bufCount → consumers = ∅`
3. `bufCount < capacity → producers = ∅`
4. `producers = ∅ ∨ consumers = ∅`, implied by 1, 2 and 3.

All three can be false between two critical sections. trySend' wakes only the first queued consumer (L474) and tryRecv' only the first queued producer (L531), and the woken waiter retries in a later critical section, so other sends and receives can run first. The three hold again once every woken waiter has retried.

Context

I found this while checking a model of the bounded channel with Veil, where all three came out false as invariants.

Steps to Reproduce

On a channel of capacity 1:

  1. Two recvs on the empty channel: both queue in consumers.
  2. send 1 buffers 1 and wakes the first receiver only, so bufCount = 1 while the second is still queued (2 fails).
  3. Before the woken receiver retries, send 2 finds the buffer full and queues, so producers and consumers are both nonempty (4 fails).

For 3: send 1 is buffered, send 2 and send 3 queue, then one recv takes 1 and wakes only send 2, so bufCount = 0 while send 3 is still queued.

Expected behavior: the listed invariants hold whenever the lock is free.

Actual behavior: 2, 3 and 4 can fail between critical sections while woken waiters have yet to retry.

Versions

Lean (version 4.38.0-nightly-2026-11-29, x86_64-unknown-linux-gnu, commit 80362eeeada90bd5e5b0aae9d1d15dd95978a6d4, Release). The text is the same on master and in v4.36.0. Amazon Linux 2023.

Additional Information

A possible rewording:

 1. `0 < capacity`
-2. `0 < bufCount → consumers = ∅`
-3. `bufCount < capacity → producers = ∅`
-4. `producers = ∅ ∨ consumers = ∅`, implied by 1, 2 and 3.
+2. `0 < bufCount → consumers = ∅`, once every woken consumer has retried
+3. `bufCount < capacity → producers = ∅`, once every woken producer has retried
+4. `producers = ∅ ∨ consumers = ∅`, once every woken waiter has retried (by 1, 2 and 3)
 5. `bufCount` corresponds to the amount of slots in `buf` that are `some`.
 6. `sendIdx = (recvIdx + bufCount) % capacity`. However all four of these values still get tracked
    as there is potential to make a non-blocking send lock-free in the future with this approach.
 7. `closed = true → consumers = ∅ ∧ producers = ∅`
+
+A send wakes only the first queued consumer and a receive only the first queued producer, and the
+woken waiter retries in a later critical section. Until it has, a value can be buffered while
+consumers are queued, a slot can be free while producers are queued, and both queues can be
+nonempty.

Happy to send it as a PR if that's easier :)

Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Lingua principale
Lean
Stelle
10.5k
Fork
1.2k
Merge medio
17h 55m
PR unite (30g)
356

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di leanprover/lean4

Tutte le issue di leanprover/lean4

Issue simili

Altre issue su Documentation

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.