BUG that cannot be locked, which is a BUG that cannot be easily solved (XX is being used by another process, so this process cannot access this file)

Source: Internet
Author: User

Occasionally, I will do the following things for the running autumn Garden site:

1: view the website Event Log (check whether there are any exceptions not found on the website, if any ).
2: view the captured exception log (see what causes it, if any ).
3: View abnormal logs of database execution statements (see what happens ).

I read the log earlier today. I am familiar with a log that has never been processed. I suddenly thought about how to deal with it:

Log: http://www.cyqdata.com/search/cnblogs/finger+print------------------------Error On: 5:30:18 [WriteException]: file "D: \ * \ TableSchema \ CYQ. data. tableSchema_ SQL .qblog.Blog_User.ts "is being used by another process. Therefore, this process cannot access this file.: In System. IO. _ Error. winIOError (Int32 errorCode, String maybeFullPath) in System. IO. fileStream. init (String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) in System. IO. fileStream .. ctor (String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) in System. IO. streamWriter. createFile (String path, Boolean append) in System. IO. streamWriter .. ctor (String path, Boolean append, Encoding encoding, Int32 bufferSize) in System. IO. streamWriter .. ctor (String path, Boolean append) in CYQ. data. tool. IOHelper. save (String fileName, String text, Boolean isAppend) in System. IO. _ Error. winIOError (Int32 errorCode, String maybeFullPath) in System. IO. fileStream. init (String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) in System. IO. fileStream .. ctor (String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) in System. IO. streamWriter. createFile (String path, Boolean append) in System. IO. streamWriter .. ctor (String path, Boolean append, Encoding encoding, Int32 bufferSize) in System. IO. streamWriter .. ctor (String path, Boolean append) in CYQ. data. tool. IOHelper. save (String fileName, String text, Boolean isAppend)

 

A small explanation of this error:

This error is actually caused by adding try catch to the IO Write File Operation, and then recording it. According to the intuitive error prompt:
Since it is a process error, the ideal assumption is that another process is generated when the application pool is being recycled, and the exception caused by writing a file at the same time as another unfinished process, recorded.

I thought about a multi-process concurrency solution by increasing the mutex between processes:

Static System. Threading. Semaphore _ mutex = new System. Threading. Semaphore (1, 1, "IOHelper. Save ");
Private static bool Save (string fileName, string text, bool isAppend)
{
Try
{
If (_ mutex. WaitOne (2000, false) // process synchronization.
{
Using (StreamWriter writer = new StreamWriter (fileName, isAppend ))
{
Writer. Write (text );
}
}
Return true;
}
Catch (Exception err)
{
Log. WriteLogToTxt (err );
}
Finally
{
Try
{
_ Mutex. Release ();
}
Catch
{

}
}
Return false;
}

After writing the code, I created a project, wrote a Demo, and tested the concurrency of multiple processes. The code is very simple. Click a button, open a thread, and run the following code:

String path = AppDomain. CurrentDomain. BaseDirectory + "a. ts ";
While (true)
{
IOHelper. Save (path, "test", true );
}

 

When a thread is started, an endless loop keeps writing files.

After running the command, I checked whether the file was created normally. So I opened the file and checked whether the log was written. I found that the file was written normally, and then I returned to the code.

Then I opened another software and ran it again. I triggered a magic BUG like this:

Click it to throw an exception:

The file "F: \ * \ bin \ Debug \ a. ts" is being used by another process. Therefore, this process cannot access this file. "

 

After the magic BUG is triggered, there is no solution:

1: No matter how I write code, only single thread, Lock, dual Lock, Mutex, or hybrid use, this exception is thrown after several seconds of running.
2: Then I added the Thread sleep and found that it is normal to be added to the Thread. Slee (100) or lower. This is normal and I suspect that the file is closed and there is a latency function?
3: Through Reflect to view the source code, I checked several base classes and didn't see any special ones... I wonder if the problem persists...
3: After that, I wrote some similar content in the flash memory... It's a bit confusing.
4: a few hours later, I gave up. Your brother-in-law won't do it .. There is no such thing as magic.
5: I think there are too many things open, and I will turn them off, including folders.
6: The nature of writing code. If the problem is not solved, I will go back and try again.

The problem is missing ????

This operation is normal, and no error is reported for the number of concurrent operations... What's wrong with your sister ????
So I went to see if there was any problem with the created file ....
Return to the left-side navigation pane .. Your brother-in-law has thrown another exception...

I suddenly realized something. After several rounds of tossing exceptions and not throwing exceptions, I finally found this god-like BUG:

It turns out that when the file is viewed, it is equal to the reason why the file is selected by the mouse focus. If the file is in forocus state, the exception is thrown when the file is frequently written.

This BUG cannot be solved. Your sister:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.