0

I've written a .Net Web API which takes it's inputs, parses them and then stores an XML file on a network share linked to our server. I have also built a Windows service which scans the network share for new files to process our business logic.

This works nearly 100% of the time, but very occasionally (1 in 20,000 times) IIS6 holds a lock on the file it creates and won't clear until IIS is restarted. The locked files are always 0 bytes.

Currently I have a file which has been locked for nearly 20 hours! Here is the code that creates the file:

    Try
        '-- Make sure the file doesn't already exist
        TempFileName = strFullFileName
        i = 1

        While IO.File.Exists(TempFileName)
            TempFileName = strFullFileName.Replace(".xml", "_" & i & ".xml")

            i += 1
        End While

        strFullFileName = TempFileName

        '-- Deserialise the message into a file
        drSerializer = New XmlSerializer(DetailsOfMsg.GetType)
        FS = New FileStream(strFullFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None)
        XW = XmlWriter.Create(FS)
        drSerializer.Serialize(XW, DetailsOfMsg)

    Finally
        Try : XW.Flush() : Catch : End Try
        Try : FS.Close() : Catch : End Try
        Try : XW.Close() : Catch : End Try
        FS = Nothing
        XW = Nothing
    End Try

Why is IIS still holding a lock?

aaroncatlin
  • 3,203
  • 1
  • 16
  • 27

3 Answers3

3

Did you try to wrap the code within "Using" blocks? This ensures that types of FileStream and XmlWriter get disposed once the block's scope ends.

  • 1
    Thanks for the answer. I've added the Using blocks to my code but this won't go into our production environment for a few days so i won't be able to confirm that it solves the issue right away. – aaroncatlin Jan 29 '15 at 10:27
  • Well obviously I have tested the code before it goes into production. If you had read my post you'd know that the problem only occurs in production and only very rarely, so I cannot replicate the issue in our Stage environment. Therefore the only way to know the problem is solved is to monitor it in our production environment. – aaroncatlin Feb 02 '15 at 10:29
  • This seems to have fixed the problem, well at least for today's traffic. I'll continue to monitor, but have marked this as the Answer. Thanks. – aaroncatlin Feb 03 '15 at 14:10
  • Sadly the problem does still occasionally occur. I'm going to attribute this to nearly-end-of-life Windows Server 2003 and IIS 6 running on VMWare. We're due to upgrade our OS this year, so hopefully that will resolve the issue for good. – aaroncatlin Feb 28 '15 at 11:26
1

I think you need to separate this process. First create file on say folder X. Once created then move this file from folder X to shared location as there is watcher associated with this network share. Also, once file found then pick it and move to working folder and then start your business process on that file. 0 byte may be indicator of write and watch deadlock.

Amit
  • 882
  • 6
  • 13
0

I can see that, you have created FileStream instance with FileShare = none, whereas your requirement says that, you need simultaneously read and write on shared location.

Correct code would be

FS = New FileStream(strFullFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)

FileShare - A constant that determines how the file will be shared by processes.

For more info - refer this - https://msdn.microsoft.com/en-us/library/5h0z48dh(v=vs.110).aspx

EDIT

From comment, I found that you need lock to be applied for Read operation, and the error you are getting ( not so frequently) might be because of write lock. To avoid this, you can use following.

FS = New FileStream(strFullFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Write)

FileShare.Write - Allows subsequent opening of the file for writing. If this flag is not specified, any request to open the file for writing (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.

Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
  • I do not want simultaneous read and write on the file. The file should remain locked until it is written and the Windows Service will wait until it's no longer locked in order to process it. Otherwise important information may not be written to the file when it is processed. – aaroncatlin Jan 30 '15 at 14:32
  • With this , it won't get locked for read ops , while writing is done, you can search for other values for `FileShare` , there is one more `Read` value. – Arindam Nayak Jan 30 '15 at 17:13
  • My point is that I want to keep the file locked until it is completely written to, otherwise the Windows Service will move it into a Failed folder. I'm not sure you've understood my question. – aaroncatlin Jan 30 '15 at 17:28