Problems with multi-threaded FastCGI server

Thierry Pierron <t.pierron-E+nNfmQdIM/[email protected]>
Newsgroups gmane.comp.web.fastcgi.devel
Message-ID <[email protected]>
Hello,

First, I know that this has already been asked on this list, but I 
wonder the lack of answer. The problem is simple : I can't make work 
the multi-threaded example provided with libfcgi. I'm running fastcgi 
with Apache 2.0.54 on Win32 with fcgi sdk that can be found here 
http://www.fastcgi.com/dist/.

As far as I've investigated, I got a timeout in the apache log, saying 
that fastcgi server has not respond for 30 seconds. It seems that 
whenever a thread leave the FCGX_Accept_r() function, the connection is 
broken as soon as another thread enter it : I can see the content of 
the first FCGX_FPrintF, just before the sleep(), and then I got the 
classical apache Internal error message.

If I protect the whole loop by a mutex, it works fine, though it's like 
having mono-threaded application.

I included the threaded.c example slighty modified to work with Win32 
API. I wonder if anyone succeed to make it work as a FastCGI server...


-- 
Thierry Pierron
Polkadots Software Inc.
2501 Dollard
Lasalle, QC
H8N 1S2 Canada

___________________________________
fastcgi-developers mailing list
http://fastcgi.com/fastcgi-developers/
threaded.c (text/plain, 1.7 KB)
/*
 * threaded.c -- A simple multi-threaded FastCGI application.
 */

#include <process.h>
#include <windows.h>
#include <sys/types.h>
#include "fcgiapp.h"

#define THREAD_COUNT 20

static int counts[THREAD_COUNT];

static CRITICAL_SECTION accept_mutex;
static CRITICAL_SECTION counts_mutex;

static void doit(void *a)
{
    int rc, i, thread_id = (int)a;
    FCGX_Request request;
    char *server_name;

    FCGX_InitRequest(&request, 0, 0);

    for (;;)
    {
        /* Some platforms require accept() serialization, some don't.. */
        EnterCriticalSection(&accept_mutex);
        rc = FCGX_Accept_r(&request);
        LeaveCriticalSection(&accept_mutex);

        if (rc < 0)
            break;

        server_name = FCGX_GetParam("SERVER_NAME", request.envp);

        FCGX_FPrintF(request.out,
            "Content-type: text/html\r\n"
            "\r\n"
            "<title>FastCGI Hello! (multi-threaded C, fcgiapp library)</title>"
            "<h1>FastCGI Hello! (multi-threaded C, fcgiapp library)</h1>"
            "Thread %d, Process xyz<p>"
            "Request counts for %d threads running on host <i>%s</i><p><code>",
            thread_id, THREAD_COUNT, server_name ? server_name : "?");

        FCGX_FFlush(request.out);

        Sleep(2000);

        EnterCriticalSection(&counts_mutex);
        ++counts[thread_id];
        for (i = 0; i < THREAD_COUNT; i++)
            FCGX_FPrintF(request.out, "%5d " , counts[i]);
        LeaveCriticalSection(&counts_mutex);

        FCGX_Finish_r(&request);
    }
}

int main(void)
{
    int i;

    FCGX_Init();

    InitializeCriticalSection(&accept_mutex);
    InitializeCriticalSection(&counts_mutex);

    for (i = 1; i < THREAD_COUNT; i++)
        _beginthread(doit, 0, (void*)i);

    doit(0);

    return 0;
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.