Azure / azure-functions-python-worker

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

[BUG] queue_output might generate several messages from 1 string. #1492

Open olsgaard opened 4 months ago

olsgaard commented 4 months ago

When using Queue Output Binding, sending a string as a queue message might result in several different queue messages. This does not align with the documentation[1], nor developer expectation.

For example, the documentation states that the following function will "set an individual message on the queue"

import azure.functions as func

def main(req: func.HttpRequest, msg: func.Out[str]) -> func.HttpResponse:
    input_msg = req.params.get('message')
    msg.set(input_msg)
    return 'OK'

However, if the message contains a string that looks like a json array, this will post multiple messages to the queue.

Version

Python version 3.10.14 (python3). Azure Functions Core Tools Core Tools Version: 4.0.5611 Commit hash: N/A +591b8aec842e333a87ea9e23ba390bb5effe0655 (64-bit) Function Runtime Version: 4.31.1.22191

Repro steps

Provide the steps required to reproduce the problem:

Create a V2 Python Function app and include the following functions:

@app.queue_output(arg_name="$return", queue_name="start-queue", connection="AzureWebJobsStorage", data_type='BINARY')
@app.schedule(schedule="30 * * * * *", arg_name="myTimer", run_on_startup=True, use_monitor=False)
def timer_trigger(myTimer: func.TimerRequest) -> str:
    output = '[1,2,3,4,5]'
    return output

@app.queue_trigger(arg_name="msg", queue_name="start-queue", connection="AzureWebJobsStorage")
def queue_trigger(msg: func.QueueMessage):
    payload = msg.get_json()
    logging.info(f"Python Queue trigger processed a message: {payload} of type {type(payload)}")

Expected behavior

Provide a description of the expected behavior.

We expect 1 message is sent to queue when timer_trigger is executed, which results in 1 invocation of queue_trigger

[2024-05-08T06:53:30.929Z] Python Queue trigger processed a message: [1, 2, 3, 4, 5] of type <class 'list'>
[2024-05-08T06:53:30.931Z] Executed 'Functions.queue_trigger' (Succeeded, Id=1d1e3129-7a56-4168-841d-32ab050b032b, Duration=31ms)

Actual behavior

Provide a description of the actual behavior observed.

5 messages are sent to queue and 5 invocations of queue_trigger, each receiving a single number.

[2024-05-08T06:51:31.369Z] Python Queue trigger processed a message: 1 of type <class 'int'>
[2024-05-08T06:51:31.370Z] Executed 'Functions.queue_trigger' (Succeeded, Id=66d11682-2e8f-4f1d-8893-17765d51c3fe, Duration=15ms)
[2024-05-08T06:51:31.378Z] Python Queue trigger processed a message: 2 of type <class 'int'>
[2024-05-08T06:51:31.379Z] Python Queue trigger processed a message: 3 of type <class 'int'>
[2024-05-08T06:51:31.379Z] Executed 'Functions.queue_trigger' (Succeeded, Id=acd0b0da-de60-4244-b9a2-70aabcc4876d, Duration=24ms)
[2024-05-08T06:51:31.380Z] Executed 'Functions.queue_trigger' (Succeeded, Id=41af8b4a-afe3-4c95-9b7a-1169e7821403, Duration=24ms)
[2024-05-08T06:51:31.382Z] Python Queue trigger processed a message: 4 of type <class 'int'>
[2024-05-08T06:51:31.382Z] Python Queue trigger processed a message: 5 of type <class 'int'>
[2024-05-08T06:51:31.383Z] Executed 'Functions.queue_trigger' (Succeeded, Id=036fec0f-b10b-49da-9f71-1d3534f5d249, Duration=26ms)
[2024-05-08T06:51:31.383Z] Executed 'Functions.queue_trigger' (Succeeded, Id=2076cc59-a4d5-458c-bb0f-e0e90112b372, Duration=27ms)

Note, that according to the documentation we should expect 5 messages if we return a list of 5 items in timer_trigger(), however, we send 1 and only 1 string.

Known work around

If we wrap the output in braces and return a list of length 1, we get the expected result. However, the documentation states that a single string will result in a single message, and following the principle of least surprise, this should be true.

Contents of the requirements.txt file:

Provide the requirements.txt file to help us find out module related issues.
azure-functions

Related information

Provide any related information

[1] https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-queue-output?tabs=python-v1%2Cisolated-process%2Cnodejs-v4%2Cextensionv5&pivots=programming-language-python

bhagyshricompany commented 4 months ago

Thanks for reporting will check and update you.

bhagyshricompany commented 4 months ago

@gavin-aguiar pls comment and validate.