Azure / azure-functions-python-worker

Python worker for Azure Functions.
http://aka.ms/azurefunctions
MIT License
331 stars 100 forks source link

fix: unique clients per function #1490

Open hallvictoria opened 1 month ago

hallvictoria commented 1 month ago

Description

Previously, the cache for deferred bindings returned a client based on the resource it was attached to. Therefore, two functions that had client types pointed to the same resource would receive the same client.

For example, these two functions would both use the same client when executing because the blob is the same:

@app.route(route="stream_upload")
@app.blob_input(
    arg_name="client",
    path="streamingtest/tech_blog.mp4",
    connection="DeferredBindingConnectionString"
)
async def stream_upload(req: Request, client: blob.AioBlobClient) -> str:
   async with client:
    await client.create_append_blob()
    await stream_upload(client, req)

    return "Uploaded to blob"    

@app.route(route="stream_download")
@app.blob_input(
    arg_name="client",
    path="streamingtest/tech_blog.mp4",
    connection="DeferredBindingConnectionString"
)
async def stream_download(req: Request, client: blob.AioBlobClient):
    props = await client.get_blob_properties()
    return "OK"

This causes an issue specifically for async client types and does not affect sync client types. After stream_upload is executed and stream_download is called, await client.get_blob_properties() will fail with AttributeError: 'NoneType' object has no attribute '__aenter__'. This is an issue that also occurs when using azure-storage-blob-aio directly.

This changes the cache to take in the function name as an additional part of the key so that client types will be unique based on the function.

Fixes #


PR information

Quality of Code and Contribution Guidelines