dotnet clr:
Hi Rahul ,
Following are my views regarding the issue that you are facing :
1. When you say one of the thread , can you just switch on some tracing
and check the thread Id , just to ensure that it arbitrary happens with
any thread or the same thread creates an issue , which is actually quite
unlikely , but you can use Appdomain class for this purpose .
2. Whether it's managed / unmanaged code ultimately all the API's that
do the thread creation under the cover access win 32 code , as threads
are exclusively OS privileges and believe me if at any point of time
depending upon the scehduling dynamics MS also warns about some such
issues in case of multithreaded Apps .
3. Actually in managed code when ever thread abortion takes place then
it's bound to throw the ThreadAbortException , but in you case since
it's not happening , only reason i can think of is , threading
implemented in un managed code and it is calling a managed method , so
each executing worker thread has a call to the method and exception at
point on call stack doesn't ensures going to your method's finally block
, otherwise exception with in the method can't miss it .
I think you can try :
Increasing some more sleep time / thread , to see if issue gets resolved .
Ensure that every thread created is of normal priority there's no high
priority thread , but starving others , which can done in unmanaged code .
Hope this somewhat helps ,
Mrinal
[quoted text, click to view] Rahul Anand wrote:
> I have a problem in my multithreaded application. The problem is reproducible
> very rarely, only once in 6000 similar execution (through same code path).
>
> Problem: One of multiple threads gets abnormally terminated. It does not go
> through the try-catch-finally sequence. The mehtod gets hanged and the object
> used inside the method gets collected by GC.
>
> Background about the Application: I am using COM-Interop. The threads are
> created in unmanaged code which calls methods from managed code. One of the
> thread abnormally terminates when executing .NET method.
>
> Observation: I have observed that the thread terminates only in those
> methods where I am using IO Stream operation.
>
> As I said this is very rare and the same code works fine except once or
> twice in 6000 executions.
>
> This is the code under which thread terminates mostly.
>
> <CODE>
>
> // I am generating log file for comments, to see the actual execution path
> public string GetRequestFile(string strFileName)
> {
> // Log method enterd
> StreamReader reader = null;
> try
> {
> reader = new StreamReader(strFileName);
> return reader.ReadToEnd();
> }
> catch(Exception exc)
> {
> // Log error occurred
> throw;
> }
> finally
> {
> if(reader != null)
> reader.Close();
> // Log finally executed
> }
> }
>
> <CODE>
>
> Please let me know if there is any similar bug reported under .NET I/O
> Stream operation.
>
> Does CLR forcefully terminates a thread in a way that it bypasses the
> finally block?
>
> Thanks in advance for your help.
>
> Cheers,
Hi Mrinal,
Thanks for your help.
But still I have not found any solution to the problem.
I have tested the same *method* where thread gets terminated abnormly in a
pure managed environment that is without the unmanaged COM-InterOP and it
works fine.
I have stress tested the method with 50 threads and 3000 iteration each. It
does not cause any termination of thread.
In my case all threads are created with normal priority.
I have also read that WinAPI TerminateThread causes a thread to abnormally
terminate, and in this case the thread also does not honor the
try-catch-finally execution.
Does windows calls the TerminateThread API (When using COM-InterOP) in any
situation?
And also I am not able to understand why termination of thread occurs during
stream I/O operation obly?
My concern is to prevent such situation as this can cause a user to wait for
some output which the executing thread (in case if it gets terminated) is
responsible to return.
thanks,
Rahul Anand
[quoted text, click to view] "Mrinal Kamboj" wrote:
> Hi Rahul ,
>
> Following are my views regarding the issue that you are facing :
>
> 1. When you say one of the thread , can you just switch on some tracing
> and check the thread Id , just to ensure that it arbitrary happens with
> any thread or the same thread creates an issue , which is actually quite
> unlikely , but you can use Appdomain class for this purpose .
>
> 2. Whether it's managed / unmanaged code ultimately all the API's that
> do the thread creation under the cover access win 32 code , as threads
> are exclusively OS privileges and believe me if at any point of time
> depending upon the scehduling dynamics MS also warns about some such
> issues in case of multithreaded Apps .
>
> 3. Actually in managed code when ever thread abortion takes place then
> it's bound to throw the ThreadAbortException , but in you case since
> it's not happening , only reason i can think of is , threading
> implemented in un managed code and it is calling a managed method , so
> each executing worker thread has a call to the method and exception at
> point on call stack doesn't ensures going to your method's finally block
> , otherwise exception with in the method can't miss it .
>
> I think you can try :
>
> Increasing some more sleep time / thread , to see if issue gets resolved .
>
> Ensure that every thread created is of normal priority there's no high
> priority thread , but starving others , which can done in unmanaged code .
>
> Hope this somewhat helps ,
>
> Mrinal
>
>
>
> Rahul Anand wrote:
>
> > I have a problem in my multithreaded application. The problem is reproducible
> > very rarely, only once in 6000 similar execution (through same code path).
> >
> > Problem: One of multiple threads gets abnormally terminated. It does not go
> > through the try-catch-finally sequence. The mehtod gets hanged and the object
> > used inside the method gets collected by GC.
> >
> > Background about the Application: I am using COM-Interop. The threads are
> > created in unmanaged code which calls methods from managed code. One of the
> > thread abnormally terminates when executing .NET method.
> >
> > Observation: I have observed that the thread terminates only in those
> > methods where I am using IO Stream operation.
> >
> > As I said this is very rare and the same code works fine except once or
> > twice in 6000 executions.
> >
> > This is the code under which thread terminates mostly.
> >
> > <CODE>
> >
> > // I am generating log file for comments, to see the actual execution path
> > public string GetRequestFile(string strFileName)
> > {
> > // Log method enterd
> > StreamReader reader = null;
> > try
> > {
> > reader = new StreamReader(strFileName);
> > return reader.ReadToEnd();
> > }
> > catch(Exception exc)
> > {
> > // Log error occurred
> > throw;
> > }
> > finally
> > {
> > if(reader != null)
> > reader.Close();
> > // Log finally executed
> > }
> > }
> >
> > <CODE>
> >
> > Please let me know if there is any similar bug reported under .NET I/O
> > Stream operation.
> >
> > Does CLR forcefully terminates a thread in a way that it bypasses the
> > finally block?
> >
> > Thanks in advance for your help.
> >
> > Cheers,
> > Rahul Anand
Interop code and/or the unmanaged component you're trying to call
might be raising some native exception that CLR doesn't catch.
You should try running the app under an unmanaged debugger like
windbg and check if there are any suspicious exceptions .
You can also set a breakpoint on kernel32!TerminateThread to see
if anybody is calling it. Windows itself doesn't normally use this API
since it's an extremely dangerous thing to do but external components
might be calling it (which is usually a bug).
--
This posting is provided "AS IS" with no warranties, and confers no
rights.
[quoted text, click to view] "Rahul Anand" wrote:
> I have tested the same *method* where thread gets terminated abnormly in a
> pure managed environment that is without the unmanaged COM-InterOP and it
> works fine.
> I have stress tested the method with 50 threads and 3000 iteration each.
> It
> does not cause any termination of thread.
>
> In my case all threads are created with normal priority.
>
> I have also read that WinAPI TerminateThread causes a thread to abnormally
> terminate, and in this case the thread also does not honor the
> try-catch-finally execution.
>
> Does windows calls the TerminateThread API (When using COM-InterOP) in any
> situation?
>
> And also I am not able to understand why termination of thread occurs
> during
> stream I/O operation obly?
>
> My concern is to prevent such situation as this can cause a user to wait
> for
> some output which the executing thread (in case if it gets terminated) is
> responsible to return.
Don't see what you're looking for? Try a search.