Commit 788f18d2 authored by 4ast's avatar 4ast

Merge pull request #351 from iovisor/iteritems

Fix python map.items() racing with bpf delete
parents d366b0e0 efbc28c9
...@@ -286,6 +286,30 @@ class BPF(object): ...@@ -286,6 +286,30 @@ class BPF(object):
if res < 0: if res < 0:
raise KeyError raise KeyError
# override the MutableMapping's implementation of these since they
# don't handle KeyError nicely
def itervalues(self):
for key in self:
# a map entry may be deleted in between discovering the key and
# fetching the value, suppress such errors
try:
yield self[key]
except KeyError:
pass
def iteritems(self):
for key in self:
try:
yield (key, self[key])
except KeyError:
pass
def items(self):
return [item for item in self.iteritems()]
def values(self):
return [value for value in self.itervalues()]
def clear(self): def clear(self):
# default clear uses popitem, which can race with the bpf prog # default clear uses popitem, which can race with the bpf prog
for k in self.keys(): for k in self.keys():
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment