8

I'm currently using filestreams to copy files form one location to another. It all functioned as intended until now when I suddenly have the problemn that File.open freezes the thread that it is running in.

FileStream sourceStream = File.Open(filePath, FileMode.Open)

It only happens for 1 specific file (3 GB in size). The interesting thing is one day prior it functioned normally though for this file so it can't be the file size. Next thing I checked was if some sort of exception was thrown that I don't catch.

I put a try / catch block the whole thing (normally I use the calling method to catch the exceptions) and still same effect.

            try
            {
                FileStream sourceStream = File.Open(filePath, FileMode.Open);
                sourceStream.Close();
            }
            catch (Exception e)
            {
                Console.Write("A");
            }

I also checked what happens if the file is being accessed already. Then an exception is thrown (tested it for other files as like I said for this specific file it always hangs up the thread now when I try to open it).

The file is located on the local harddrive and other files (smaller though) in the same folder don't show this problem.

As I'm now running out of ideas what the possible reason could be, my question is: What could possible reasons for this unexpected behaviour be and how can they be adverted?

EDIT: It now functions again (just when I tried to use the process monitor it started functioning again). So in total no clue what could have caused the phenomenon. If anyone would have an idea what could be a possible reason for this it would be good to know to avoid a possible repeat of the problem in the future.


Also of note as one question brought it up before the File.Open I have an using block with:

using (var stream = new BufferedStream(File.OpenRead(filePath), 1024 * 1024))
{
   //..do calculations
}

Which I use to make some hash calculations in regards to the file. THIS one had no issues at all with opening the file (only the later File.Open had the issues)

Edit: I've just received an info from the sysadmins here that shines a new light onto the problem: The system is set up in a way so that the whole system is backuped time and again file by file wihtout the OS having any knowledge of it. This means in the case of the backuped file that the OS thinks it is there and nobody accesses it when in reality it is currently being backuped (and thus accessed and unable to be accessed from within the OS according to how they described the backup process.....as the OS doesn't know about the backup happening nothing was shown in the resources hard drive access nor the task manager). Thus with that information it could be that as the OS didnt know about the file being accessed it tried to access it (through the open command) and waited and waited and waited for the hard drive read head to come to the file which never happened as it was not accessible in reality). Thus it would have had to run into a timeout which the file.open command doesn't have (at least my guess there with the new infos if I understood the sys admins accurately there)

tnx

Thomas
  • 2,886
  • 3
  • 34
  • 78
  • 1
    This is not possible :), but indeed you can use [Process Monitor](http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx) to see what really happens to this file while it is being opened. – Alexei Levenkov Jun 18 '14 at 07:55
  • Have you considered using a [BufferedStream](http://msdn.microsoft.com/en-us/library/system.io.bufferedstream.aspx)? – John Willemse Jun 18 '14 at 07:57
  • I also had thought it was impossible until it happened. An exception I could have understood if I missed a try/catch or if it hadn't function to begin with (would also be strange but at least not first it functions next day that problem :) ). will take a look at the process monitor tnx – Thomas Jun 18 '14 at 07:58
  • @john: bufferedstream still needs File.Open if I'm not mistaken? (and the problem is the File.Open itself hangs up already) – Thomas Jun 18 '14 at 08:04
  • Hmm okay. I misunderstood then. – John Willemse Jun 18 '14 at 08:08
  • Was your PC running scheduled tasks that accessed the HDD, e.g. scheduled defrag or system maintenance (in Windows 7 and above, at least), did you have a media player open accessing the disk? Any number of things could have caused a slow open, especially if the HDD was busy at the time. – Sean Airey Jun 18 '14 at 08:32
  • nope nothing of that. also what is of possible note Before the File.Open where the freeze happens I use "new BufferedStream(File.OpenRead" (which I close again before the File.Open later) to calculate a hash value of the file. THAT command functioned without troubles the whole time. Also there was no delay when I opened programs,... during that time (which would have been if the HDD had been busy) – Thomas Jun 18 '14 at 08:49
  • If you reduce the size of the file, for testing, down to 1GiB; do you still have issues with the file? If you monitored it in the task manager, does memory usage seem high? – Justin Jun 26 '14 at 13:23
  • The problem had suddenly disappeared a while ago and never came back, but as it stands it was just taht one file (no other and the file functioned before the problem suddenly appeared and then after the problem disappeared again it functioned without problems again). Memory usage was also not extraordinary (had a few GB ram left still, also nothing unusual to be seen on the resource manager for the hard drive ) – Thomas Jun 26 '14 at 13:34
  • You'll find a good example of such an *environmental* problem in [this answer](http://stackoverflow.com/a/24099616/17034). – Hans Passant Jun 28 '14 at 15:24
  • Edited the original question as I just received new informations from our sys administration (wish I had heard that info earlier) – Thomas Jun 30 '14 at 07:10
  • Have you tried using [Handle](http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx) -- aka FileHandles -- from SysInternals? – David Jul 01 '14 at 19:16
  • No didnt but just read through it. Hmmm would it make any difference in this case? as the System itself does not know that the file is in reality open (it only knows that it takes an eternity to put the file reading head of the HDD on the appropriate Position. that is all the System knows in this case [according to what the sysadmins told me]) – Thomas Jul 02 '14 at 06:41
  • In total it Looks like this to me: The System does not know taht the file is in reality not accessible and can't find out itself. file.open hangs thus as it has no timeout (as naturally no exception occurs as the System thinks all is ok). So question would be if there is any alternative to open files where I can have/set a timeout? – Thomas Jul 02 '14 at 06:50

5 Answers5

3

A couple possible reasons:

  • Your antivirus. That thing hooks into the OS and replaces the I/O functions with its own. When you open a file, it can actually perform a virus check before returning back control to your application. You could have had a bad signature update which forced the AV to perform the check on your 3GB file, and a subsequent update could have fixed the problem.

  • A bad sector on your drive. This usually makes I/O perform very poorly, but your system could have relocated the bad sector into another one, so the performance went back to normal. You can run a chkdsk /R to see if you have bad sectors.

  • Another app that locks the file, though I'd rather expect an exception in this case.

Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
1

The Problem stemmed not from c# or the Windows System, but from the architecture of how the PC was set up itself.

In this case it was set up so, that the files I tried to read could be inacessible (because they were being backed up) WITHOUT the OS of the local PC knowing it.

Thus the OS thought the file was accessible and C# received that answer from the OS when it tried to open the file. And as file operations in C# use their Windows aequivalents and those have no timeouts.... the whole Operation hanged / freezed until the file backup was finished.

In retrospect I would say: Lucas Trzesniewski answer should cover most situations where the freeze happens....my own Problem was not answerd by that only because I had such a Special Situation that caused the Problem in the end.

Thomas
  • 2,886
  • 3
  • 34
  • 78
0

Are you absolutely sure that the freezing always occurs in File.Open()?

Given the absence of exceptions it appears that the problem may be at lower level. When you've experienced it you tried to open the file with a hex editor or some other tool to check that it is actually entirely readable? It could be a problem of access to a certain area of ​​the hard drive.

Try to specify the access mode with FileAccess if you need read-only, write-only, etc.

See also this post for the actual usefulness of BufferedStream.

Community
  • 1
  • 1
Marco Sacchi
  • 712
  • 6
  • 21
  • for the file.open: Yes 100% sure (I tried it a few times with the debugger running to make sure I know WHERE exactly the thread hangs up and where it happens). I have share.read in the file-open. Currently I can't test it as it didn't happen again (although with the new info from the sys admins it is quite possible that a backup process just locked the file without the OS knowing it and the OS and application waited for the file handler (which they saw as unused) to open the file and had no timeout). – Thomas Jun 30 '14 at 07:13
0

Have you check with File.Open() function with FileAccess & FileShare values ,

I think it's a file locking issue

Ajay Peter
  • 153
  • 4
  • Yepp checked that. Was one of the first things and also made a few tries what happens if the file is read or writelocked during the time I try to file.open. In all of these cases I get an exception. In the strange occurances described above I get no exception it just hangs for a few hours until the phenomenon ceases and then it all works again (so sadly no locking Issue I'm able to refabricate) – Thomas Aug 13 '14 at 05:29
  • Can you please put values after following code System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); try { Console.WriteLine(watch.ElapsedTicks); FileStream sourceStream = File.Open(filePath, FileMode.Open); Console.WriteLine(watch.ElapsedTicks); sourceStream.Close(); Console.WriteLine(watch.ElapsedTicks); } catch (Exception e) { Console.Write("A"); } – Ajay Peter Aug 13 '14 at 05:49
0

I had a similar issue when sometimes File.Open hangs when trying to check if a file is locked.

I solved it like this:

public async Task<bool> IsLocked(FileInfo file)
{
    var checkTask = Task.Run(() =>
    {
        try
        {
            using (file.Open(FileMode.Open, FileAccess.Read, FileShare.None)) { };
            return false;
        }
        catch (Exception)
        {
            return true;
        }
    });
    var delayTask = Task.Delay(1000);
    var firstTask = await Task.WhenAny(checkTask, delayTask);
    if (firstTask == delayTask)
    {
        return true;
    }
    else
    {
        return await checkTask;
    }
}
Niels Bosma
  • 11,758
  • 29
  • 89
  • 148