Apache Module: globlal variables revert to original state lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Tor · 2012-06-22 18:15 Hi, I hope this is the right section of the Forum to ask this: I started to write an apache module with freepascal, basically using  http://wiki.freepascal.org/FPC_and_Apache_Modules. I use some global variables to hold some information, but I noticed that after 100 (or was it 101?)  accesses they revert to their original state, which they had after the initialization of the module. I would like to know 1. why? and more importantly: 2. How do I prevent it? It would be nice if someone could answer that for me (I didn't find anything with google). If needed I can provide a short example, which produces this effect. Thanks in advance, Tor ---------------------------------------------------------------------- Leledumbo · 2012-06-23 03:43 From here, global variables aren't thread safe. Try protecting any write attempt to it in a critical section. ---------------------------------------------------------------------- Tor · 2012-06-23 08:55 Thanks for the information, but... it doesn't help. Here my test code (based on the hello_world module) with CriticalSection: Code: [Select]library mod_hello; {$ifdef fpc}   {$mode objfpc}{$H+} {$endif} {$IFDEF WIN32}   {$DEFINE WINDOWS} {$ENDIF} {$define Apache2_2} uses SysUtils, httpd, apr; var  test_module: module; public name 'hello_module';  str:String;  b:boolean;  mutex:TRTLCriticalSection; const   MODULE_NAME = 'mod_hello.so'; exports  test_module name 'hello_module'; {******************************************************************* *  Handles apache requests *******************************************************************} function DefaultHandler(r: Prequest_rec): Integer; cdecl; var   RequestedHandler:String; begin   RequestedHandler := r^.handler;   { We decline to handle a request if hello-handler is not the value of r->handler }   if not SameText(RequestedHandler, 'testapache-handler') then   begin     Result := DECLINED;     Exit;   end;   ap_set_content_type(r, 'text/html');   Result := OK;   if (r^.header_only <> 0) then   begin     Exit;   end;   if (b) then    begin      EnterCriticalsection(mutex);      str:='bar';      LeaveCriticalsection(mutex);    end else    begin      EnterCriticalsection(mutex);      b:=true;      str:='FOOOOOOOOOOOOOOOOOOO!';      LeaveCriticalsection(mutex);    end;    ap_rputs(PChar(str),r); end; {******************************************************************* *  Registers the hooks *******************************************************************} procedure RegisterHooks(p: Papr_pool_t); cdecl; begin   ap_hook_handler(@DefaultHandler, nil, nil, APR_HOOK_MIDDLE); end; {******************************************************************* *  Library initialization code *******************************************************************} var i:Integer; begin     b:=False;     InitCriticalSection(mutex);     STANDARD20_MODULE_STUFF(test_module);     with test_module do     begin       name := MODULE_NAME;       register_hooks := @RegisterHooks;     end; end.  If I use this module I first see the foo and after a refresh the bar. But if I refresh around 100 times or wait a while and then refresh, it says foo again. EDIT: I found out, that Apache has the (for me) nasty habit of giving each thread its own copy of the global variables. There are some methods to have shared global variables, but none of them are currently availabe in FreePascal. => So I just write my own server with synapse....