Skip to content

Potential data races when accessing variable stderr in function PthreadCall #499

@ITWOI

Description

@ITWOI

Hi all,

Our bug scanner has reported a data race issue at env_posix.cc#L551

Followings are code snippets.

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<PosixEnv*>(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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions