Skip to content

Commit

Permalink
Fixe a no-threaded bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jczic committed Mar 27, 2024
1 parent 81f66d0 commit 4a7f599
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions MicroWebSrv2/libs/XAsyncSockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,20 @@ def __init__(self) :
# ------------------------------------------------------------------------

def _addSocket(self, socket, asyncSocket) :
if socket :
socketno = socket.fileno()
if socket and asyncSocket :
with self._opLock :
if socketno not in self._asyncSockets :
self._asyncSockets[socketno] = asyncSocket
if socket not in self._asyncSockets :
self._asyncSockets[socket] = asyncSocket
return True
return False

# ------------------------------------------------------------------------

def _removeSocket(self, socket) :
if socket :
socketno = socket.fileno()
with self._opLock :
if socketno in self._asyncSockets :
del self._asyncSockets[socketno]
if socket in self._asyncSockets :
del self._asyncSockets[socket]
if socket in self._readList :
self._readList.remove(socket)
if socket in self._writeList :
Expand Down Expand Up @@ -135,26 +133,26 @@ def jobReadyForReading(args) :
if sock == self._udpSockEvt :
self._udpSockEvt.recv_into(udpSockEvtBuf)
else :
asyncSocket = self._asyncSockets.get(sock.fileno())
asyncSocket = self._asyncSockets.get(sock)
if asyncSocket and asyncSocket.GetSocketObj() == sock :
if self._socketListAdd(sock, self._handlingList) :
if socketsList is rd :
if self._microWorkers :
self._microWorkers.AddJob(jobReadyForReading, (asyncSocket, sock))
else :
jobReadyForReading(asyncSocket)
jobReadyForReading((asyncSocket, sock))
elif socketsList is wr :
self._socketListRemove(sock, self._writeList)
if self._microWorkers :
self._microWorkers.AddJob(jobReadyForWriting, (asyncSocket, sock))
else :
jobReadyForWriting(asyncSocket)
jobReadyForWriting((asyncSocket, sock))
else :
self._removeSocket(sock)
if self._microWorkers :
self._microWorkers.AddJob(jobExceptionalCondition, (asyncSocket, sock))
else :
jobExceptionalCondition(asyncSocket)
jobExceptionalCondition((asyncSocket, sock))
else :
self._removeSocket(sock)
sock.close()
Expand Down Expand Up @@ -337,7 +335,6 @@ def _close(self, closedReason=XClosedReason.Error, triggerOnClosed=True) :
self._socket.close()
except :
pass
self._socket = None
if self._recvBufSlot is not None :
self._recvBufSlot.Available = True
self._recvBufSlot = None
Expand Down Expand Up @@ -691,7 +688,7 @@ def OnReadyForReading(self) :
if not self.IsSSL or self._socket.pending() == 0 :
return
else :
self._asyncSocketsPool.NotifyNextReadyForReading(self, False)
self._close(XClosedReason.ClosedByHost)
return

# ------------------------------------------------------------------------
Expand Down Expand Up @@ -844,13 +841,15 @@ def StartSSL( self,
try :
self._asyncSocketsPool.NotifyNextReadyForWriting(self, False)
self._asyncSocketsPool.NotifyNextReadyForReading(self, False)
self._asyncSocketsPool.RemoveAsyncSocket(self)
self._socket = ssl.wrap_socket( self._socket,
keyfile = keyfile,
certfile = certfile,
server_side = server_side,
cert_reqs = cert_reqs,
ca_certs = ca_certs,
do_handshake_on_connect = False )
self._asyncSocketsPool.AddAsyncSocket(self)
except Exception as ex :
raise XAsyncTCPClientException('StartSSL : %s' % ex)
self._doSSLHandshake()
Expand All @@ -867,9 +866,11 @@ def StartSSLContext(self, sslContext, serverSide=False) :
try :
self._asyncSocketsPool.NotifyNextReadyForWriting(self, False)
self._asyncSocketsPool.NotifyNextReadyForReading(self, False)
self._asyncSocketsPool.RemoveAsyncSocket(self)
self._socket = sslContext.wrap_socket( self._socket,
server_side = serverSide,
do_handshake_on_connect = False )
self._asyncSocketsPool.AddAsyncSocket(self)
except Exception as ex :
raise XAsyncTCPClientException('StartSSLContext : %s' % ex)
self._doSSLHandshake()
Expand Down

0 comments on commit 4a7f599

Please sign in to comment.