Async credential provider fails with "no running event loop" when credentials are refreshed during a long async operation

Aperta
#819 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
aws, python, rust
Ambito
backend, cloud

Direzione di ricerca

Start at the async credential_provider handling used by S3Store and put_async, then trace how credential refresh invokes Python while the multipart upload is running. Reproduce with the supplied example using credentials that expire during the upload; done means the provider is awaited on the operation's running event loop and the upload completes without no running event loop. The issue names no source files or tests, so first locate the provider implementation and related async tests.

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

Descrizione

Summary

An async credential_provider works for the first calls of an async operation, but when credentials expire in the middle of a long-running async operation (e.g. a multipart put_async), the refresh fails with RuntimeError: no running event loop, and the operation fails with UnauthenticatedError.

The docs say async credential providers are supported only with obstore's async APIs while an event loop is running. Here both conditions hold: put_async is awaited inside a running event loop, and the loop keeps running for the whole upload.

Reproduction

Credentials are valid for 1 second, the upload takes about 6 seconds:

import asyncio
import urllib.request
from datetime import UTC, datetime, timedelta

import obstore
from moto.server import ThreadedMotoServer
from obstore.store import S3Store


async def credential_provider():
    print('credential_provider called')
    return {
        'access_key_id': 'test',
        'secret_access_key': 'test',
        'token': None,
        'expires_at': datetime.now(UTC) + timedelta(seconds=1),
    }


async def slow_data():
    for _ in range(3):
        await asyncio.sleep(2)
        yield b'x' * 5 * 2**20


async def main():
    server = ThreadedMotoServer(port=0, verbose=False)
    server.start()
    host, port = server.get_host_and_port()
    endpoint = f'http://{host}:{port}'
    urllib.request.urlopen(urllib.request.Request(f'{endpoint}/bucket', method='PUT'))

    store = S3Store(
        'bucket',
        endpoint=endpoint,
        region='us-east-1',
        client_options={'allow_http': True},
        credential_provider=credential_provider,
    )
    try:
        await obstore.put_async(store, 'key', slow_data(), use_multipart=True, chunk_size=5 * 2**20)
    finally:
        server.stop()


asyncio.run(main())

Output:

credential_provider called
credential_provider called
Traceback (most recent call last):
  ...
obstore.exceptions.UnauthenticatedError: The operation lacked valid authentication credentials for path External AWS credential provider: RuntimeError: no running event loop

Debug source:
Unauthenticated {
    path: "External AWS credential provider",
    source: PyErr {
        type: <class 'RuntimeError'>,
        value: RuntimeError('no running event loop'),
        traceback: None,
    },
}
Expected

The provider is awaited on the event loop that the async operation was started from, and the upload completes.

Context

A workaround is a synchronous provider that runs the async code on the original loop with anyio.from_thread.run(..., token=anyio.lowlevel.current_token()) captured in that loop. obstore calls the provider from its own threads, so this does not deadlock.

Environment
  • obstore 0.12.1
Lingua principale
Python
Stelle
842
Fork
50
Merge medio
9h
PR unite (30g)
13

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

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 developmentseed/obstore

Tutte le issue di developmentseed/obstore

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.