Search code examples
azureazure-functionsqueuetrigger

Azure function App - Global variable shared across executions and old code running intermittently


Currently, I am facing two issues in the Azure function app. I have provided the details below:

1. Global variable content is being shared across executions: I have used Concurrent dictionary which is a global variable, private and static. This variable is being used in the Queue Trigger.

private static readonly ConcurrentDictionary<string, string> s_mapping = new ConcurrentDictionary<string, string>()

Intermittently, I see that the above variable is being shared across different Queue trigger executions. How can I rectify it, so that the variables are not shared across different run instances?

2. Old code running after publish through Visual studio: I publish the code using visual studio, intermittently I see that the old code is running. In the configuration, WEBSITE_RUN_FROM_PACKAGE is set as 1 and also I deploy it as a zip file from VS. I tried restarting the Function App but it doesn't seem to work.

Really appreciate the help here or any guidance on what can be done for these issues.


Solution

  • Each function app runs in its own process and all functions run in that process. Static variables are shared across all functions in that application just as if you'd written an ASP.NET or console app. For example, you can create an HttpClient in a static class and all functions can access that same client instance (we encourage this, in fact). It's always been this way, so nothing changed there.

    Source: https://github.com/Azure/Azure-Functions/issues/1202

    1-) don't use static variables or split into different azure function apps.

    2-) try removing files from wwwroot before publishing new code. This should not happen, but it's possible in case of high workloads (code being processed while you're publishing new code). I believe the best you can do is to proper setup a cleaning step before pushing changes.