Server no longer hangs on client disconnects (#217)

* Server no longer hangs on client disconnects

* Changed timeout and am now catching errors explicitly

Co-authored-by: KanjiEater <kanjieat3r@gmail.com>
This commit is contained in:
kanjieater 2020-12-28 15:57:22 -06:00 committed by GitHub
parent 9bbaf79575
commit 5386364c8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,7 +19,6 @@ import socket
from . import web, util from . import web, util
# #
# WebRequest # WebRequest
# #
@ -47,27 +46,38 @@ class WebClient:
return False return False
rlist, wlist = select.select([self.sock], [self.sock], [], 0)[:2] rlist, wlist = select.select([self.sock], [self.sock], [], 0)[:2]
self.sock.settimeout(5.0)
if rlist: if rlist:
msg = self.sock.recv(recvSize) while True:
if not msg: try:
self.close() msg = self.sock.recv(recvSize)
return False except (ConnectionResetError, socket.timeout):
self.close()
return False
if not msg:
self.close()
return False
self.readBuff += msg
req, length = self.parseRequest(self.readBuff)
if req is not None:
self.readBuff = self.readBuff[length:]
self.writeBuff += self.handler(req)
break
self.readBuff += msg
req, length = self.parseRequest(self.readBuff)
if req is not None:
self.readBuff = self.readBuff[length:]
self.writeBuff += self.handler(req)
if wlist and self.writeBuff: if wlist and self.writeBuff:
length = self.sock.send(self.writeBuff) try:
self.writeBuff = self.writeBuff[length:] length = self.sock.send(self.writeBuff)
if not self.writeBuff: self.writeBuff = self.writeBuff[length:]
if not self.writeBuff:
self.close()
return False
except:
self.close() self.close()
return False return False
return True return True
@ -200,3 +210,4 @@ class WebServer:
client.close() client.close()
self.clients = [] self.clients = []