Is FastCgi thread safe?

I am a little confused about how FastCGI works. Does only one instance of my program really work, or is there some kind of magic thread? This is important because if I have data structures that have an area outside the main loop, I need to know if these data structures should be thread safe.

EDIT: My application is in Perl, and here is my apache2 configuration:

<IfModule mod_fastcgi.c> AddHandler fastcgi-script .fcgi .fcg FastCgiServer /usr/lib/app/process.fcg -idle-timeout 60 -processes 1 </IfModule> 

Just to understand what I'm asking ... for this code:

 use CGI::Fast qw/:standard/; my %sharedHash; while (new CGI::Fast) { # do stuff with %sharedHash } 

Is the “make stuff” part safe, or is it part of “multithreaded magic," which may mean that more than one thread is doing "doing things" at the same time, thereby corrupting %sharedHash ?

+6
fastcgi
source share
2 answers

FastCGI is just the interface between your web server and your application. Your application can be multithreaded (almost always with Java, often in Python) or written in an asynchronous event-driven style (Twisted in Python, Node.js, etc.). If the first, then you need to make sure that your access to the global state structures is correctly synchronized with the stream.

From the FastCGI white paper : Architecture Independence. CGI is not tied to any specific server architecture (single-threaded, multi-threaded, etc.).

+1
source share

I believe this depends on the Fast-CGI application. Looking at the FastCGI specification , section 4 (Types of Management Records), the application sets 3 values ​​for simultaneous connections:

  • FCGI_MAX_CONNS : the maximum number of simultaneous transport connections this application will accept, for example. "1" or "10".

  • FCGI_MAX_REQS : the maximum number of simultaneous requests to this application will accept, for example. "1" or "50".

  • FCGI_MPXS_CONNS : "0" if this application does not multiplex connections (ie, processing requests for each connection simultaneously), "1" otherwise.

From this it can be seen that while your application sets 1 for FCGI_MAX_CONNS and FCGI_MAX_REQS and 0 for FCGI_MPXS_CONNS , then any FCGI_MPXS_CONNS -compatible web server will consider your application as completely single-threaded, presumably starting new processes for processing parallel requests on the web server.

Update: Regarding the magic streaming and your data structures, if you don’t share your data structures between several requests, I don’t see the reasons why they should be thread safe - t see why several streams will be used to process one request, however, it depends on your FastCGI library.

+1
source share

All Articles