python-3.xazureazure-functionsdependenciescontinuous-deployment

azure function responding with 503 or 502 error after some intervals


I have an azure function deployed which is giving this exception as follows:

[![Unable to reload azure.functions. Using default. Exception: cannot import name 'BlobSource' from 'azure.functions.decorators' (/azure-functions-host/workers/python/3.9/LINUX/X64/azure/functions/decorators/init.py)][1]][1]

Along with the above exception, I am keep getting 502 or 503 error after some intervals and then it return to its ok response i.e 200.

I am deploying the azure function via the following yml file:



name: Deploy Python project to Azure Function App



on:

  push:

    branches: ["main"]



env:

  AZURE_FUNCTIONAPP_NAME: 'my-function-name'   

  AZURE_FUNCTIONAPP_PACKAGE_PATH: '.'       

  PYTHON_VERSION: '3.9'                     



jobs:

  test:

    runs-on: ubuntu-latest



    steps:

    - uses: actions/checkout@v4

    - name: Set up Python 3.9

      uses: actions/setup-python@v3

      with:

        python-version: ${{ env.PYTHON_VERSION }}

    - name: Install dependencies

      run: |

        python -m pip install --upgrade pip

        pip install flake8 pytest

        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

    - name: Test with pytest

      run: |

        pytest --disable-warnings



  build-and-deploy:

    runs-on: ubuntu-latest

    needs: test

    if: ${{ needs.test.result == 'success' }}

    environment: dev

    steps:

    - name: 'Checkout GitHub Action'

      uses: actions/checkout@v4



    - name: Setup Python ${{ env.PYTHON_VERSION }} Environment

      uses: actions/setup-python@v4

      with:

        python-version: ${{ env.PYTHON_VERSION }}



    - name: 'Resolve Project Dependencies Using Pip'

      shell: bash

      run: |

        pushd './${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}'

        python -m pip install --upgrade pip

        pip install -r requirements.txt --target=".python_packages/lib/site-packages"

        popd



    - name: 'Run Azure Functions Action'

      uses: Azure/functions-action@v1

      id: fa

      with:

        app-name: ${{ env.AZURE_FUNCTIONAPP_NAME }}

        package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}

        publish-profile: ${{ secrets.AZURE_FUNCTIONAPP_PUBLISH_PROFILE }} # Remove publish-profile to use Azure RBAC

        scm-do-build-during-deployment: true

        enable-oryx-build: true

requirements.txt



azure-functions==1.20.0

pandas==2.2.2

pydantic==2.8.2

pydantic_core==2.20.1

pytest==8.3.2

pytest-asyncio==0.23.8

requests==2.32.3

runtime version: 4 python version: 3.9

I am trying to figure out what might be causing this exception, but the exception is persistent.

Any help would be greatly appreciated!

function_app.py

import azure.functions as func
import logging
from src.funcmain import *

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

@app.route(route="register-account",methods=['POST'])
async def account_registration(req: func.HttpRequest) -> func.HttpResponse:
    logging.info(f'Request received from {req.url}')

    try:
        return await register_account(req=req)
    
    except Exception as e:
        logging.error(f'Error processing request: {str(e)}')
        return func.HttpResponse(f"Internal server error :{str(e)}", status_code=500)
    
@app.route(route="contact",methods=['POST'])
async def contact(req: func.HttpRequest) -> func.HttpResponse:
    logging.info(f'Request received from {req.url}')

    try:
        if req.params.get('action') == 'update':
            return await update_contact(req=req)
        
        elif req.params.get('action') == 'create':
            return await register_contact(req=req)
        
        else:
            return func.HttpResponse(f"Invalid action: {req.params.get('action')}", status_code=400)
    
    except Exception as e:
        logging.error(f'Error processing request: {str(e)}')
        return func.HttpResponse(f"Internal server error :{str(e)}", status_code=500)

@app.route(route="lead", methods=['POST'])
async def lead_operation(req: func.HttpRequest) -> func.HttpResponse:
    logging.info(f'Request received from {req.url}')
    try:
        if req.params.get('action') == 'update':
            return await update_lead(req)
        elif req.params.get('action') == 'create':
            return await create_lead(req)
        else:
            return func.HttpResponse(f"Invalid action: {req.params.get('action')}", status_code=400)
    except Exception as e:
        logging.error(f'Error processing request: {str(e)}')
        return func.HttpResponse(f"Internal server error: {str(e)}", status_code=500)

@app.route(route="offer", methods=['POST'])
async def offer_operation(req: func.HttpRequest) -> func.HttpResponse:
    logging.info(f'Request received from {req.url}')
    try:
        if req.params.get('action') == 'create':
            return await create_offer(req)
        elif req.params.get('action') == 'update':
            return await update_offer(req)
        else:
            return func.HttpResponse(f"Invalid action: {req.params.get('action')}", status_code=400)
    except Exception as e:
        logging.error(f'Error processing request: {str(e)}')
        return func.HttpResponse(f"Internal server error: {str(e)}", status_code=500)

@app.route(route="watchlist", methods=['POST'])
async def watchlist_operation(req: func.HttpRequest) -> func.HttpResponse:
    logging.info(f'Request received from {req.url}')
    try:
        if req.params.get('action') == 'create':
            return await add_watchlist(req)
        elif req.params.get('action') == 'update':
            return await update_watchlist(req)
        else:
            return func.HttpResponse(f"Invalid action: {req.params.get('action')}", status_code=400)
    except Exception as e:
        logging.error(f'Error processing request: {str(e)}')
        return func.HttpResponse(f"Internal server error: {str(e)}", status_code=500)

@app.route(route="ping", methods=['GET'])
async def ping(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Ping request received.')
    return func.HttpResponse("Service is Up!", status_code=200)

Solution

  • Just to help other who maybe facing such error where your function app is giving responses between 502 or 503 intermittently.

    Follow the following steps:

    1. Troubleshoot the issue using the diagnostic tool and solve problems through azure portal

    2. If above didn't gave you any insights, try looking into the runtime logs using log streaming found in left menu.

    3. (Only for those who has subscribe to premium provision)If you're still not getting any insight or leads that may help resolve issue, check the Kudu console.Link to Console : http://<your-function-name>.scm.azurewebsites.net(check logs or memory consumption)

    4. If you're still not getting any insights or unable to find exception, Then it might be region specific.Your server location might be experiencing some problem.

    Try migrating your app to new region (which is bit handy) or contact support team.

    I am attaching some doc for your reference:

    1. Azure function returns 503 after 30 seconds

    2. https://learn.microsoft.com/en-us/answers/questions/1287868/how-to-solve-azure-functions-runtime-is-unreachabl

    3. https://github.com/Azure/azure-functions-python-worker/issues/1567

      https://github.com/Azure/azure-functions-host/issues/8583