Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route can't complete response until server request timeout if the client close the connection ungracefully #4457

Open
hellocome opened this issue Dec 23, 2024 · 0 comments

Comments

@hellocome
Copy link

When the client close the connection ungracefully - for example send RST without sending ACK/FIN, RouteDirectives can't complete a slow response until hitting the server request timeout, is this by design or bug?

Set Server request timeout to 5 seconds

 val slowResponseTime = 3.seconds

 path("abc") {
        val timeoutResponse = {
          HttpResponse(
            StatusCodes.EnhanceYourCalm,
            entity = formatCurrentMilliseconds() + ": Unable to serve response within time limit")
        }

        withRequestTimeoutResponse(request => {
          System.out.println(formatCurrentMilliseconds() + ": Unable to serve response within time limit = " + count.get())
          timeoutResponse
        }) {
          val slowFuture = {
            val futureResponse: Future[HttpResponse] = Future {
              Thread.sleep(slowResponseTime.toMillis) 
              System.out.println(formatCurrentMilliseconds() + ": response to abc is created")
              HttpResponse(entity = HttpEntity(ContentTypes.`text/html(UTF-8)`, "response to abc,it is slow"))
            }

            futureResponse
          }
          val response = slowFuture // very slow
          complete(response)
        }
      }

we can see debug log like, I would expect complete should be invoked if there is an error in the flow, Unable to serve response within time limit should not be displayed in this case or it should not wait until server request timeout

[DEBUG] [12/23/2024 10:11:24.485] [akka-http-server-system-akka.actor.internal-dispatcher-6] [akka://akka-http-server-system/system/IO-TCP/selectors/$a/0] New connection accepted
20241223 10:11:24:623: Get Request = 0
[DEBUG] [12/23/2024 10:11:26.488] [akka-http-server-system-akka.actor.internal-dispatcher-6] [akka://akka-http-server-system/system/IO-TCP/selectors/$a/1] Closing connection due to IO error java.io.IOException: An existing connection was forcibly closed by the remote host
20241223 10:11:27:635: response to abc is created
20241223 10:11:29:636: Unable to serve response within time limit

Python script to simulate ungraceful shutdown

import socket
import struct
import time

def send_http_request():
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    linger_struct = struct.pack('ii', 1, 0)
    client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, linger_struct)
    client_socket.connect(("localhost", 8080))
    request = "GET /abc HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"
    client_socket.send(request.encode())
    time.sleep(2)
    client_socket.close()
    if client_socket:
        client_socket.close()

if __name__ == "__main__":
    send_http_request()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant