Delphi XE8 gracefully handle ETetheringException at application startup
Asked Answered
A

1

8

The Delphi app tethering manager reserves 20 ports in the range from 2020-2039 to start up its communication threads. I wanted to find out what happens, when the 21st application is started on the same computer. It turns out that this application just hangs unresponsive until it is killed and I haven't found a way to terminate it gracefully.

To reproduce, just drop a TTetheringManager and a TTetheringProfile on an empty form, connect the profile to the Manager, compile, start the application 20 times outside the IDE and finally start the 21st instance in the IDE.

This is what I have found out so far:

In System.Tether.NetworkAdapter: procedure TTetheringNetworkManagerCommunicationThread.Execute; an ETetheringException.Create(SManagerNetworkCreation) is raised when either the TCP or the UDP communication server could not be started. So far so good.

This exception shows up in the Debugger. When I continue and step through the following code, the application finally hangs in System: function AcquireExceptionObject and never returns.

Since the application is raised during Application.Run, I tried to catch the Exception via:

try
  Application.Run;
except
  ShowMessage('Something went terribly wrong!');
end;

but this didn't work. Any suggestions how I can catch (or prevent) this exception?

Asta answered 16/6, 2015 at 10:11 Comment(9)
You should be able to add an event handler for the application's OnException event and handle the exception there.Apogeotropism
Thanks for your suggestion, but this doesn' help either. I tried setting Application.OnException, but this event handler is never executed. The exception can also be triggered by assigning the TetherManager to the TetherProfile at run time, like: procedure TForm1.FormShow(Sender: TObject); begin try try TetheringAppProfile1.Manager := TetheringManager1; except ShowMessage('Something went wrong here: '); end; finally ShowMessage('Finally made it'); end; end; Spoondrift
Boy this looks ugly :/ Neither except nor finally are ever reached and the application just hangs.Spoondrift
If you can't detect exception with try..except block then it is possible that the component already has a code to handle such exception by itself.Hooper
I've run this with madexcept, which displays/catches the exception. See here: s25.postimg.org/j0l8xe7f3/Tether1.png s25.postimg.org/quluisf7z/Tether2.png Hopefully that gives some idea of what's going on.Phage
Looks like the exception is in a thread. Is it caught? Does the thread terminate (from memory, that's what usually happens with an uncaught exception in a thread)?Pillar
Having pondered the call stacks again, there's 2 points to note: 1) The main thread is blocked/waiting in a WaitFor() presumably for some signal to wake up, and 2) The exception that happens in the thread obviously does not then wake up the main thread, ergo the Exception is never seen/processed and the app hangs etc. It smells like a bug to me. At the very least I'd have expected there to be some logic somewhere to ensure that the main thread gets woken up in case of fatal exception, such as here.Phage
@Phage thanks for your observation. I also think it's a bug in the tethering implementation, but I wanted to get some feedback from the community in case I missed something.Spoondrift
Until this is fixed, you could put in code that refuses to become the 20th connection. If there are 19, stay out of it, which you should be able to trap for and handle gracefully.Micelle
A
0

Since this really seems to be a bug, I filed a quality report for it: https://quality.embarcadero.com/browse/RSP-11345

Asta answered 25/6, 2015 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.