Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File.open hangs and freezes thread when accessing a local file

Tags:

c#

file-io

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

like image 871
Thomas Avatar asked Jun 18 '14 07:06

Thomas


2 Answers

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.

like image 156
Lucas Trzesniewski Avatar answered Nov 17 '22 04:11

Lucas Trzesniewski


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.

like image 33
Thomas Avatar answered Nov 17 '22 03:11

Thomas