Sep 25, 2012

MySQL 'Cannot create thread' on Linux

It may be not obvious, but on Linux RLIMIT_NPROC  limits not number of processes, but number of threads.
Here is proof:

[a@a-lnx ~]$ cat thread_test.c 

#include pthread.h
#include string.h
#include stdio.h
#include stdlib.h
#include unistd.h
#include errno.h
#include ctype.h

static void *
thread_start(void *arg)
    long int n = (long int) arg;


    printf("Thread %d: exited\n", n);

    return 0;

main(int argc, char *argv[])
    long int i;

/* Create threads until get error */
    for (i=0; ; i++) {

        if (pthread_create(malloc(sizeof(pthread_t)), NULL, &thread_start, (void*)i) != 0)
            printf("Error after %d threads\n", i);

[a@a-lnx ~]$ gcc -pthread thread_test.c
[a@a-lnx ~]$ ./a.out
Error after 906 threads
[a@a-lnx ~]$ ulimit -u
[a@a-lnx ~]$ ulimit -u 2000
[a@a-lnx ~]$ ./a.out
Error after 1883 threads

So make sure it is set high enough, otherwise MySQL can generate 'Cannot create thread' errors. 
Since this limit applies to user (not to process), this parameter is especially important if multiple mysql instances are running (with the same user).

No comments:

Post a Comment