alibaba / alibaba/RedAlert

Potential Data Race in HeartbeatClient

Open
#1 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
66
Forks
23
PR merge metrics
No merged PRs in 30d

Description

Hi,

Our code scanner has reported a data race at `workLoop` method of [HeartbeatClient](https://github.com/alibaba/RedAlert/blob/master/RAServer/ra/service/HeartbeatClient.cpp#L33)

```c++
bool HeartbeatClient::start() {
int err = pthread_create(&_thread, NULL, &HeartbeatClient::workLoop, this);
if (err != 0) {
LOG(ERROR) << "Cannot start thread for heartbeat client";
return false;
}
_running = true;
LOG(INFO) << "Start sending heartbeats to '" << _remoteHost << ":" << _remotePort << "'";
return true;
}
```
`_running` initialized with `false`,
Could there is a chance that HeartbeatClient::workLoop access the `_running` before the main thread has assigned `true` into, the heart beat thread might just stopped before the main thread has updated the value of `_running`.

May be we could move the assignment into thread body to avoid this issue.
```c++
void* HeartbeatClient::workLoop(void *arg) {
......
// client->_running = true; // <====== move here instead
while (client->_running) {
```

Regards,
Alex, SourceBrella Inc.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in RAServer/ra/service/HeartbeatClient.cpp at HeartbeatClient::start and workLoop. Trace when _running is initialized, written, and read around pthread_create, then verify whether the heartbeat thread can exit before startup completes. Done means the startup state is race-free and the client reliably continues its heartbeat loop.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
observability-sre
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.