Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if entry exists in dictionary before delete #355

Merged
merged 1 commit into from Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion libp2p/pubsub/mcache.py
Expand Up @@ -96,7 +96,8 @@ def shift(self) -> None:
last_entries: List[CacheEntry] = self.history[len(self.history) - 1]

for entry in last_entries:
del self.msgs[entry.mid]
if entry.mid in self.msgs:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a minimal improvement but you can also do self.msgs.pop(encry.mid, None). Slightly more robust as it's also threadsafe though admittedly that probably doesn't matter much here.

del self.msgs[entry.mid]

i: int = len(self.history) - 2

Expand Down
6 changes: 4 additions & 2 deletions libp2p/stream_muxer/mplex/mplex.py
Expand Up @@ -297,7 +297,8 @@ async def _handle_close(self, stream_id: StreamID) -> None:
# the entry of this stream, to avoid others from accessing it.
if is_local_closed:
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 with self.streams_lock:
Expand All @@ -315,7 +316,8 @@ async def _handle_reset(self, stream_id: StreamID) -> None:
if not stream.event_local_closed.is_set():
stream.event_local_closed.set()
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:
if not self.event_shutting_down.is_set():
Expand Down
3 changes: 2 additions & 1 deletion libp2p/stream_muxer/mplex/mplex_stream.py
Expand Up @@ -180,7 +180,8 @@ async def close(self) -> None:
if _is_remote_closed:
# Both sides are closed, we can safely remove the buffer from the dict.
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:
"""closes both ends of the stream tells this remote side to hang up."""
Expand Down