Azure function Python Http Trigger 'Operation was aborted' throws KeyError

Ellie Howe 10 Reputation points
2024-04-12T10:39:26.52+00:00

I am locally developing an azure function http trigger with Python. My function runs, half way through I get the message 'Operation was aborted'. A few minutes pass and the code continues successfully but the function is no longer executing. My code then throws the following KeyError:

Exception has occurred: KeyError
exception: no description
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9\WINDOWS\X64\azure\functions\_thirdparty\werkzeug\datastructures.py", line 985, in __getitem__
    raise KeyError()
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9\WINDOWS\X64\azure\functions\_thirdparty\werkzeug\datastructures.py", line 1149, in __contains__
    self.__getitem__(key, _get_mode=True)
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9\WINDOWS\X64\azure\functions\http.py", line 87, in encode
    if 'content-type' not in headers:
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9\WINDOWS\X64\azure_functions_worker\bindings\meta.py", line 107, in get_datum
    datum = binding.encode(obj, expected_type=pytype)
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9\WINDOWS\X64\azure_functions_worker\bindings\meta.py", line 152, in to_outgoing_proto
    datum = get_datum(binding, obj, pytype)
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 532, in _handle__invocation_request
    return_value = bindings.to_outgoing_proto(
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 266, in _dispatch_grpc_request
    resp = await request_handler(request)
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9\WINDOWS\X64\azure_functions_worker\main.py", line 60, in main
    return aio_compat.run(start_async(
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9\WINDOWS\X64\worker.py", line 71, in <module>
    main.main()
KeyError: 

My function:


import azure.functions as func
import os
import logging
from ChapmanFreeborn_Xeneta_RetrieveLatest_Src.selfie_interface import SelfieInterface
from ChapmanFreeborn_Xeneta_RetrieveLatest_Src.excel_file_manager import ExcelFileManager

app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)

@app.route(route="RetrieveLatestReport")
def RetrieveLatestReport(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    # Parameters for SelfieInterface
    tenant = os.getenv('SELF_Tenant')
    username = os.getenv('SELF_Username')
    password = os.getenv('SELF_Password')

    # Init SelfieInterface
    iface = SelfieInterface(tenant)

    # Init ExcelFileManager
    xlfm = ExcelFileManager()

    # Logging request headers for inspection
    headers_str = ", ".join(f"{key}: {value}" for key, value in req.headers.items())
    logging.info(f"Request Headers: {headers_str}")

    # Login
    iface.login(username, password)

    # Get Latest Dataset
    latest_dataset = iface.get_latest_dataset()

    # Download dataset
    zip_file_path = iface.download_dataset(latest_dataset)

    if zip_file_path:
        new_excel_filename = "LatestReport.xlsx"
        excel_file_path = xlfm.unzip_excel_file(zip_file_path, new_excel_filename, password=os.getenv('EXCEL_File_Password'))
        
        # Check if file was successfully renamed
        if excel_file_path:
            # Return a response
            return func.HttpResponse("Dataset downloaded successfully", status_code=200)
        else:
            return func.HttpResponse("Failed to rename Excel file", status_code=500)
    else:
        return func.HttpResponse("Failed to download dataset", status_code=500)
    
        

it aborts after the dataset is downloaded and '[2024-04-12T10:21:24.833Z] Dataset downloaded to LatestReport.zip'

and the rest of the code executes after the function stops executing

Executing task: .venv\Scripts\activate ; func host start 
Found Python version 3.9.0 (py).
Azure Functions Core Tools
Core Tools Version:       4.0.5571 Commit hash: N/A +9a5b604f0b846df7de3eb37b423a9eba8baa1152 (64-bit)
Function Runtime Version: 4.30.0.22097
For detailed output, run func with --verbose flag.
For detailed output, run func with --verbose flag.
[2024-04-12T10:21:12.612Z] Host lock lease acquired by instance ID '0000000000000000000000000872075F'.                                           0affb5
[2024-04-12T10:21:21.100Z] Executing 'Functions.RetrieveLatestReport' (Reason='This function was programmatically called via the host APIs.', Id=02024-04-12T10:21:21.408Z] Python HTTP trigger function processed a request.[2024-04-12T10:21:21.416Z] Request Headers: host: localhost:7071, accept-enFor detailed output, run func with --verbose flag.                                                                                               ,er-en
[2024-04-12T10:21:12.612Z] Host lock lease acquired by instance ID '0000000000000000000000000872075F'.                                           r-idea
[2024-04-12T10:21:21.100Z] Executing 'Functions.RetrieveLatestReport' (Reason='This function was programmatically called via the host APIs.', Id=0affb5f6-8ff0-4a93-b932-d039f56a0079)
[2024-04-12T10:21:21.408Z] Python HTTP trigger function processed a request.[2024-04-12T10:21:21.416Z] Request Headers: host: localhost:7071, acckzbpaQept-encoding: gzip,deflate, x-ms-correlation-request-id: a444c2b2-0a20-410a-88ad-f2cee963de92, accept-language: en, content-type: application/json, user-agent: vscode-azurefunctions/1.14.1 core-rest-pipeline/1.12.3 Node/v18.17.1 OS/(x64-Windows_NT-10.0.19045), connection: close, x-ms-client-request-id: d3ea903a-d41e-4b1b-b1ee-e18edccaa258, content-length: 2
[2024-04-12T10:21:22.523Z] Logged in Successfully
[2024-04-12T10:21:24.833Z] Dataset downloaded to LatestReport.zip
[2024-04-12T10:24:16.249Z] Excel file 'reports.xlsx' extracted
[2024-04-12T10:24:16.288Z] Excel file 'reports.xlsx' renamed to 'LatestReport.xlsx'

I feel like I have tried everything I know to narrow down why I would be receiving this error and why the function would abort, but I cannot seem to resolve the issue.

Would anyone be able to help me with this, please?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,001 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.