[FASTCGI] libfcgi in multi threaded app
Martin Chapman
chapmanm at pixia.com
Mon May 3 12:56:09 EDT 2010
AlannY,
Here is the bad news. Although the website claims that you can do this, you
really cannot run multiple fastcgi accepts in a true mutli threaded fashion
without modification to the code. The reason for this is because fastcgi
stores the stdio handles in a global structure that is not thread safe. The
example they provide launches multiple threads but in order to have it work
correctly you have to synchronize all the code between the accept and the
finish calls which defeats the purpose of having multiple threads in the
first place. This is what happens when you let multiple threads run
simultaneously using fastcgi:
1. All threads launch and they all block on FCGX_Accept_r until a request is
received.
2. The first request is received by a single thread and the thread starts to
process the request.
3. A second request is received by another thread before the first thread is
finished and when this happens the stdio variables in the global struct are
reset for the second request.
4. The first thread finishes it's processing and attempts to write its
response to stdio and it hangs because the stdio variables were reset by the
second thread when the second thread received the second request.
That said, a dude from Apple did post a way to get around this a few months
ago but I never got around to seeing if it worked and if it truly was a
solution for multi threaded processing. The link is listed below:
http://www.fastcgi.com/archives/fastcgi-developers/2009-December/000406.html
If it works I would love to hear about it.
Martin
-----Original Message-----
From: fastcgi-developers-bounces+chapmanm=pixia.com at mailman.fastcgi.com
[mailto:fastcgi-developers-bounces+chapmanm=pixia.com at mailman.fastcgi.com]
On Behalf Of AlannY
Sent: Monday, May 03, 2010 9:27 AM
To: fastcgi-developers at mailman.fastcgi.com
Subject: [FASTCGI] libfcgi in multi threaded app
Hi there.
I'm having trouble with libfcgi and my multi threaded application.
I've created a socket via FCGX_OpenSocket. Everything all right.
Then, I'm creating threads (5, for example). In each thread, I'm
creating a request with FCGX_InitRequest (with socket, created early and
FCGI_FAIL_ACCEPT_ON_INTR).
Then the main loop starts. In this loop I'm calling FCGX_Accept_r on
this request. Here is trouble. Some of threads can successfully accept,
but some of then can't. Return code from FCGX_Accept is -9.
I've tried to setup mutex before and after FCGX_Accept_r, but it's not
helps ;-(
At the end of thread, I'm calling FCGX_Finish_r with 0 (don't destroy
socket, because it may be used by another thread).
What can I do? How to sync threads?
P.S. Single threaded apps works well ;-(
--
)\._.,--....,'``.
/, _.. \ _\ (`._ ,.
`._.-(,_..'--(,_..'`-.;.'
_______________________________________________
FastCGI-developers mailing list
FastCGI-developers at mailman.fastcgi.com
http://mailman.pins.net/mailman/listinfo.cgi/fastcgi-developers
More information about the FastCGI-developers
mailing list