Having issues with Azure Function App, which gives error if not run for a week

Tasneem Nomani 40 Reputation points
2024-07-30T19:59:09.18+00:00

I have a azure function app.

This function is executed when a azure app service makes a request.

The azure function gets file from azure storage for processing.

If the request is not made for a week, the function app gives error

Object reference not set to an instance of an object

When I republish my azure function without making any code change, the function runs fine.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,118 questions
Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,228 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,927 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,913 questions
0 comments No comments
{count} votes

Accepted answer
  1. Konstantinos Passadis 19,251 Reputation points MVP
    2024-07-30T21:16:11.73+00:00

    Hello @Tasneem Nomani

    Welcome to Microsoft QnA!

    What type of Azure Functions are you using ?

    I can think a lot of issues causing this

    Most probale

    1. Cold Start Issues:

    • Explanation: Azure Functions can experience "cold starts" when they haven't been used for a while. This means the runtime environment needs to be spun up, which can take some time. During a cold start, dependencies might not be fully initialized, leading to null reference errors.
    • Mitigation:
      • Warm-up Triggers: Implement a recurring timer trigger or a scheduled HTTP ping to keep your function warm and prevent cold starts.
        • Premium Plan: Consider using the Azure Functions Premium plan, which offers features like pre-warmed instances to minimize cold start latency.

    2. Dependency Injection Problems:

    • Explanation: If your function relies on dependency injection to access external services (like Azure Storage), the connection might not be established or might time out after a period of inactivity.
    • Mitigation:
      • Retry Logic: Add retry logic around your dependency injection calls to handle transient connection failures.
      • Singleton Services: If appropriate, configure your dependencies as singletons to maintain the connection across function executions.
    • https://github.com/Azure/azure-functions-host/issues/8021#issuecomment-1004303750

    https://learn.microsoft.com/en-us/answers/questions/1409596/always-on-in-azure-function-apps-running-on-an-app

    --

    I hope this helps!

    Kindly mark the answer as Accepted and Upvote in case it helped!

    Regards

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. akinbade abiola 18,465 Reputation points
    2024-07-30T21:20:15.19+00:00

    Hello Tasneem Nomani,

    Thanks for your question.

    This is most likely as a result of cold start. When your function isn't invoked for an extended period resources are usually deallocated. So the first request usually takes longer while re-allocating.

    See: https://azure.microsoft.com/en-us/blog/understanding-serverless-cold-start/

    You can mark it 'Accept Answer' and 'Upvote' if this helped you

    Regards,

    Abiola

    1 person found this answer helpful.
    0 comments No comments

  2. Tasneem Nomani 40 Reputation points
    2024-08-01T14:29:54.7266667+00:00

    Thank you for the response.

    I have implemented lazy loading of services in startup.cs.

    I also created a timer trigger function to keep accessing the function every hour.

    Hopefully this will resolve my issue.

    0 comments No comments

  3. Tasneem Nomani 40 Reputation points
    2024-08-01T14:35:58.09+00:00

    The azure web app that is calling the function is also facing same issue.

    asp.net mvc web application hosted as azure app service, this uses forms authentication

    I can login to the site and browse.

    When I try to submit a form on the site it gives error

    "Error An error occurred while processing your request"

    Without making any changes to the code, if i republish the code to azure or restart the app service, the error is gone.

    I looked at the application event log in azure portal.

    it has this error 4005 Forms authentication failed for the request. Reason: The ticket supplied has expired.

    0 comments No comments

  4. Konstantinos Passadis 19,251 Reputation points MVP
    2024-08-04T13:08:16.6133333+00:00

    Hello @Tasneem Nomani

    We have to enable more logging to see deeper

    Can you do that ?

    Also enable Application Insights probably we will get something for both apps

    In the Web App also enable:

    User's image

    I hope this helps!

    Kindly mark the answer as Accepted and Upvote in case it helped!

    Regards


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.