in reply to cgi thread creation segfault

The situation between running the script from the command line and running it from mod_perl is a fundamentally different one. When running it from the command line, your Perl interpreter is a dedicated process, while with mod_perl, the interpreter has been dynamically loaded into the Apache process (as a shared library), i.e. the Perl interpreter and Apache are one and the same process.

Such close cooperation requires the involved parties to behave as expected. And apparently, Apache does not expect the Perl part to create multiple threads of themselves. In other words, Apache - at least the MPM you're using (prefork?) - doesn't seem to be thread-safe.

Replies are listed 'Best First'.
Re^2: cgi thread creation segfault
by lelotto85 (Initiate) on Dec 28, 2011 at 14:01 UTC

    I'm using MPM worker (not prefork)... These are my apache settings:

    Server version: Apache/2.2.17 (Ubuntu) Server built: Nov 3 2011 02:13:18 Server's Module Magic Number: 20051115:25 Server loaded: APR 1.4.2, APR-Util 1.3.9 Compiled using: APR 1.4.2, APR-Util 1.3.9 Architecture: 32-bit Server MPM: Worker threaded: yes (fixed thread count) forked: yes (variable process count) Server compiled with.... -D APACHE_MPM_DIR="server/mpm/worker" -D APR_HAS_SENDFILE -D APR_HAS_MMAP -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled) -D APR_USE_SYSVSEM_SERIALIZE -D APR_USE_PTHREAD_SERIALIZE -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT -D APR_HAS_OTHER_CHILD -D AP_HAVE_RELIABLE_PIPED_LOGS -D DYNAMIC_MODULE_LIMIT=128 -D HTTPD_ROOT="/etc/apache2" -D SUEXEC_BIN="/usr/lib/apache2/suexec" -D DEFAULT_PIDLOG="/var/run/apache2.pid" -D DEFAULT_SCOREBOARD="logs/apache_runtime_status" -D DEFAULT_ERRORLOG="logs/error_log" -D AP_TYPES_CONFIG_FILE="mime.types" -D SERVER_CONFIG_FILE="apache2.conf"
    As you can see i'm not using prefork MPM and that's why I thought apache could create threads while executing my perl script... Is my reasoning wrong? Maybe I'm missing some parameter or I misconfigured some IfModule statement.

      I thought apache could create threads while executing my perl script...

      The worker model uses threads for handling multiple requests.  From the docs:

      "A single control process (the parent) is responsible for launching child processes. Each child process creates a fixed number of server threads as specified in the ThreadsPerChild directive, as well as a listener thread which listens for connections and passes them to a server thread for processing when they arrive."

      This is not the same as creating threads "while executing" the Perl script. The situation you have is that one of the worker threads - with Perl being an integral part of them - is again trying to create further threads.  I'm not too familiar with the implementation details of the worker module to tell if that's supposed to work or not... (Practice shows, it doesn't :)

Re^2: cgi thread creation segfault
by mbethke (Hermit) on Dec 28, 2011 at 18:07 UTC
    In other words, Apache - at least the MPM you're using (prefork?) - doesn't seem to be thread-safe.

    <speculation>I would suppose prefork to be able to run this kind of script or at least pose the least problems, while any threaded MPMs will likely (and obviously do) become very confused if some other component such as Perl starts fiddling with the thread pool created by any of their processes.</speculation>

      I'm a bit confused... @mbethke, are you telling me that I should try with MPM prefork?

      I've been scraping the apache documentation for days and I found about MPM prefork: It is appropriate for sites that need to avoid threading for compatibility with non-thread-safe libraries So I thought this could not be the best approach. Otherwise I didn't understand how can I make a multi-threaded perl script working under Apache... Any help is welcome even if this could not be the correct place to post this request... Thank you very much.

        lelotto85, the way I read this MPM-prefork doc snippet is that prefork is the appropriate multiprocessing mode if you want to run an Apache module that is itself not thread safe, stuff like *cough*mod_php*cough*. These modules fail if they are entered by multiple threads at the same time, e.g. because they use module-global variables, so Apache plays it safe with MPM-prefork and simply forks a new process for each connection handler. That doesn't mean that modules that are in fact able to use threads can't use them in the context of this process, think (as I said that's speculation because I haven't tried) it's rather the opposite: if Apache doesn't manage its own threading, you're least likely to screw it up when you create your own threads. May be worth trying.

        That said, are you sure you need threads in your CGI? Depending on what other content or even other sites your Apache serves MPM-prefork can perform worse than other modules, especially on slow forkers like Solaris or Windows. Frameworks like Coro or POE could save you some headaches (although the latter may bring its own performance problems by being rather fat) by using cooperative multitasking that lets you have virtual threads without real multiprocessing. A sensibly loaded web server will make use of multiple CPUs anyway by using them to handle distinct connections.

        Finally got it!!!

        My error was in apache configuration... I had:

        SetHandler perl-script

        Instead of:

        SetHandler modperl

        Thanks for your support! ;P