Atozed Forums

Full Version: Critical section problem: new problem on old code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, all.  I have an IW10 stand-alone app packaged as a Windows service compiled with Delphi XE that continues to run great.  Except, that on one customer server (Win Server 2012 Datacenter) the service has begun crashing periodically.  The same .EXE doesn't seem to have this problem elsewhere.

In any case, the error logged in the Windows Event Log is Exception 0xc0000264 ... which pertains to critical section not owned.

I understand what that means, and am aware of fixes for some historical bugs.  (For example my project does use the IWRtlFix unit.)

I also understand that threading race conditions can be tricky, and can be masked until one day they appear.

So my question is:  Are there known Delphi or IW bugs discovered in the past few years that would impact IW10 on XE that are related to critical sections?

I've reviewed the code, and it seems to be OK.  But something (like a fairly recent Windows update) has caused the previously-stable service to crash.  This article seems relevant though:  https://docs.microsoft.com/en-us/windows...on-objects ...but that specific change was some time ago (before I began having problems).

Any ideas?
CriticalSections are actually quite simple. I dont see anything in that article that would affect CriticalSection usage unless someone was depending on them to be an ordered queue.

I use CriticalSections extensively in IntraWeb, Indy and other areas and have never had an issue with them.

IW10 is pretty old but I'm not aware off the top of my head of any issues and we do have some customers still using 10 in production.

There may be some hints here:
https://stackoverflow.com/questions/2174...ot-working

0xc0000264 may happen if your CS is being destroyed and then later an attempted use.
Hi, Chad.  Thanks for the reply.  I'm not using critical sections directly...I am just reviewing their use in IW10 and the Delphi XE RTL.

The focus of my inquiry is this: A binary that has been running fine has recently become unstable on at least one server, and in this case, the service has begun failing with a critical section-related exception 0xc0000264.  (The server in question is Win 2012 Datacenter in a VMWare virtual machine on fast hardware...so fast hardware performance could contribute to exposing timing-related CS problems.)

I just wanted to see if there were any problems discovered in Delphi RTL or IW with respect to critical sections.

A few links pertaining to IW and critical sections:

https://www.atozed.com/2012/01/20121030-en/)
https://forums.embarcadero.com/thread.js...eID=763497
https://www.atozed.com/2019/04/15-0-21-version-history/
https://www.delphitools.info/2011/11/30/...alsection/

...so while critical sections may be "simple" ... making them work as intended isn't always straight-forward :-)

Thanks for https://stackoverflow.com/questions/2174...ot-working ... I had seen that.

I suppose I will need to set up MadExcept and do some further tracking.  Do you have any other suggestions?
I think IW 10 was released more than 10 years ago. You should have in mind that multi-core computing was just starting to crawl in 2009. Most computers had 1-2 cores (Intel released its first quad core on 2008).
I'm sure that around that time most Delphi "multi-threading" code would actually run serialized and very few issues regarding multi-threading would surface. Code running in a Core 2 Duo in 2009 would never present the same issues (race conditions and *specially* dead locks) as it would running in a new 32-core Ryzen Threadripper CPU.

I don't have any simple answer for you, but here are my comments on each link you mentioned:

Quote:https://www.atozed.com/2012/01/20121030-en/)

Yes, this is a major problem which can be reproduced in test environment. Our fix introduces a new critical section object in order to avoid an application crash. This fix has been redesigned in newer IW 15.1.0 (currently being tested) and won't use any critical sections. I have never found issues with race conditions, but when 3rd party and user code is involved, certainly a dead lock is a possibility. I don't see how an exception or an application crash could come from this, though.

There are some other places in RTL where you can find potential problems (which would never happen on a Core 2-Duo but can definitely appear on a 32-core CPU, like DFM steraming)

Quote:https://forums.embarcadero.com/thread.js...eID=763497

Retrieving all sessions in one locked list is a bad idea. Really. It might "work" for a tiny application with 5 users, but not more than this. We have also redesigned this "feature" completely in newer IW 15.1.0. BTW, it doesn't involve any critical sections to retrieve a session list.

About the SRW lock (Slim Reader Writer), it replaced the session list critical section since latest IW 12 versions. It is faster than a std critical section and scales much better because allow multiple readers with the drawback that it can't be used recursively (something we don't do anyway in IW code)

Quote:https://www.atozed.com/2019/04/15-0-21-version-history/

This specific critical section object doesn't exist in IW 10 (or 11 and 12 either). It was introduced in IW 14. A recent change in design made it unnecessary. 

Quote:https://www.delphitools.info/2011/11/30/...alsection/

This fix has been applied to IW since version 12, IIRC. The article doesn't explain it deeply but that's definitely a problem that should be addressed.