MagicStack / MagicStack/asyncpg
Potential starvation with connection pool
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 8.1k
- Forks
- 468
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
I have the following piece of code where I initialize 100 workers and a connection pool of size 10. Each worker loops and trys to acquire a connection and hold it for 200ms.
import asyncio
import asyncpg
async def worker(pool: asyncpg.Pool):
while True:
async with pool.acquire(timeout=10):
# hold the connection for 200ms
await asyncio.sleep(0.2)
async def main():
async with asyncpg.pool.create_pool(max_size=10) as pool:
async with asyncio.TaskGroup() as tg:
for _ in range(100):
tg.create_task(worker(pool))
asyncio.run(main())
When running on my laptop, this code reliably crashes with a TimeoutError after a few minutes, meaning that at least one worker failed to acquire a connection after 10 seconds. Given that there're 10 workers per connection and each worker holds the connection for 200ms, I would expect workers to be able to acquire a connection every 2 seconds.
This looks like starvation to me. Are there fairness guarantees regarding connection pools? Is there anything I could do other than just increasing the max pool size?
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, die bereitgestellte Python-Reproduktion mit asyncio.TaskGroup, 100 Workern und asyncpg.pool.create_pool(max_size=10) auszuführen. Untersuche das Verhalten von pool.acquire(timeout=10) unter Konkurrenz und bestimme, ob der beobachtete TimeoutError auf Verhungern oder auf erwartete Ablaufplanung zurückzuführen ist; abgeschlossen bedeutet, die Fairness-Garantie zu dokumentieren oder einen reproduzierbaren Pool-Fehler zu identifizieren.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- postgresql, python
- Bereich
- databases
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100