← Back to fpsonar
Five rules about state that is not safe to touch from the thread touching it: shared data written without a lock, a lock held across a callback, a critical section used before it exists, a thread-local variable nothing ever gives a value, and a UI control reached from a worker thread.
All are off by default.
Concurrency bugs are the ones least likely to be found by testing — they depend on timing, so they reproduce on the build server and not on your machine, or once a week in production. That makes them unusually worth catching statically, and it also means these rules are heuristics: each identifies a form that is usually wrong, not a proof that your program races.
Shared state
editGlobalWrittenFromThreadRoutine
editmajor · off by default
A write to a unit-level variable from a thread routine, with no critical section covering it. Two threads writing the same global without a lock is the textbook data race: on a 64-bit target even a single assignment need not be atomic, and the compiler is free to reorder around it.
fpsonar complains about this
var
GCounter: Integer; // unit level
procedure TWorker.Execute;
begin
GCounter := GCounter + 1; // unsynchronised
end;
fpsonar accepts this
procedure TWorker.Execute;
begin
EnterCriticalSection(GLock);
try
GCounter := GCounter + 1;
finally
LeaveCriticalSection(GLock);
end;
end;
InterlockedIncrement(GCounter) is the cheaper fix for a plain counter.
SynchronizeWithLockHeld
editmajor · off by default
A main-thread callback dispatched while a critical section is held — Synchronize inside a
lock. Synchronize blocks until the main thread runs the callback; if the main thread is
waiting for the same lock, neither can proceed and the application hangs with no error.
A deadlock that needs both threads to arrive at the wrong moment, so it survives testing and appears under load.
fpsonar complains about this
EnterCriticalSection(FLock);
try
Synchronize(@UpdateDisplay); // main thread may want FLock
finally
LeaveCriticalSection(FLock);
end;
fpsonar accepts this
EnterCriticalSection(FLock);
try
lSnapshot := FState; // copy under the lock
finally
LeaveCriticalSection(FLock);
end;
FDisplayState := lSnapshot;
Synchronize(@UpdateDisplay); // dispatch with nothing held
CriticalSectionNotInitialized
editmajor · off by default
A critical-section call on a routine local that no InitCriticalSection precedes. An
uninitialised critical section is whatever was on the stack, so entering it has undefined
behaviour — often it appears to work, which is the worst outcome.
Reported only for locals, where the analyser can see the whole story. A section held in a field or global may be initialised anywhere, and the rule stays silent rather than guess.
fpsonar complains about this
var
lLock: TRTLCriticalSection;
begin
EnterCriticalSection(lLock); // never initialised
fpsonar accepts this
var
lLock: TRTLCriticalSection;
begin
InitCriticalSection(lLock);
try
EnterCriticalSection(lLock);
try
Work;
finally
LeaveCriticalSection(lLock);
end;
finally
DoneCriticalSection(lLock);
end;
Thread-local and UI state
editThreadvarInitialization
editmajor · off by default
A threadvar that routines read but no routine ever assigns.
The trap is that a unit's initialization section runs once, for the thread that loads
the unit. Every other thread gets its own copy of the variable, zero-filled — so a threadvar
set up only in initialization holds nil, 0 or '' in every thread but the first. The
code works in single-threaded tests and fails the moment a second thread runs it.
The rule reports only implementation-section threadvars that are read somewhere and assigned in no routine body.
fpsonar complains about this
implementation
threadvar
GBuffer: TStringList;
procedure Append(const aLine: string);
begin
GBuffer.Add(aLine); // nil in every thread but the first
end;
initialization
GBuffer := TStringList.Create;
fpsonar accepts this
implementation
threadvar
GBuffer: TStringList;
function CurrentBuffer: TStringList;
begin
if GBuffer = nil then
GBuffer := TStringList.Create; // each thread initialises its own copy
Result := GBuffer;
end;
procedure Append(const aLine: string);
begin
CurrentBuffer.Add(aLine);
end;
Whichever thread creates the object must also free it, in that thread — a threadvar is not
cleaned up for you.
VclAccessOffMainThread
editmajor · off by default
A UI control member accessed directly in the Execute body of a class descending from
TThread. UI toolkits are not thread-safe: controls may only be touched from the main
thread, and doing otherwise produces corruption or a crash that reproduces only under load.
The rule matches a curated list of member names — Canvas, Caption, Checked, Color,
Cursor, Enabled, Font, Hint, Lines, Visible — and identifies the thread class by
its written ancestor name. It is a heuristic, deliberately narrow: it will not catch every
unsafe access, and it ships at low confidence for that reason.
fpsonar complains about this
procedure TWorker.Execute;
begin
while not Terminated do
begin
Step;
ProgressForm.Caption := 'working'; // touched from the worker thread
end;
end;
fpsonar accepts this
procedure TWorker.Execute;
begin
while not Terminated do
begin
Step;
Synchronize(@ShowProgress); // runs in the main thread
end;
end;
procedure TWorker.ShowProgress;
begin
ProgressForm.Caption := 'working';
end;
Queue is the non-blocking alternative to Synchronize where the worker need not wait.
Related chapters
editOwnership and lifetime generally are in memory and lifetime — relevant here, because a thread-local object has no owner unless the thread that created it frees it.
See also
edit- fpsonar — what fpSonar is, and how to run it
← Back to fpsonar