Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/RTL/Delphi/Thread support    [ Add a report in this area ]  
Report #:  86851   Status: Open
Timeout hole in TMonitor.Enter()
Project:  Delphi Build #:  14.0.3615.26342
Version:    14.0 Submitted By:   Remy Lebeau (TeamB)
Report Type:  Minor failure / Design problem Date Reported:  8/2/2010 6:12:08 PM
Severity:    Infrequently encountered problem Last Updated: 3/20/2012 2:24:39 AM
Platform:    All platforms Internal Tracking #:   281271
Resolution: None (Resolution Comments) Resolved in Build: : None
Duplicate of:  None
Voting and Rating
Overall Rating: No Ratings Yet
0.00 out of 5
Total Votes: None
In TMonitor.Enter(), if the Timeout parameter is not INFINITE, then Enter() uses GetTickCount() in a spin lock timeout loop.  However, it is making a classic GetTickCount() blunder in that it assumes that GetTickCount() never wraps back to 0.  If the ticks wrap while Enter() is running, the timeout calculations will be off and Enter() can exit prematurely.  It needs to check for wrapping when calculating the elapsed tick times.
Steps to Reproduce:

Tomohiro Takahashi at 8/2/2010 6:54:34 PM -
> ... If the ticks wrap while Enter() is running, ...
Could you please tell us more detail about 'wrap' and attach sample project to confirm the issue, if possible?

Adam Wu at 8/30/2010 8:17:20 AM -
I think he is talking about the Windows API:

"...the time will wrap around to zero if the system is run continuously for 49.7 days..."

It is kinda hard to test, you have to start the program and keep it running on the computer until it reaches 49.7 days uptime... But, it definitely will happen, as warned in the MSDN documentation.

PS. GetTickCount64() does not have this problem, but Delphi needs to go 64bit first... :P

Ryan VanIderstine at 11/21/2010 9:06:46 AM -
You can simulate this failure by stubbing GetTickCount() and have it count much faster although the supplied MSDN documentation should be enough to confirm that it a problem (although an edge case with an object lock that extends that period).

Server Response from: ETNACODE01