Locks in an ExceptHandler

delphi package - automated exception handling
Post Reply
obones
Posts: 66
Joined: Fri May 15, 2009 11:47 am

Locks in an ExceptHandler

Post by obones »

In order to have additional information in my service bug reports, I register an exception handler like this:

Code: Select all

 RegisterExceptionHandler(AddDetailsExceptHandler, stDontSync, epCompleteReport);
And the AddDetailsExceptHandler simply puts text in a new bug report section like this:

Code: Select all

exceptIntf.BugReportSections['sectionName'] := SomeText
This works fine, I get the details I'm looking for.

Under some yet to be understood circumstances, the entire service becomes unresponsive and so I used the GetProcessBugReport method as described here: viewtopic.php?f=4&t=28692
This gave me an entire snapshot of the frozen process and I have noticed that I have one thread that has AddDetailsExceptHandler inside its stack trace from calling this method:

Code: Select all

function GetFullBugReport(E: Exception): string;
var
  MEException: IMEException;
  Handled: Boolean;
begin
  MEException := NewException(etNormal, E);
  Handled := False;
  MEException.FireHandlers(epCompleteReport, Handled);
  Result := MEException.BugReport;
end;
The AddDetailsExceptHandler method tries to do a complete service state dump, which means it calls property getters on various objects, one of which issues TMonitor.Enter(Self) as it is required for regular work to protect its internals.
As it turns out, in the retrieved dump, there clearly is another thread that is currently holding the monitor while waiting for another object that is already held by the code calling GetFullBugReport. I thus have a deadlock here, I'll have to figure out a way to overcome this.

However, this whole lock situation got me thinking about how madExcept manages to get the stacktrace for all threads. I mean, is it freezing all threads while handling an exception in the "except" block?
If yes, I could also have an issue where the thread that might release the lock object cannot run until the bugreport is created, but the exception handler waits for it to run. But maybe I got this wrong, because definitely, I have a deadlock above.

Thanks for your answers
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Re: Locks in an ExceptHandler

Post by madshi »

It depends on the option called "pause all running delphi/bcb threads" here:

http://help.madshi.net/madExceptSettings2.htm

Might make sense for you to uncheck that, if it's not already unchecked?

If you still get deadlock problems, my suggestion would be to not "blindly" enter the critical section (or whatever synchronization object you're using), but to check if it's already in use and simply skip AddDetailsExceptHandler in that case. E.g. you can use TryEnterCriticalSection instead of EnterCriticalSection.
obones
Posts: 66
Joined: Fri May 15, 2009 11:47 am

Re: Locks in an ExceptHandler

Post by obones »

That's what I thought too, this option is set to False. And because those threads don't process any messages and don't call "PauseMeEventually", they wouldn't be paused anyway.

Not entering blindly is something I had not thought of, the TMonitor record does provide a TryEnter method.

But in the end, I figured I could avoid locking anyway, so it's even easier this way.

Thanks for your answer
Post Reply