File.open freezes and freezes a thread while accessing a local file

I am currently using filestreams to copy files from one place to another. All this worked as intended until now, when I suddenly had a problem that File.open was freezing the thread in which it was running.

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

This only happens for 1 specific file (3 GB in size). Interestingly, one day before it functioned normally, although the file size may not be the case for this file. The next thing I checked was that some kind of exception was made that I wonโ€™t catch.

I put the try / catch block as a whole (usually I use the calling method to throw exceptions) and the 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 already accessed. Then an exception is thrown (checked for other files, as I said for this particular file, it always hangs in the stream when I try to open it).

The file is located on the local hard drive, and other files (although smaller) in the same folder do not show this problem.

As I am now running out of ideas, what is the possible reason, my question is: What can be the reasons for this unexpected behavior and how can they be advertised?

EDIT: Now it functions again (only when I tried to use the process monitor did it start functioning again). Thus, in general, there is no reason whatsoever for this phenomenon. If anyone had an idea of โ€‹โ€‹what might be the possible cause of this, it would be good to know in order to avoid a possible recurrence of the problem in the future.


Also notice how one question brought it to the File.Open file. I have a using block:

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

What I use to create some hash calculations regarding a file. It had no problems at all with opening the file (only later File.Open had problems)

Edit: I just received information from system administrators who are shining a new light on the problem: The system is configured in such a way that the whole system will be archived from file from time to time if the OS did not know about it. This means that in the case of a backup file, which the OS believes that it exists, and no one accesses it, when in fact it is currently subject to backup (and thus access to it cannot be accessed from the inside OS in accordance with the description of the backup process ..... since the OS does not know about the origin of the backup, nothing was shown in accessing the resources of the hard disk or task manager). Thus, with this information it may happen that, since the OS does not know about the file it is accessing, it tried to access it (via the open command) and waited and waited and waited for the read header of the hard disk to go to the file which was never the same as in reality it was inaccessible). Thus, he would have to start a timeout that does not have the file.open command (at least my hunch is there with new information, if I understood the system administrators correctly)

Tnx

+6
source share
3 answers

A couple of possible reasons:

  • Your antivirus. This thing captures the OS and replaces the I / O functions with its own. When you open a file, it can actually perform a virus scan before returning control to your application. Perhaps you had a bad signature update that caused AV to check your 3GB file, and a subsequent update might fix the problem.

  • Bad sector on your disk. Usually this leads to the fact that the I / O operations are performed very poorly, but your system could move the bad sector to another, so the performance returned to normal. You can run chkdsk /R to see if you have bad sectors.

  • Another application that locks the file, although I would rather expect an exception in this case.

+1
source

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

Given the absence of exceptions, it seems that the problem may be at a lower level. When you experienced this, did you try to open the file with a hex editor or some other tool to verify that it is actually fully readable? This may be a problem accessing a specific area of โ€‹โ€‹the hard drive.

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

See also this post for actual BufferedStream utility.

0
source

Do you check the FileAccess and FileShare values โ€‹โ€‹with the File.Open () function,

I think the file lock problem

0
source

Source: https://habr.com/ru/post/970989/


All Articles