Skip to content

Commit

Permalink
Bound the number of consecutive discards we allow, terminating all re…
Browse files Browse the repository at this point in the history
…jection sampling
  • Loading branch information
DRMacIver committed Jul 4, 2019
1 parent deb3eb8 commit 87c40e0
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions hypothesis-python/src/hypothesis/internal/conjecture/data.py
Expand Up @@ -752,6 +752,7 @@ def __init__(self, max_length, draw_bytes, observer=None):
self.draw_times = []
self.max_depth = 0
self.has_discards = False
self.consecutive_discard_counts = []

self.__result = None

Expand Down Expand Up @@ -862,15 +863,34 @@ def start_example(self, label):
if self.depth > self.max_depth:
self.max_depth = self.depth
self.__example_record.start_example(label)
self.consecutive_discard_counts.append(0)

def stop_example(self, discard=False):
if self.frozen:
return
self.consecutive_discard_counts.pop()
if discard:
self.has_discards = True
self.depth -= 1
assert self.depth >= -1
self.__example_record.stop_example(discard)
if self.consecutive_discard_counts:
# We block long sequences of discards. This helps us avoid performance
# problems where there is rejection sampling. In particular tests which
# have a very small actual state space but use rejection sampling will
# play badly with generate_novel_prefix() in DataTree, and will end up
# generating very long tests with long runs of the rejection sample.
if discard:
self.consecutive_discard_counts[-1] += 1
# 20 is a fairly arbitrary limit chosen mostly so that all of the
# existing tests passed under it. Essentially no reasonable
# generation should hit this limit when running in purely random
# mode, but unreasonable generation is fairly widespread, and our
# manipulation of the bitstream can make it more likely.
if self.consecutive_discard_counts[-1] > 20:
self.mark_invalid()
else:
self.consecutive_discard_counts[-1] = 0

def note_event(self, event):
self.events.add(event)
Expand Down

0 comments on commit 87c40e0

Please sign in to comment.