Strange delay in TROHttpThread.IntExecute

Hello

I have a HttpServer based on TROHttpServerApiServer.
When testing the configuration “localHttpServer + localHttpClient”, I met strange delays (15-20ms) even for dummy calls that “do nothing”.

My further research showed that the reason is in TROHttpThread.IntExecute:

1 Removing the sleep(1) resolves the issue

procedure TROHttpThread.IntExecute;
begin
  ...
      else
        break; // unknown error
      end;
      // sleep(1); // <--- The patched line
    end;
  finally
    FreeMem(lRequestBuffer);
  end;
end;

As I see the idea of the sleep(1) is to switch execution from the current thread.
Q1: Does removing the sleep(1) break something?

2 As well I noticed strange sleep(100) in

    ERROR_IO_PENDING: begin
      // If the function is being used asynchronously, a return value of ERROR_IO_PENDING
      // indicates that the next request is not yet ready and will be retrieved later
      // through normal overlapped I/O completion mechanisms.
      sleep(100);
    end;

I don’t see the reason why to sleep here if the call

  lRes := HttpServerAPI.ReceiveHttpRequest(fOwner.fReqQueueHandle, ...,lReceivedBytes);

is synchronous and will block execution anyway.
Q2: Could sleep(100) be omitted here?

Best regards
Alexander

you are right.
sleep(1); can be replaced here with

  {$IFDEF DelphiXEUP}
  TThread.Yield; //xe+
  {$ELSE}
    {$IFDEF FPC}
    TThread.Yield; //fpc
    {$ELSE}
    SwitchToThread; //d7-d2010
    {$ENDIF}
  {$ENDIF}

try to replace sleep(100) with

if GetCurrentThreadID <> MainThreadID then sleep(100);

Thanks, logged as bugs://85703

TThread.Yield; //xe+

Q1: Why should the execution yield to another thread here? As I see the execution is blocked by ReceiveHttpRequest when there is no request in the internal queue, doesn’t it?

if GetCurrentThreadID <> MainThreadID then sleep(100);

Q2: Given that in ~100% of times the code is executed in non-main thread this change does nothing. For what the sleep is required here?

bugs://85703 got closed with status fixed.

Server should have possibility to manipulate with threads via SoftTerminate.
w/o TThread.Yield it could be problematic on some pc

you are right here, it does nothing.
Have you caught ERROR_IO_PENDING error?
seems, it should be raised only for asynchronous calls …

Hi Evgeny,

I’m still not convinced that all these sleep(1)/Yield/... is a good approach to co-working the server and its threads.
IMO using of Sync objects in TROHttpServerApiServer.UpdateThreads and TROHttpThread.IntExecute to manage threads would be more robust and much better in the terms of performance.

No, I haven’t because it’s impossible. HttpServerAPI.ReceiveHttpRequest() in TROHttpThread.IntExecute is always called synchronously and it doesn’t make sense to handle ERROR_IO_PENDING here.
I think this case could be removed at all to avoid confusing.

Best regards
Alexander

Can you retest original issue, pls?
with sleep(1) you had 15-20ms delay.
What delay you have with Yield ?

I removed Yield at all. Now my tests show ~1ms.
Thank you for support.