[fpc-pascal] fcl-web: Trequest.RemoteAddr is empty (sometimes)

Michael Van Canneyt michael at freepascal.org
Tue Jan 12 10:24:12 CET 2021



On Tue, 12 Jan 2021, Luca Olivetti via fpc-pascal wrote:

> El 8/1/21 a les 11:14, Luca Olivetti via fpc-pascal ha escrit:
>> El 8/1/21 a les 11:06, Michael Van Canneyt via fpc-pascal ha escrit:
>> 
>>>
>>> Are you using https ?
>> 
>> No, plain text http.
>> An even weirder fact is that I seem to get the wrong ip address (i.e. 
>> the address of a different client).
>
> I think I found the possible cause:
>
> I have an own thread that spawns a TEmbeddedHttpServer (actually a 
> derived class to manage server-sent-events, 
> https://lists.lazarus-ide.org/pipermail/lazarus/2020-June/238072.html) 
> with threaded:=true.
>
> My thread has a method to manage the requests and it does this
>
>     FRequest:=ARequest;
>     Synchronize(@SyncNewConnection);
>     if FCommandQueue<>nil then .....
>
>
> I think that, since the method is called from a different thread for 
> each request, if two requests come at the same time, the FRequest and/or 
> the FCommandQueue (which is set in the synchronized method) could get 
> mixed up.

If there is only 1 FRequest instance/location in your thread, then this is definitely
so...

> I now did 2 things:
>
> 1) protected that part of the code with a critical section
> 2) use TThread.Synchronize(nil,..) (since it's not my thread but another one
>
>
>    FCrit.Acquire;
>    FRequest:=ARequest;
>    TThread.Synchronize(nil, at SyncNewConnection);
>    LocCommandQueue:=FCommandQueue;
>    FCrit.Release;
>    if LocCommandQueue<>nil then...
>
>
>
> WDYT?

>From the info you gave me, I think this could work, yes.

Care should be taken that the critical lock does not cause a deadlock, 
I don't know what happens in SyncNewConnection.

>
> Am I right thinking that, even if several copies of the above method are 
> running, each will get it's own local variables, so the LocCommandQueue 
> variable (as well as the other locals) won't be clobbered by another copy?
> Or should I declare them as threadvar?

If FCommandQueue is the only thing that is set during SyncNewConnection
based on TRequest, then yes. For safety, I would Nil the FCommandQueue after
assigning it to locCommandQueue.

Michael.


More information about the fpc-pascal mailing list