Skip to content

Commit 0ec3f3f

Browse files
committed
Add security to XAsyncSocket lib
1 parent 36058ee commit 0ec3f3f

File tree

1 file changed

+42
-45
lines changed

1 file changed

+42
-45
lines changed

MicroWebSrv2/libs/XAsyncSockets.py

+42-45
Original file line numberDiff line numberDiff line change
@@ -40,66 +40,58 @@ def __init__(self) :
4040
# ------------------------------------------------------------------------
4141

4242
def _incThreadsCount(self) :
43-
self._opLock.acquire()
44-
self._threadsCount += 1
45-
self._opLock.release()
43+
with self._opLock :
44+
self._threadsCount += 1
4645

4746
# ------------------------------------------------------------------------
4847

4948
def _decThreadsCount(self) :
50-
self._opLock.acquire()
51-
self._threadsCount -= 1
52-
self._opLock.release()
49+
with self._opLock :
50+
self._threadsCount -= 1
5351

5452
# ------------------------------------------------------------------------
5553

5654
def _addSocket(self, socket, asyncSocket) :
5755
if socket :
5856
socketno = socket.fileno()
59-
self._opLock.acquire()
60-
ok = (socketno not in self._asyncSockets)
61-
if ok :
62-
self._asyncSockets[socketno] = asyncSocket
63-
self._opLock.release()
64-
return ok
57+
with self._opLock :
58+
if socketno not in self._asyncSockets :
59+
self._asyncSockets[socketno] = asyncSocket
60+
return True
6561
return False
6662

6763
# ------------------------------------------------------------------------
6864

6965
def _removeSocket(self, socket) :
7066
if socket :
7167
socketno = socket.fileno()
72-
self._opLock.acquire()
73-
ok = (socketno in self._asyncSockets)
74-
if ok :
75-
del self._asyncSockets[socketno]
76-
if socket in self._readList :
77-
self._readList.remove(socket)
78-
if socket in self._writeList :
79-
self._writeList.remove(socket)
80-
self._opLock.release()
81-
return ok
68+
with self._opLock :
69+
if socketno in self._asyncSockets :
70+
del self._asyncSockets[socketno]
71+
if socket in self._readList :
72+
self._readList.remove(socket)
73+
if socket in self._writeList :
74+
self._writeList.remove(socket)
75+
return True
8276
return False
8377

8478
# ------------------------------------------------------------------------
8579

8680
def _socketListAdd(self, socket, socketsList) :
87-
self._opLock.acquire()
88-
ok = (socket.fileno() in self._asyncSockets and socket not in socketsList)
89-
if ok :
90-
socketsList.append(socket)
91-
self._opLock.release()
92-
return ok
81+
with self._opLock :
82+
if socket.fileno() in self._asyncSockets and socket not in socketsList :
83+
socketsList.append(socket)
84+
return True
85+
return False
9386

9487
# ------------------------------------------------------------------------
9588

9689
def _socketListRemove(self, socket, socketsList) :
97-
self._opLock.acquire()
98-
ok = (socket.fileno() in self._asyncSockets and socket in socketsList)
99-
if ok :
100-
socketsList.remove(socket)
101-
self._opLock.release()
102-
return ok
90+
with self._opLock :
91+
if socket.fileno() in self._asyncSockets and socket in socketsList :
92+
socketsList.remove(socket)
93+
return True
94+
return False
10395

10496
# ------------------------------------------------------------------------
10597

@@ -116,22 +108,27 @@ def _processWaitEvents(self) :
116108
except KeyboardInterrupt as ex :
117109
raise ex
118110
except :
119-
print('a')
120111
continue
121112
if not self._processing :
122113
break
123114
for socketsList in ex, wr, rd :
124115
for socket in socketsList :
125-
asyncSocket = self._asyncSockets.get(socket.fileno(), None)
126-
if asyncSocket and self._socketListAdd(socket, self._handlingList) :
127-
if socketsList is ex :
128-
asyncSocket.OnExceptionalCondition()
129-
elif socketsList is wr :
130-
self._socketListRemove(socket, self._writeList)
131-
asyncSocket.OnReadyForWriting()
132-
else :
133-
asyncSocket.OnReadyForReading()
134-
self._socketListRemove(socket, self._handlingList)
116+
with self._opLock :
117+
asyncSocket = self._asyncSockets.get(socket.fileno(), None)
118+
if asyncSocket :
119+
if self._socketListAdd(socket, self._handlingList) :
120+
if socketsList is ex :
121+
asyncSocket.OnExceptionalCondition()
122+
elif socketsList is wr :
123+
self._socketListRemove(socket, self._writeList)
124+
asyncSocket.OnReadyForWriting()
125+
else :
126+
asyncSocket.OnReadyForReading()
127+
self._socketListRemove(socket, self._handlingList)
128+
else :
129+
self._socketListRemove(socket, self._readList)
130+
self._socketListRemove(socket, self._writeList)
131+
socket.close()
135132
sec = perf_counter()
136133
if sec > timeSec + XAsyncSocketsPool._CHECK_SEC_INTERVAL :
137134
timeSec = sec

0 commit comments

Comments
 (0)