Puzzled with this behavior. I'd appreciate help. I don't understand how initialization and finalization sections should behave with an Apache module on Linux (and maybe any .so). Take a look at this simple web broker application:

Puzzled with this behavior. I'd appreciate help. I don't understand how initialization and finalization sections should behave with an Apache module on Linux (and maybe any .so). Take a look at this simple web broker application:

unit WebModuleUnit1;

interface

uses System.SysUtils, System.Classes, Web.HTTPApp, TestLog;

type
TWebModule1 = class(TWebModule)
procedure WebModule1DefaultHandlerAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
end;

var
GlobalCount: Integer = 1;

var
WebModuleClass: TComponentClass = TWebModule1;

implementation

{%CLASSGROUP 'System.Classes.TPersistent'}

{$R *.dfm}

procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
Inc(GlobalCount);
Log('GlobalCount: ' + IntToStr(GlobalCount));
Response.Content :=
'GlobalCount: ' + IntToStr(GlobalCount);
end;

initialization
Log('Initialization’);
GlobalCount := 5;
finalization
Log('Finalization');
end.

Now, what I do is start Apache, perform some requests (I leave some time between requests to close the HTTP connection) and then shutdown Apache. This is the log from Apache on Windows (a sane one):

4/25/2017 7:01:03 PM [00000974]: Initialization
4/25/2017 7:01:05 PM [00000C14]: GlobalCount: 6
4/25/2017 7:01:06 PM [00000C14]: GlobalCount: 7
4/25/2017 7:01:06 PM [00000C14]: GlobalCount: 8
4/25/2017 7:01:06 PM [00000C14]: GlobalCount: 9
4/25/2017 7:01:07 PM [00000C14]: GlobalCount: 10
4/25/2017 7:01:07 PM [00000C14]: GlobalCount: 11
4/25/2017 7:01:07 PM [00000C14]: GlobalCount: 12
4/25/2017 7:01:07 PM [00000C14]: GlobalCount: 13
4/25/2017 7:01:14 PM [0000099C]: GlobalCount: 14
4/25/2017 7:01:16 PM [0000099C]: GlobalCount: 15
4/25/2017 7:01:26 PM [0000099C]: GlobalCount: 16
4/25/2017 7:01:44 PM [000010F0]: GlobalCount: 17

and this is the log from Apache on Linux:

Apache Start:
4/25/17 6:54:41 PM [7F391A6B7780]: Initialization
4/25/17 6:54:41 PM [7F391A6B7780]: Finalization
Requests from Browser:
4/25/17 6:54:52 PM [7F391A6B7780]: GlobalCount: 6
4/25/17 6:54:53 PM [7F391A6B7780]: GlobalCount: 7
4/25/17 6:54:53 PM [7F391A6B7780]: GlobalCount: 8
4/25/17 6:54:54 PM [7F391A6B7780]: GlobalCount: 9
4/25/17 6:54:54 PM [7F391A6B7780]: GlobalCount: 10
4/25/17 6:55:02 PM [7F391A6B7780]: GlobalCount: 6
4/25/17 6:55:07 PM [7F391A6B7780]: GlobalCount: 7
4/25/17 6:55:07 PM [7F391A6B7780]: GlobalCount: 8
4/25/17 6:55:15 PM [7F391A6B7780]: GlobalCount: 6
4/25/17 6:55:15 PM [7F391A6B7780]: GlobalCount: 7
4/25/17 6:55:16 PM [7F391A6B7780]: GlobalCount: 8
4/25/17 6:55:16 PM [7F391A6B7780]: GlobalCount: 9
4/25/17 6:55:16 PM [7F391A6B7780]: GlobalCount: 10
4/25/17 6:55:16 PM [7F391A6B7780]: GlobalCount: 11
4/25/17 6:55:16 PM [7F391A6B7780]: GlobalCount: 12
4/25/17 6:55:26 PM [7F391A6B7780]: GlobalCount: 6
Apache Shutdown:
4/25/17 6:55:32 PM [7FB7C987B780]: Initialization
4/25/17 6:55:32 PM [7F391A6B7780]: Finalization
4/25/17 6:55:32 PM [7F391A6B7780]: Finalization
4/25/17 6:55:32 PM [7F391A6B7780]: Finalization
4/25/17 6:55:32 PM [7F391A6B7780]: Finalization
4/25/17 6:55:32 PM [7F391A6B7780]: Finalization
4/25/17 6:55:32 PM [7F391A6B7780]: Finalization
4/25/17 6:55:32 PM [7FB7C987B780]: Finalization
4/25/17 6:55:32 PM [7F391A6B7780]: Finalization

Would anybody care and take a guess about why this might be happening?

Comments

  1. Yes, the on Linux Apache spawns multiple processes, different from Windows which are just multiple threads. I wonder, though, what is the exact explanation for the missing initialization calls. Is it related to the way Apache spawns its child processes?

    ReplyDelete

Post a Comment