View Issue Details

IDProjectCategoryView StatusLast Update
0002957CCdcielGeneralpublic26-10-06 23:16
ReporterJan Dubovsky Assigned To 
PrioritynormalSeveritymajorReproducibilityN/A
Status newResolutionopen 
Summary0002957: Race condition on the PHD2 TCP socket in the internal autoguider client
Description`T_autoguider_phd` (src/cu_autoguider_phd.pas) is itself a `TThread`.
`Execute` (~line 115-157) runs the receive loop and, in its `finally`
block, calls `tcpclient.Disconnect` and `tcpclient.Free` once the thread
terminates.

`Send` (~line 159-172), which is called from the main thread (via
Guide/Pause/Dither/GetLockPosition), only guards access with a
non-atomic check: `(not terminated) and (tcpclient <> nil)`, then calls
`tcpclient.Sock.SendString(...)`. There is no critical section protecting
`tcpclient` between the two methods.

If `Terminate` fires concurrently — e.g. the thread notices a connection
error, or the user disconnects while `WaitBusy`/`WaitGuiding` is pumping
`Application.ProcessMessages` and re-enters — `Execute`'s `finally` block
can free `tcpclient` in the small window right after `Send`'s check
passed, resulting in an access to freed memory.
Steps To ReproduceHard to reproduce deterministically; most likely when PHD2 is disconnected
or its connection drops while actively guiding or dithering. Observed as
an intermittent crash / access violation around PHD2 disconnect.
Additional Information### Suggested fix

Protect `tcpclient` with a critical section (or an atomic
reference/interlocked flag) shared between `Execute`'s teardown and
`Send`, so `Send` cannot run concurrently with `tcpclient.Free`.
Tagscrash

Activities

There are no notes attached to this issue.

Issue History

Date Modified Username Field Change
26-10-06 23:16 Jan Dubovsky New Issue
26-10-06 23:16 Jan Dubovsky Tag Attached: crash