google / google/leveldb

Potential data races when accessing variable stderr in function PthreadCall

Open
#499 3 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C++
Stars
39.4k
Forks
8.2k
PR merge metrics
No merged PRs in 30d

Description

Hi all,

Our bug scanner has reported a data race issue at [env_posix.cc#L551](https://github.com/google/leveldb/blob/master/util/env_posix.cc#L551)

Followings are code snippets.

```c++
void PosixEnv::Schedule(void (*function)(void*), void* arg) {
...
if (!started_bgthread_) {
started_bgthread_ = true;
PthreadCall(
"create thread",
pthread_create(&bgthread_, NULL, &PosixEnv::BGThreadWrapper, this));
}
...
PthreadCall("unlock", pthread_mutex_unlock(&mu_));
}

void BGThread();
static void* BGThreadWrapper(void* arg) {
reinterpret_cast(arg)->BGThread();
return NULL;
}

void PosixEnv::BGThread() {
while (true) {
...
PthreadCall("unlock", pthread_mutex_unlock(&mu_));
...
}
}

void PthreadCall(const char* label, int result) {
if (result != 0) {
fprintf(stderr, "pthread %s: %s\n", label, strerror(result));
...
}
}
```

The call trace is as follows:

`PosixEnv::Schedule->PthreadCall`

and

`PosixEnv::Schedule->BGThreadWrapper->PosixEnv::BGThread->PthreadCall`

The race may be rarely triggered, but it would corrupt the log when occured.

SourceBrella Inc.,
Yu

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.