ponyorm / pony

Pony Object Relational Mapper
Apache License 2.0
3.58k stars 242 forks source link

wrong entry fetched from extractors_cache #675

Open elmjag opened 1 year ago

elmjag commented 1 year ago

When calling asttranslation.create_extractors() from HybridFuncMonad.call(), sometimes wrong entry is returned.

Thus wrong function AST object is used in subseqent call to translator.dispatch(). This bug.py demonstrates the problem. The script will normally exit with a traceback that looks something like this: https://gist.github.com/elmjag/b4205be63a23af8697a8236fb5faf6aa

The issue here is that HybridFuncMonad.call() uses function object's id() as key_code argument when calling create_extractors(). The same id() value can be reused for a different function object. Thus when calling create_extractors() for a new function objects, which happens to have same id() as previously cached function, you'll get entry for the old function from extractors_cache.

The situation where id() values are reused for different function objects is probably when old function is unrefed. Once the function object have been deleted, that memory address becomes available. That old memory is sometimes reused when creating a new, unrelated, function object.

This is also in line with the offical id() documentation

 Two objects with non-overlapping lifetimes may have the same id() value.

Note that the bug.py creates a new Database objects while it loops. I think this significat for this issue, as the Database object hold references to function objects, cached in extractors_cache. If same Database object is keep during the whole lifetime of the process, then this issues does not show up.