Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blue screen when using Ping

I'm running into the bug where it BSODon ending debugging in the middle of a ping.

I have a few ways to disable it in my (wpf) application (where I ping continuously), but sometimes I forget to do so and BSOD.

I'd like to get around that say by changing a global AllowRealPinging variable and sleeping for 2 seconds in a callback before exiting the debugger so I don't BSOD.

like image 958
Roman A. Taycher Avatar asked Jul 19 '13 23:07

Roman A. Taycher


2 Answers

This is a known bug in Windows 7, you'll get a BSOD with bug-check code 0x76, PROCESS_HAS_LOCKED_PAGES in tcpip.sys when you terminate the process. The most relevant feedback article is here. Also covered in this SO question. No great answers there, the only known workaround is to fallback to a .NET version earlier than 4.0, it uses another winapi function that doesn't trigger the driver bug.

Avoiding pinging while you debug is certainly the best way to avoid this problem. Your desired approach is not going to work, your program is entirely frozen when it hits a breakpoint, kaboom when you stop debugging.

The simplest way is to just not starting pinging in the first place in the specific case of having a debugger attached. Use the System.Diagnostic.Debugger.IsAttached property to detect this in your code.

like image 162
Hans Passant Avatar answered Sep 27 '22 22:09

Hans Passant


This is a good way around:

private void GetPing(){

            Dictionary<string, string> tempDictionary = this.tempDictionary;  //Some adresses you want to test
            StringBuilder proxy = new StringBuilder();

            string roundTripTest = "";
            string location;
            int count = 0;  //Count is mainly there in case you don't get anything

            Process process = new Process{

                StartInfo = new ProcessStartInfo{
                    FileName = "ping.exe",
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    CreateNoWindow = true,

                }

            };

            for (int i = 0; i < tempDictionary.Count; i++){

                proxy.Append(tempDictionary.Keys.ElementAt(i));

                process.StartInfo.Arguments = proxy.ToString();

                do{
                    try{

                        roundTripTest = RoundTripCheck(process);

                    }
                    catch (Exception ex){

                        count++;

                    }

                    if (roundTripTest == null){

                        count++;

                    }

                    if (count == 10 || roundTripTest.Trim().Equals("")){

                        roundTripTest = "Server Unavailable";

                    }

                } while (roundTripTest == null || roundTripTest.Equals(" ") || roundTripTest.Equals(""));
            }

            process.Dispose();

        }

RoundTripCheck method, where the magic happens:

       private string RoundTripCheck(Process p){


            StringBuilder result = new StringBuilder();
            string returned = "";

            p.Start();

            while (!p.StandardOutput.EndOfStream){

                result.Append(p.StandardOutput.ReadLine());

                if (result.ToString().Contains("Average")){

                    returned = result.ToString().Substring(result.ToString().IndexOf("Average ="))
                                     .Replace("Average =", "").Trim().Replace("ms", "").ToString();
                    break;
                }


                result.Clear();

            }

            return returned;

        }

I had the same problem, this solves it!

like image 22
Brain Bytes Avatar answered Sep 27 '22 21:09

Brain Bytes