Merge pull request #355 from NIC619/check_before_del

Check if entry exists in dictionary before delete
This commit is contained in:
NIC Lin 2019-11-19 16:23:26 +08:00 committed by GitHub
commit 74198c70b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 4 deletions

View File

@ -96,7 +96,8 @@ class MessageCache:
last_entries: List[CacheEntry] = self.history[len(self.history) - 1] last_entries: List[CacheEntry] = self.history[len(self.history) - 1]
for entry in last_entries: for entry in last_entries:
del self.msgs[entry.mid] if entry.mid in self.msgs:
del self.msgs[entry.mid]
i: int = len(self.history) - 2 i: int = len(self.history) - 2

View File

@ -297,7 +297,8 @@ class Mplex(IMuxedConn):
# the entry of this stream, to avoid others from accessing it. # the entry of this stream, to avoid others from accessing it.
if is_local_closed: if is_local_closed:
async with self.streams_lock: async with self.streams_lock:
del self.streams[stream_id] if stream_id in self.streams:
del self.streams[stream_id]
async def _handle_reset(self, stream_id: StreamID) -> None: async def _handle_reset(self, stream_id: StreamID) -> None:
async with self.streams_lock: async with self.streams_lock:
@ -315,7 +316,8 @@ class Mplex(IMuxedConn):
if not stream.event_local_closed.is_set(): if not stream.event_local_closed.is_set():
stream.event_local_closed.set() stream.event_local_closed.set()
async with self.streams_lock: async with self.streams_lock:
del self.streams[stream_id] if stream_id in self.streams:
del self.streams[stream_id]
async def _cleanup(self) -> None: async def _cleanup(self) -> None:
if not self.event_shutting_down.is_set(): if not self.event_shutting_down.is_set():

View File

@ -180,7 +180,8 @@ class MplexStream(IMuxedStream):
if _is_remote_closed: if _is_remote_closed:
# Both sides are closed, we can safely remove the buffer from the dict. # Both sides are closed, we can safely remove the buffer from the dict.
async with self.muxed_conn.streams_lock: async with self.muxed_conn.streams_lock:
del self.muxed_conn.streams[self.stream_id] if self.stream_id in self.muxed_conn.streams:
del self.muxed_conn.streams[self.stream_id]
async def reset(self) -> None: async def reset(self) -> None:
"""closes both ends of the stream tells this remote side to hang up.""" """closes both ends of the stream tells this remote side to hang up."""