Incorrect AllOf Condition handling
In the process of adding events to a Condition's self._events, any processed event is checked. In checking, the Condition is evaluated. In evaluating, count (the number of processed events) is compared to len(events) (really len(self._events), the number of added events). Therefore, if the first event is already processed when the Condition is created, the number of added events and the number of processed events will both be 1 when self.all_events is called for the first time. This results in self.all_events returning True and the Condition succeeding before it checks if any other event is processed.
The example below illustrates an "and" Condition succeeding with only one of its two events having been processed.
def test(env): timer1 = env.timeout(1) timer2 = env.timeout(2) anything = env.event() yield timer1 yield timer2 ret = yield timer1 & anything assert timer1.processed == True assert anything.triggered == False assert ret == {timer1: None} print('Condition met at %d' % env.now) import simpy env = simpy.Environment() proc = env.process(test(env)) env.run(until=5) print('Simulation ended at %d' % env.now)
The Condition should never succeed, so test should never resume from its last yield.
Expected output:
Simulation ended at 5
Actual output:
Condition met at 2 Simulation ended at 5
I believe this to be a bug, though I am new to SimPy. I see that in all SimPy examples, a Condition's events are created nearly immediately prior to the creation of the Condition, so maybe this isn't usually a problem. Nevertheless, this does not seem like intended behavior, and a solution does not seem complex.
I have attached my own temporary solution in which the total number of events passed to the Condition is calculated in the Condition's init and is checked against the length of events in the self.all_events method, in order to confirm that all events have been added to self._events. However, I don't know that this is the best way to fix the issue, or that my fix doesn't introduce any other issues. I hope that the issue is resolved soon, as I will be shipping a product that uses SimPy and would like the SimPy source to be official.
SimPy 3.0.5, Python 3.3.5, Windows 7 64-bit
Comments (6)
-
-
-
assigned issue to
-
assigned issue to
-
Hi Jan,
I could not wait with a fix. See pull request #33. I didn't use your approach because there was another hidden bug. It would be nice, if you could try the pull request and report back if it fixes your problem.
Cheers, Ontje
-
Thanks for the quick response. The bug never manifested itself in a problem on my end, so this fixes the issue as far as I'm concerned. I'll be sure to let you know if I run into anything else in my project.
I'm new to Bitbucket, should I mark the issue as resolved or take any further action?
-
The issue will be automatically closed when ontje merges the pull request. Thanks for your report and your feedback! :-)
-
- changed status to resolved
Make conditions immutable. Also fixes issue
#52.→ <<cset 543f2876071b>>
- Log in to comment
Thanks for the report Jan. This does indeed look like a bug. I will try to come up with a fix tomorrow.