title: multi threaded TCP server in C crashes
body: <p>I wrote a multi threaded TCP server in C. It crashes when I run multiple test client against it. I get 2 type of crashes which both seems to have same root cause. The attached crash happens in main. In the other flavor, I get the crash in client part when it wants to lock in order to close the socket.
  Can somebody please tell me what causes the crash?</p>
  
  <p>TCP Server code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;arpa/inet.h&gt; //inet_addr
  #include &lt;pthread.h&gt; //for threading , link with lpthread
  
  #define BUFSIZE     2048 // TODO
  #define MAXWORKERS 10
  
  pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
  const pthread_cond_t condInit = PTHREAD_COND_INITIALIZER;
  pthread_cond_t idle = PTHREAD_COND_INITIALIZER;
  
  typedef struct {
      pthread_t       tid;
      int             sd;
      pthread_cond_t  cond;
      uint8_t         num;
      uint8_t         state;
  } worker_t;
  
  worker_t     workers[MAXWORKERS]; //  reference to worker
  
  void error(const char *msg)
  {
      perror(msg);
      exit(1);
  }
  
  /* worker thread */
  void *handle_client(void *arg)
  {
      worker_t* worker = (worker_t *) arg;
      int n;
      char buf[BUFSIZE];
  
      /* By default a new thread is joinable, we don't
       really want this (unless we do something special we
       end up with the thread equivalent of zombies). So
       we explicitly change the thread type to detached
       */
  
      pthread_detach(pthread_self());
  
      printf("Thread %ld started for client number %d (sd %d)\n", pthread_self(),
              worker-&gt;num, worker-&gt;sd);
  
      /* worker thread */
      while (1)
      {
          /* wait for work to do */
          while (worker-&gt;state == 0)
          {
              pthread_cond_wait(&amp;worker-&gt;cond, &amp;mtx);
          }
          int sd = worker-&gt;sd; /* get the updated socket fd */
          pthread_mutex_unlock(&amp;mtx);
  
           n = read(sd, buf, BUFSIZE);
           if (n &lt; 0)
               error("ERROR reading from socket");
  
           n = write(sd, "I got your message",18);
           if (n &lt; 0)
               error("ERROR writing to socket");
  
          /* work done - set itself idle assumes that read returned EOF */
          pthread_mutex_lock(&amp;mtx);
          close(sd);
          worker-&gt;state = 0;
          printf("Worker %d has completed work \n", worker-&gt;num);
          pthread_cond_signal(&amp;idle); /* notifies dispatcher*/
          pthread_mutex_unlock(&amp;mtx);
      } /* end while */
  
  }
  
  int main() { /* Dispatcher */
      int ld, sd;
      struct sockaddr_in skaddr;
      struct sockaddr_in from;
      int addrlen, length;
      int i;
  
      if ((ld = socket( AF_INET, SOCK_STREAM, 0)) &lt; 0) {
          perror("Problem creating socket\n");
          exit(1);
      }
  
      skaddr.sin_family = AF_INET;
      skaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  
      //skaddr.sin_port = htons(0);
      skaddr.sin_port = htons(41332);
  
      if (bind(ld, (struct sockaddr *) &amp;skaddr, sizeof(skaddr)) &lt; 0) {
          perror("Problem binding\n");
          exit(0);
      }
  
      /* find out what port we were assigned and print it out */
  
      length = sizeof(skaddr);
      if (getsockname(ld, (struct sockaddr *) &amp;skaddr, &amp;length) &lt; 0) {
          perror("Error getsockname\n");
          exit(1);
      }
      in_port_t pport = ntohs(skaddr.sin_port);
      printf("%d\n", pport);
  
      /* put the socket into passive mode (waiting for connections) */
  
      if (listen(ld, 5) &lt; 0) {
          perror("Error calling listen\n");
          exit(1);
      }
  
      /* do some initialization */
  
      for (i = 0; i &lt; MAXWORKERS; i++) {
          workers[i].state    = 0;
          workers[i].num      = i;
          workers[i].sd       = 0;
          workers[i].cond     = condInit;
          pthread_create(&amp;workers[i].tid, NULL, handle_client, (void *) &amp;workers[i]);
      }
  
      /* Dispatcher now processes incoming connections forever ... */
      while (1) {
          printf("Ready for a connection...\n");
          addrlen = sizeof(skaddr);
  
          printf("trying to accept a new connection\n");
          if ((sd = accept(ld, (struct sockaddr*) &amp;from, &amp;addrlen)) &lt; 0) {
              perror("Problem with accept call\n");
              exit(1);
          }
          printf("Got a connection - processing...\n");
          for (i = 0; i &lt; MAXWORKERS; i++) {
              pthread_mutex_lock(&amp;mtx);
              if (workers[i].state == 0) /* worker i is idle – dispatch him to work */
              {
                  printf("dispatch to worker number: %d \n", i);
                  pthread_mutex_unlock(&amp;mtx);
                  break;
              }
              printf("worker number: %d was busy\n", i);
  
              pthread_mutex_unlock(&amp;mtx);
          }
  
          if (i == MAXWORKERS) {
              /* all workers busy */
              pthread_mutex_lock(&amp;mtx);
              pthread_cond_wait(&amp;idle, &amp;mtx); /* wait for one idle; */
              pthread_mutex_unlock(&amp;mtx);
          } else { /* dispatch worker */
              pthread_mutex_lock(&amp;mtx);
              workers[i].state = 1;
              workers[i].sd = sd;
              pthread_cond_signal(&amp;workers[i].cond); /* wake up worker */
              pthread_mutex_unlock(&amp;mtx);
          }
      }
  }
  </code></pre>
  
  <p>crash:</p>
  
  <pre><code>Thread 11 (Thread 0x7fae90004700 (LWP 16318)):
  #0  pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
  #1  0x0000000000401024 in handle_client (arg=0x602408 &lt;workers+648&gt;) at main.c:87
  #2  0x00007fae94bdf6ba in start_thread (arg=0x7fae90004700) at pthread_create.c:333
  #3  0x00007fae949153dd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
  
  Thread 10 (Thread 0x7fae90805700 (LWP 16317)):
  #0  pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
  #1  0x0000000000401024 in handle_client (arg=0x6023c0 &lt;workers+576&gt;) at main.c:87
  #2  0x00007fae94bdf6ba in start_thread (arg=0x7fae90805700) at pthread_create.c:333
  #3  0x00007fae949153dd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
  
  Thread 9 (Thread 0x7fae9400c700 (LWP 16310)):
  #0  pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
  #1  0x0000000000401024 in handle_client (arg=0x6021c8 &lt;workers+72&gt;) at main.c:87
  #2  0x00007fae94bdf6ba in start_thread (arg=0x7fae9400c700) at pthread_create.c:333
  #3  0x00007fae949153dd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
  
  Thread 8 (Thread 0x7fae91006700 (LWP 16316)):
  #0  pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
  #1  0x0000000000401024 in handle_client (arg=0x602378 &lt;workers+504&gt;) at main.c:87
  #2  0x00007fae94bdf6ba in start_thread (arg=0x7fae91006700) at pthread_create.c:333
  #3  0x00007fae949153dd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
  
  Thread 7 (Thread 0x7fae91807700 (LWP 16315)):
  #0  pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
  #1  0x0000000000401024 in handle_client (arg=0x602330 &lt;workers+432&gt;) at main.c:87
  #2  0x00007fae94bdf6ba in start_thread (arg=0x7fae91807700) at pthread_create.c:333
  #3  0x00007fae949153dd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
  
  Thread 6 (Thread 0x7fae9300a700 (LWP 16312)):
  #0  pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
  #1  0x0000000000401024 in handle_client (arg=0x602258 &lt;workers+216&gt;) at main.c:87
  #2  0x00007fae94bdf6ba in start_thread (arg=0x7fae9300a700) at pthread_create.c:333
  #3  0x00007fae949153dd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
  
  Thread 5 (Thread 0x7fae92008700 (LWP 16314)):
  #0  pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
  #1  0x0000000000401024 in handle_client (arg=0x6022e8 &lt;workers+360&gt;) at main.c:87
  #2  0x00007fae94bdf6ba in start_thread (arg=0x7fae92008700) at pthread_create.c:333
  #3  0x00007fae949153dd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
  
  Thread 4 (Thread 0x7fae92809700 (LWP 16313)):
  #0  pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
  #1  0x0000000000401024 in handle_client (arg=0x6022a0 &lt;workers+288&gt;) at main.c:87
  #2  0x00007fae94bdf6ba in start_thread (arg=0x7fae92809700) at pthread_create.c:333
  #3  0x00007fae949153dd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
  
  Thread 3 (Thread 0x7fae9380b700 (LWP 16311)):
  #0  pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
  #1  0x0000000000401024 in handle_client (arg=0x602210 &lt;workers+144&gt;) at main.c:87
  #2  0x00007fae94bdf6ba in start_thread (arg=0x7fae9380b700) at pthread_create.c:333
  #3  0x00007fae949153dd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
  
  Thread 2 (Thread 0x7fae9480d700 (LWP 16309)):
  #0  pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
  #1  0x0000000000401024 in handle_client (arg=0x602180 &lt;workers&gt;) at main.c:87
  #2  0x00007fae94bdf6ba in start_thread (arg=0x7fae9480d700) at pthread_create.c:333
  #3  0x00007fae949153dd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
  
  Thread 1 (Thread 0x7fae94ff3700 (LWP 16308)):
  #0  0x00007fae94843428 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
  ---Type &lt;return&gt; to continue, or q &lt;return&gt; to quit---
  #1  0x00007fae9484502a in __GI_abort () at abort.c:89
  #2  0x00007fae9483bbd7 in __assert_fail_base (fmt=&lt;optimized out&gt;, assertion=assertion@entry=0x7fae94beb015 "mutex-&gt;__data.__owner == 0", file=file@entry=0x7fae94beaff8 "../nptl/pthread_mutex_lock.c", line=line@entry=81, 
      function=function@entry=0x7fae94beb180 &lt;__PRETTY_FUNCTION__.8623&gt; "__pthread_mutex_lock") at assert.c:92
  #3  0x00007fae9483bc82 in __GI___assert_fail (assertion=assertion@entry=0x7fae94beb015 "mutex-&gt;__data.__owner == 0", file=file@entry=0x7fae94beaff8 "../nptl/pthread_mutex_lock.c", line=line@entry=81, 
      function=function@entry=0x7fae94beb180 &lt;__PRETTY_FUNCTION__.8623&gt; "__pthread_mutex_lock") at assert.c:101
  #4  0x00007fae94be1f68 in __GI___pthread_mutex_lock (mutex=mutex@entry=0x602140 &lt;mtx&gt;) at ../nptl/pthread_mutex_lock.c:81
  #5  0x0000000000400cfa in main () at main.c:173
  </code></pre>