7

I have an annoying problem. I have this simple server code (let's say):

#!/usr/bin/env python3
import wsgiref.simple_server

def my_func(env, start_response):
  start_response('200 OK', [])
  return [''.encode()]

server = wsgiref.simple_server.make_server(
  '0.0.0.0',
  19891,
  my_func,
)

server.serve_forever()

However, 1 time from 5 tries (so approx 20% of requests) are served very-very slow. When I interrupt the server processing when this huge delay is in place I'm always getting the below exception:

Exception happened during processing of request from ('192.168.1.100', 3540)
Traceback (most recent call last):
  File "/usr/lib/python3.5/socketserver.py", line 313, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python3.5/socketserver.py", line 341, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python3.5/socketserver.py", line 354, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python3.5/socketserver.py", line 681, in __init__
    self.handle()
  File "/usr/lib/python3.5/wsgiref/simple_server.py", line 119, in handle
    self.raw_requestline = self.rfile.readline(65537)
  File "/usr/lib/python3.5/socket.py", line 575, in readinto
    return self._sock.recv_into(b)
KeyboardInterrupt

Do you have any idea how to avoid this annoying thing? Or what could be the reason behind this behavior?

Update1: I have tried TCP_NODELAY with modifying simple_server.py ->WSGIServer-> server_bind function as below:

def server_bind(self):
    """Override server_bind to store the server name."""
    import socket
    self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY,1)
    HTTPServer.server_bind(self)
    self.setup_environ()

Unfortunatelly no change :(

vpas
  • 513
  • 1
  • 5
  • 18

1 Answers1

0

You are using port number 3540, which is the default port for "TCP/UDP Port Finder". You can have a port collision with another service/daemon on your OS.

You can change to port 80, 8000, 8080… If you want a web server.

Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
  • What? No, I'm using / starting my server on port 19891. – vpas May 27 '17 at 20:20
  • @chameleon: OK, but you have this error: `Exception happened during processing of request from ('192.168.1.100', 3540)`. – Laurent LAPORTE May 27 '17 at 20:22
  • That's the clients port where is connecting to server... For example I have retried loading the server from chrome again, and the port is now 11071, however the issue is the same. – vpas May 27 '17 at 20:23