title: exit() call pthread_mutex_lock()
body: <p>I have a strange problem with threads. If I start my program without arguments or with a <code>--help</code> option, it will exit immediately.</p>
  
  <p>The program loads a network checker thread on start. When it is time to exit or disable some online functions, it simply terminates the network checker thread. This appears to be done correctly... until I call <code>exit()</code>.</p>
  
  <p>On gdb i can see that the network checker is successfully closed - but for some obscure reasons when the main program calls <code>exit()</code> then <code>pthread_mutex_lock()</code> is also called.</p>
  
  <p>Here's the backtrace:</p>
  
  <pre><code>(gdb) bt
  #0  0x00007ffff711e804 in __lll_lock_wait () from /lib64/libpthread.so.0
  #1  0x00007ffff711a0e0 in _L_lock_533 () from /lib64/libpthread.so.0
  #2  0x00007ffff7119f79 in pthread_mutex_lock () from /lib64/libpthread.so.0
  #3  0x00007ffff7dea3cd in _dl_fini () from /lib64/ld-linux-x86-64.so.2
  #4  0x00007ffff6dbe6a1 in __run_exit_handlers () from /lib64/libc.so.6
  #5  0x00007ffff6dbe725 in exit () from /lib64/libc.so.6
  #6  0x0000000000413ec4 in option_handler (argc=1, argv=0x7fffffffdcb8) at autocrack.c:302
  #7  0x00000000004135a7 in main (argc=1, argv=0x7fffffffdcb8) at autocrack.c:34
  </code></pre>
  
  <p>This <em>does not</em> happen when I call <code>exit()</code> from inside my <code>main</code> function. This only happens when I call <code>exit()</code> <a href="https://github.com/tux-mind/autocrack/blob/3cb167b1d0732ce38e50db77d403f3ba4aa22fbf/autocrack.c#L302" rel="nofollow">from inside the option handler</a>. </p>
  
  <p>I used <code>grep -rin pthread_mutex_ .</code> to see where else locks are used in my program: the program should never reach these points.  </p>
  
  <p>The full source code can be found at <a href="https://github.com/tux-mind/autocrack" rel="nofollow">https://github.com/tux-mind/autocrack</a> </p>
  
  <p>What's going on?</p>