question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Locking issue with DLLs in Functions

See original GitHub issue

I’m having issues of files locking within a Functions App, that is preventing Continuous Delivery. The issue presents itself when referencing a .NET DLL within the function, running a test, then attempting to deploy a new version of the referenced .NET DLL.

Here are the steps needed to replicate the issue:

  1. Create a simple class library in Visual Studio called “FunctionsTest.Calculation” and build.
using System;
using System.Text;
namespace FunctionsTest.Calculation
{
    public class SomeWork
    {
        public string Execute(string inputTest)
        {
            return String.Format("result {0}", inputTest);
        }
    }
}
  1. Create a new Functions App
  2. Create a new Manually triggered C# Function called “ManualTriggerCSharp1”, with the following code
#r "FunctionsTest.Calculation.dll"
using System;
using FunctionsTest.Calculation;

public static void Run(string input, TraceWriter log)
{
    log.Info($"C# manually triggered function called with input: {input}");
    var somework = new SomeWork();
    var result = somework.Execute(input);
    log.Info("result:" + result );
}
  1. Upload a copy of the generated DLL to the BIN directory of the Function App using FTP
  2. Run the function … all should execute correctly.
  3. Change the return parameter type of the method “Execute” within the local class library and rebuild the DLL.
using System;
using System.Text;

namespace FunctionsTest.Calculation
{
    public class SomeWork
    {
        public double Execute(string inputTest)
        {
            return 1f;
        }
    }
}
  1. Attempting to FTP the new DLL to Azure returns: Command: STOR FunctionsTest.Calculation.dll Response: 550 The process cannot access the file because it is being used by another process. Wait a while, then try again … wait again… do this a few few times and you should get the same error.
  2. Eventually, when you get tired of waiting, rename your locally generated DLL file to something else for example “FunctionsTest.Calculation1.dll” and FTP that to the server. This should work fine, so you now have two DLLs in the BIN directory.
  3. Update the reference in the Function script to point to the new DLL (note line: **#r “FunctionsTest.Calculation1.dll” ** but continue to use old namespace using FunctionsTest.Calculation).
#r "FunctionsTest.Calculation1.dll"
using System;
using FunctionsTest.Calculation;

public static void Run(string input, TraceWriter log)
{
    log.Info($"C# manually triggered function called with input: {input}");
    var somework = new SomeWork();
    var result = somework.Execute(input);
    log.Info("result:" + result );
}
  1. Run the script again and you get the error, showing the function is still working against the old DLL reference, even after modifying the code:

Exception while executing function: Functions.ManualTriggerCSharp1. mscorlib: Exception has been thrown by the target of an invocation. Method not found: ‘Double FunctionsTest.Calculation.SomeWork.Execute(System.String)’.

  1. Change the namespace in Visual Studio of the local DLL to “FunctionsTest.Calculation1”, rebuild and FTP to the server under the filename “FunctionsTest.Calculation1.dll”.
  2. Change the Function to use the new namespace (note line: using FunctionsTest.Calculation1) and success, we’re up and running again.
#r "FunctionsTest.Calculation1.dll"
using System;
using FunctionsTest.Calculation1;
public static void Run(string input, TraceWriter log)
{
    log.Info($"C# manually triggered function called with input: {input}");
    var somework = new SomeWork();
    var result = somework.Execute(input);
    log.Info("result:" + result );
}

This is perhaps a edge case; however it does seem to indicate that Functions are holding on to references to external resources, even after a shutdown/restart or recompile. With CI we are updating these DLLs continually and it’s a drawback that we can’t simply update the files.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
paulbatumcommented, Sep 6, 2018

Just a short note for anyone running into problems discussed in this issue. In Functions V2 there is no shadowcopy behavior (because it does not exist in .NET core) and so running into locking issues is more likely. However we have implemented support for the “take app offline” feature that is provided through msdeploy. To use this feature from VS, when you’re publishing look for the “take app offline” checkbox. If you can’t see it, you can specify the setting manually by going to your .pubxml file for your publish profile and make sure you have this setting:

<EnableMsDeployAppOffline>True</EnableMsDeployAppOffline>
2reactions
davidebbocommented, Nov 29, 2016

When using msdeploy, can you try setting MSDEPLOY_RENAME_LOCKED_FILES=1 in your Azure Appsettings? This enables a mode that should allow it to deal with loaded assemblies.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Avoiding deployment locking errors by running Web and ...
We've had ongoing issues when deploying web and functions apps involving the locking of DLLs during the deployment.
Read more >
c++ - Multithreaded dll and lock whan calling function from ...
1 Answer. A mutex possibly. It is possible that the program holds a mutex while running into the dll. And the GetSymbol tries...
Read more >
Dynamic-Link Library Best Practices - Win32 apps
Call ExitThread. Exiting a thread during DLL detach can cause the loader lock to be acquired again, causing a deadlock or a crash....
Read more >
multithreading locking on dll
If I understood the question, you are trying to implement the locking in C# code. To do this you can use a Mutex[^]...
Read more >
How to Rectify the DLL Locked Issue during UCCE MR ...
Click Find and navigate to Handle or DLL. Enter the locked DLL name and search. This shows you the process name that uses...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found