<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>NTDebugging Puzzler 0x00000006: Invalid Handle - can you handle it?</title><link>http://blogs.msdn.com/b/ntdebugging/archive/2008/05/19/ntdebugging-puzzler-0x00000006-invalid-handle-can-you-handle-it.aspx</link><description>Hi NTDebuggers, this week’s puzzler just so happens to match its number: 0x000000006 = ERROR_INVALID_HANDLE. That said, let me give you a scenario and the challenge will be to provide the best action plan to isolate the problem. This should include an</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: NTDebugging Puzzler 0x00000006: Invalid Handle - can you handle it?</title><link>http://blogs.msdn.com/b/ntdebugging/archive/2008/05/19/ntdebugging-puzzler-0x00000006-invalid-handle-can-you-handle-it.aspx#8550774</link><pubDate>Sun, 25 May 2008 11:21:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8550774</guid><dc:creator>Tal Rosen</dc:creator><description>&lt;p&gt;Run application verifier, and enable &amp;quot;handle tracing&amp;quot;. Then attach a debugger to the application, and reproduce the crash.&lt;/p&gt;
&lt;p&gt;App Verifier should raise a debug break when attempting to use the invalid handle (you can use Adplus to auto generate a full dump).&lt;/p&gt;
&lt;p&gt;If the handle is NULL or some strange value, it’s probably a memory corruption (needs memory breakpoints monitoring).&lt;/p&gt;
&lt;p&gt;If the handle value is reasonable, it’s probably a closed handle (closed by some other thread?), you can watch the handle trace call stacks (using !htrace) to figure out who closed our handle.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8550774" width="1" height="1"&gt;</description></item><item><title>re: NTDebugging Puzzler 0x00000006: Invalid Handle - can you handle it?</title><link>http://blogs.msdn.com/b/ntdebugging/archive/2008/05/19/ntdebugging-puzzler-0x00000006-invalid-handle-can-you-handle-it.aspx#8535710</link><pubDate>Fri, 23 May 2008 04:15:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8535710</guid><dc:creator>molotov</dc:creator><description>&lt;p&gt;Scenario 1:&lt;/p&gt;
&lt;p&gt;I suppose, I would have the customer download and install Application Verifier and the Debugging Tools for Windows. &amp;nbsp;I'd instruct the customer to set up the application in verifier for Basics -&amp;gt; Handles, and then have the customer launch the app in WinDbg and reproduce the problem. &amp;nbsp;If the problem was a &amp;quot;double close&amp;quot; (an attempt to close a handle twice), verifier would cause a break at the time of the second attempt to close the handle. &amp;nbsp;If the problem was that a function was presented with an invalid handle (say, WriteFile being passed a file handle that is not valid, or has already been closed), verifier would cause a break at the time of the attempted use of that handle. &amp;nbsp;Both situations can be caused by faulty / non-existent thread synchronization. &amp;nbsp;Another cause may be not setting the HANDLE to NULL after closing it, and then attempting to use the same HANDLE variable. Memory corruption could also cause the value held by a HANDLE to be invalid. &amp;nbsp;If memory corruption is suspected, I'd suggest also turning on full page heap (Basics -&amp;gt; Heaps) and running the app in WinDbg. &amp;nbsp;Anyways, once there is a verifier stop, the best case would probably be that the stack immediately points out what's wrong, but exploration with !htrace may be desirable once the HANDLE value is determined by looking at the parameters to whatever function is on the stack at the time of the stop, that is attempting to use an invalid handle (!htrace [handle value]).&lt;/p&gt;
&lt;p&gt;Scenario 2:&lt;/p&gt;
&lt;p&gt;I guess I'd do about the same as in scenario 1, for starters. &amp;nbsp;Once I have more info, I might see if I can replicate the problem in the debugger, and set breakpoints at various locations if looking at the code didn't turn up anything immediately obvious. &amp;nbsp;If the code just happened to have trace logging that covered activities relating to usage of the handles, all the better...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8535710" width="1" height="1"&gt;</description></item><item><title>re: NTDebugging Puzzler 0x00000006: Invalid Handle - can you handle it?</title><link>http://blogs.msdn.com/b/ntdebugging/archive/2008/05/19/ntdebugging-puzzler-0x00000006-invalid-handle-can-you-handle-it.aspx#8527417</link><pubDate>Wed, 21 May 2008 13:21:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8527417</guid><dc:creator>JM</dc:creator><description>&lt;p&gt;This isn't a puzzler, it's a quiz, so it's not half as stimulating. This would be an excellent interview question for those people you want to hire, though. :-)&lt;/p&gt;
&lt;p&gt;That said, 1. procmon, 2. appverifier/windbg. Details are left as an exercise.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8527417" width="1" height="1"&gt;</description></item><item><title>re: NTDebugging Puzzler 0x00000006: Invalid Handle - can you handle it?</title><link>http://blogs.msdn.com/b/ntdebugging/archive/2008/05/19/ntdebugging-puzzler-0x00000006-invalid-handle-can-you-handle-it.aspx#8526969</link><pubDate>Wed, 21 May 2008 10:54:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8526969</guid><dc:creator>Infro</dc:creator><description>&lt;p&gt;Most Likely Options:&lt;/p&gt;
&lt;p&gt;	#1.	Invalid Handle Returned but not checked for.	(I do this all the bleedin time, ahh bad coding practices when you make too many programs for yourself.)&lt;/p&gt;
&lt;p&gt;	#2.	Handle Closed and then used.	(Hurm, bad thread safety or very bad programming habits, (worse than mine!))&lt;/p&gt;
&lt;p&gt;	#3.	Handle is of wrong type.	(2nd most common for myself, WaitForMultipileObjects with a screw up somewhere)&lt;/p&gt;
&lt;p&gt;	#4.	Handle being released by some other method.	(Uncommon, and probably hardest to track down)&lt;/p&gt;
&lt;p&gt;Anyhow, we want to be able to narrow it down from these first off. To do that we know that we have unhandled exceptions and app crashing, first we'll back track.&lt;/p&gt;
&lt;p&gt;So #1 We'll want a record of events leading to the crash, as its an invalid handle error lets first use API Spy which is going to be very very handy in this situtation.&lt;/p&gt;
&lt;p&gt;We'll want to have it Looking for Creation, Loading, and Deletion of &lt;/p&gt;
&lt;p&gt;Process, Thread, Section/FileMapping, File, Event, Semaphore, Mutex, Timer, Pipe, Socket, GDI Objects, Resource Objects&lt;/p&gt;
&lt;p&gt;Also we'll want a Dump File with the crashed file Open.&lt;/p&gt;
&lt;p&gt;#1 Use API Spy and Mark the following to be Recorded, then open the application that crashes. (Assuming that the handle will get closed by this application, it can provide us with details as to when it was closed)&lt;/p&gt;
&lt;p&gt;DuplicateHandle&lt;/p&gt;
&lt;p&gt;WaitForMultipleObjects (Check Array Size)&lt;/p&gt;
&lt;p&gt;CloseHandle&lt;/p&gt;
&lt;p&gt;CloseObject&lt;/p&gt;
&lt;p&gt;CloseWindow&lt;/p&gt;
&lt;p&gt;DeleteObject&lt;/p&gt;
&lt;p&gt;DeleteTimerQueue&lt;/p&gt;
&lt;p&gt;DestroyWindow&lt;/p&gt;
&lt;p&gt;TerminateJobOject&lt;/p&gt;
&lt;p&gt;TerminateProcess&lt;/p&gt;
&lt;p&gt;TerminateThread&lt;/p&gt;
&lt;p&gt;Save the Log from API Spy and send it to .....&lt;/p&gt;
&lt;p&gt;#2 Use WinDBG or Adplus to get a dump, Adplus -crash or use WinDBG, open the process that is to crash, wait until it crashes, then run .dump /ma filename, and send the file to ......&lt;/p&gt;
&lt;p&gt;#3 Get better description of situtation (Does it crash when you do something specific? Random Crash? Repeatable? If Something Specific try and get more information)&lt;/p&gt;
&lt;p&gt;Now if I'm going to be activly debugging this with source in WinDBG I'd let it run until it crashes, while looking through/DebugOutputting the source for the CloseHandle-TerminateThread functions. DebugOutput would have a stack trace at each one of these functions as to see where/how this function was called, or at least get an idea. Once it crashes, try and find as much info about the handle that was invalid and how it came to be invalid (!Handle, !Object, Find if its valid, was valid, what not, what released it/modified the value).&lt;/p&gt;
&lt;p&gt;As this is getting a bit long I think that its pretty good, anyhow, looking forward to some more puzzlers, so long as I can find time ;).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8526969" width="1" height="1"&gt;</description></item><item><title>re: NTDebugging Puzzler 0x00000006: Invalid Handle - can you handle it?</title><link>http://blogs.msdn.com/b/ntdebugging/archive/2008/05/19/ntdebugging-puzzler-0x00000006-invalid-handle-can-you-handle-it.aspx#8520077</link><pubDate>Tue, 20 May 2008 02:47:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8520077</guid><dc:creator>Skywing</dc:creator><description>&lt;P&gt;I'd enable handle tracing in gflags and have the process be run under the debugger. &amp;nbsp;Take a dump when the invalid handle exception comes up.&lt;/P&gt;
&lt;P&gt;Then, use !htrace to find when the handle was opened/closed, and analyze from there along with the stack trace of where the exception occurred.&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8520077" width="1" height="1"&gt;</description></item></channel></rss>