Undefined Exception Leaving Critical Section

Getting a random Undefined Exception when trying to leave a critical section i have previously entered in the same method

The Method is being called on a cooperative thread and looks like this

pRenderMutex.Enter()
try
//Do studd
catch err as runtimeException
  //Log errors
end try
pRenderMutex.Leave() //getting error here

pRenderMutex is not being nilled out, and is the default Type. Only seems to be happening on Windows machines. Never seen it before since compiling with 2024r4.2, previous build was with 2023r3.

I wonder if support for thread types added a bug in there.
Cannot replicate myself, but I am seeing logged issues from users.
Stack trace shows this as last entry
CriticalSection.Leave%%o

Just wondering if moving to 2025r1.1 would solve it
Cheers
Graham

Try this code, better save than sorry:

pRenderMutex.Enter()
try
//Do studd
catch err as runtimeException
  //Log errors
Finally 
pRenderMutex.Leave() // now it always cals the leave even if there was an exception.
end try

It could still be a bug, so keep that in mind. However when there is an exception (that you did not catch) it would have skipped the leave and thay may cause an issue.

It would of called the leave, if there was an error. there is no return there.

Not if there was another exception that you did not catch. You should expect the unexpected..:slight_smile:

I see you catch RuntimeException while you should never do that (or have a really good reason for it) since you could catch all kinds of wrong one’s.

Do you have more of the stack trace?

CriticalSection.Leave%%o
SlideshowEngine.Internal_PlayVideo%%o
SlideshowEngine.Internal_Play%%o
SlideshowEngine.Event_Run%%o

Nothing useful in there (the bottom is the Thread)

Its a catch all, I generally don’t want errors or use them to program so this works well for me. Other than EndException.

What would happen if the calling thread was stopped, in the middle of a critical section. Does entering a critical section link it to the current thread. If that is cleaned up could it cause this problem.

IE
CoopThread.Run
Enter Critical section
Kill Coop Thread from main thread
Leave Critical Section - get error

That clears things up for readers.

I guess that’s undefined behaviour, you could subclass your thread Stop method to delay (timer calllater of some boolean check) this case to overcome such stop in the middle.

I’m not sure what would happen, and that could be different per platform.

Could it be that some data is not within this criticalsection ?

Do you use ‘#pragma BackgroundTasks False’’ somehwere? If not you can try it around your thread code, sice it’s cooperative it should block other threads from switching (and maybe from crashing).

Can you try to see what kind of exception it is since you have a stack, you should have more info.

pRenderMutex.Enter()
try
//Do studd
catch err as runtimeException
  //Log errors
end try
pRenderMutex.Leave() //getting error here
In your app.unhandledException or where you got the exception:
Var exc_type As String = Introspection.getType(err).FullName
// log this exc_type somehwere

My unhandled exception code is a little out of date, written before introspection. might need to update it.

All this time I thought Xojo had a weird UndefinedException, it was just me not updating my code to better send / log errors :man_facepalming:

So you know what type of exception it is that you happen to get?

Nope, i will need to build a release and send to a client to test

1 Like

Catch-all exceptions can be tricky. Per the documentation:

EndException and ThreadEndException

Under some circumstances, an Exception block can catch an EndException or a ThreadEndException. This happens when you use a catch-all Exception statement, i.e. “Exception err as RuntimeException” instead of giving a more specific runtime Exception class. In such a case, you MUST re-raise the Exception to avoid messing up the runtime environment and create some unpredictable problems.

See Exception — Xojo documentation

If a thread is being killed, and you accidentally catch that ThreadEndException, bad things could happen.

I don’t see any log of catching the threadendexception however very good to know about. I normally don’t kill threads, i just set a flag and let them end on their own. Does not mean i am not doing that here.