LuaScript Libcurl error while using HttpPost in OnStoredInstance

I have a very simple lua script in my orthanc setup. I’m pasting the code below for reference.

The script is working in the sense that my api is receiving the HttpPost request and the content is as expected. However, on the orthanc side the HttpPost request hangs and eventually results in a libcurl error. I’ll paste the logs below the code.

My api is a simple Python Flask API that is returning a basic response message with a 200 status code.

Is there something I need to change to make the program not hang?

Lua Script (with a my-app url)

function OnStoredInstance(instanceId, tags, metadata)
print('DICOM instance received: ’ … instanceId)

local url = “https://my-app.com/orthance/instance
local body = RestApiGet(‘/instances/’ … instanceId)
local headers = {
[“content-type”] = “application/json”,
[“Expect”] = “”
}
HttpPost(url, body, headers)

print(“DICOM instance sent to my-app”)
end

Orthanc Error Logs

W1109 07:33:12.861521 LuaContext.cpp:93] Lua says: DICOM instance received: f624ab5f-0ed49e99-d38a1b48-c07a5acb-7e7f88aa

  • Expire in 0 ms for 6 (transfer 0x466afe0)
  • Expire in 60000 ms for 8 (transfer 0x466afe0)
  • Expire in 60000 ms for 2 (transfer 0x466afe0)
  • Trying 34.83.64.96…
  • TCP_NODELAY set
  • Expire in 200 ms for 4 (transfer 0x466afe0)
  • Connected to my-api.com (34.83.64.96) port 443 (#157)
  • ALPN, offering http/1.1
  • SSL re-using session ID
  • SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256
  • ALPN, server accepted to use http/1.1
  • Server certificate:
  • start date: Oct 29 11:13:00 2020 GMT
  • expire date: Jan 27 11:13:00 2021 GMT
  • issuer: C=US; O=Let’s Encrypt; CN=Let’s Encrypt Authority X3
  • SSL certificate verify result: unable to get local issuer certificate (20), continuing anyway.

POST /orthance/instance HTTP/1.1
Host: my-api.com
Accept: /
content-type: application/json
Content-Length: 451

  • upload completely sent off: 451 out of 451 bytes
  • old SSL session ID is stale, removing
  • Operation timed out after 60000 milliseconds with 0 bytes received
  • Closing connection 157
    Error E1109 07:34:12.867312 HttpClient.cpp:57] Error code 28 in libcurl: Timeout was reached
    I1109 07:34:12.867348 HttpClient.cpp:954] HTTP status code 0 in 60000 ms after POST request on: My-Api: The Ultimate SMTP Relay Service
    I1109 07:34:12.867363 HttpClient.cpp:960] cURL status code: 28
    E1109 07:34:12.867372 OrthancException.h:76] Error in the network protocol: libCURL error: Timeout was reached
    E1109 07:34:12.867407 LuaContext.cpp:303] Lua: Error in HttpPost() or HttpPut() for URL My-Api: The Ultimate SMTP Relay Service
    W1109 07:34:12.867421 LuaContext.cpp:93] Lua says: DICOM instance sent to radimal reporter

Here is the important part of your logs:

  • Operation timed out after 60000 milliseconds with 0 bytes received
    Error E1109 07:34:12.867312 HttpClient.cpp:57] Error code 28 in libcurl: Timeout was reached

This timeout after 60 seconds indicates a problem in your Flask application (the latter accepts the connection, but stalls and returns nothing).

If you expect further support, share a full minimal working example (the source code of your Flask application able to run on the localhost is missing to assess reproducibility):
https://book.orthanc-server.com/users/support.html#discussing-a-minimal-working-example

Hi Oliver,

While your lua script is being executed, there’s a lock that is acquired in Orthanc.
While your lua script calls your flask server, this lock is still locked.
Your flask server calls the Orthanc API which, in turns, tries to lock the lua lock which is already locked → deadlock.

Solution,
Your flask server should queue events and process them asynchronously.

HTH

Alain

Screen Shot 2020-11-11 at 6.40.36 AM.png

Hello Alain,

Thank you for this. I wasn’t aware of the lock mechanic, but it makes perfect sense. I’ve fixed the issue now.

Best,
Oliver

Screen Shot 2020-11-11 at 6.40.36 AM.png