Ticket #5843 (closed defect: fixed)
race condition in cached_method (should not be shared between instances)
| Reported by: | nthiery | Owned by: | mhansen |
|---|---|---|---|
| Priority: | critical | Milestone: | sage-4.3.2 |
| Component: | misc | Keywords: | race condition, cached_method, cache |
| Cc: | sage-combinat, mhansen | Author(s): | Willem Jan Palenstijn |
| Report Upstream: | N/A | Reviewer(s): | Tim Dumol |
| Merged in: | sage-4.3.2.alpha0 | Work issues: |
Description
Consider the following class (simplified from a real life example, after 3 hours of heisenbug debugging):
class bla:
def __init__(self, value):
self.value = value
#
@cached_method
def f(self, x):
return self.value
The method f ignores its input, and should return self.value:
sage: x = bla(1) sage: y = bla(2) sage: x.f(None) 1 sage: y.f(None) 2
Then, y.f(x.f) should ignore the inner x.f and return 2. It does not:
sage: sage: y.f(x.f) 1
The reason is that x.f and y.f, and all other instances of bla share the same cached_method object.
sage: x.f is y.f True sage: x.f is x.__class__.f True
and the _instance field is set to the latest instance for which this method has been queried:
sage: yf = y.f sage: yf._instance is y True sage: x.f Cached version of <function f at 0xb532d84> sage: yf._instance is y False sage: yf._instance is x True
Most of the time things work well, but there can be race conditions, as in the example above.
Nicolas and Florent
Attachments
Change History
Note: See
TracTickets for help on using
tickets.

