You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 13 Next »

If a lock is being held and an operation that can block is performed, any other thread that needs to acquire that lock may also block. This condition can degrade system performance or cause a deadlock to occur.

Blocking calls include, but are not limited to, network, file, and console I/O.

Noncompliant Code Example

This noncompliant code example demonstrates an occurrence of a blocking call that waits to receive data on a socket while a mutex is locked. The recv() call blocks until data arrives on the socket. While it is blocked, other threads that are waiting for the lock are also blocked.

Although this example is specific to network I/O, the recv() call could be replaced with any blocking call, and the same behavior would occur.

mtx_t mutex;

void thread_foo(void *ptr) {
  uint32_t num;
  int result;
  int sock;

  /* sock is a connected TCP socket */

  if ((result = recv(sock, (void *)&num, sizeof(uint32_t), 0)) < 0) {
    /* Handle Error */
  }

  /* ... */

  if ((result = mtx_unlock(&mutex)) != thrd_success) {
    /* Handle Error */
  }
}

int main() {
  thrd_t thread;
  int result;

  if ((result = mtx_init(&mutex, mtx_plain)) != thrd_success) {
    /* Handle Error */
  }

  if (thrd_create(&thread,(void *)& thread_foo, NULL) != thrd_success) {
    /* Handle Error */
  }

  /* ... */

  thrd_join(thread, NULL);

  if ((result = mtx_destroy(&mutex)) != thrd_success) {
    /* Handle Error */
  }

  return 0;
}

Compliant Solution (Block while Not Locked)

This compliant solution performs the recv() call when the lock has not been acquired. The blocking behavior consequently affects only the thread that called the blocking function.

void thread_foo(void *ptr) {
  uint32_t num;
  int result;
  int sock;

  /* sock is a connected TCP socket */

  if ((result = recv(sock, (void *)&num, sizeof(uint32_t), 0)) < 0) {
    /* Handle Error */
  }

  if ((result = mtx_lock(&mutex)) != thrd_success) {
    /* Handle Error */
  }

  /* ... */

  if ((result = pthread_mutex_unlock(&mutex)) != 0) {
    /* Handle Error */
  }
}

Compliant Solution (Use a Nonblocking Call)

This compliant solution performs the recv() call with the parameter o_nonblock, which causes the call to fail if no messages are available on the socket.

void thread_foo(void *ptr) {
  uint32_t num;
  int result;

  /* sock is a connected TCP socket */

  if ((result = recv(sock, (void *)&num, sizeof(uint32_t), O_NONBLOCK)) < 0) {
    /* Handle Error */
  }

  if ((result = mtx_lock(&mutex)) != thrd_success) {
    /* Handle Error */
  }

  /* ... */

  if ((result = mtx_unlock(&mutex)) != thrd_success) {
    /* Handle Error */
  }
}

Exceptions

CON36-EX1: A thread may block while holding one or more locks and waiting to acquire another lock. When acquiring multiple locks, the order of locking must avoid deadlock, as specified in CON35-C. Avoid deadlock by locking in predefined order.

Risk Assessment

Blocking or lengthy operations performed within synchronized regions could result in a deadlocked or an unresponsive system.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

CON36-C

low

probable

high

P2

L3

Related Guidelines

The CERT Oracle Secure Coding Standard for Java: LCK09-J. Do not perform operations that can block while holding a lock

Sources

[Barney 2010] POSIX Threads Programming
[Open Group] pthread_cancel(), recv()


  • No labels