I am playing with https://github.com/arnoudbuzing/wolfram-server and I faced problems with requests that are slightly longer. I tried to narrow it down here.


This could be my failure to grasp sockets world, in such case I apologize. But I don't understand why SocketReadyQ returns False while it can be read?


    Print["Msg//Short: ", Short @ message["Data"]];
    Print["SocketReadyQ: ", SocketReadyQ @ message["SourceSocket"]];
    Print["Read more anyway: \n", Short @ ByteArrayToString @ SocketReadMessage @ message["SourceSocket"]];
    WriteString[message["SourceSocket"], ExportString[GenerateHTTPResponse[HTTPErrorResponse[500]], "HTTPResponse"]];

  "", <|"Headers" -> {"Expect" -> ""}, 
   "Body" -> StringRepeat["a", 10000]|>]

Msg//Short: POST / HTTP/1.1 Host: Accept: */... aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

SocketReadyQ: False

Read more anyway: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ... aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

So the problem is I am trying to handle a bigger request but it is split. I can't read blindly because SocketReadMessage will freeze when there's no more data and the point is to respond quickly.

If I Close then the next listener call will capture the next part of the original data but that does not help because it would be cumbersome to assemble and respond to client across many messages.


Did I miss something? What is a proper way to handle long requests?


I tried to SocketReadMessage wrraped with TimeConstrained as suggested, for the old api, here: https://mathematica.stackexchange.com/a/80053/5478

But the constraint needs to be at least 0.5s to be able to read if there's full buffer. So in case of all those short requests I will be adding 0.5s just to check if they are complete. That is too much.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.