Skip to content

Reorder typeof checks to avoid infinite loops on StructrefProxy __hash__ - #8241

Merged
esc merged 5 commits into
numba:mainfrom
DannyWeitekamp:main
Sep 7, 2022
Merged

esc merged 5 commits into
numba:mainfrom
DannyWeitekamp:main

Conversation

@DannyWeitekamp

@DannyWeitekamp DannyWeitekamp commented Jul 13, 2022 •

Copy link
Copy Markdown
Contributor

I was having an issue where I couldn't implement __hash__ for a StructRefProxy by calling into a dispatcher that takes the structref as an argument because the dispatcher calls typeof on the proxy instance which in turn calls cffi_utils.is_cffi_func() which calls __hash__ on the instance creating an infinite loop.

I'm not sure how often typeof is called (I suspect a lot), but my guess is that putting the _numba_type_ check first might provide a small compile-time performance benefit. I would imagine objects with _numba_type_ are more common than cffi functions and thus should be checked first.

I don't have a ton of time, but I'm happy to add a test if ya'll think it would be worth it.

@stuartarchibald stuartarchibald left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch. The proposed change seems like a good idea in general, typeof does indeed get called a lot and CFFI use is probably less likely than something with a _numba_type_ attribute! I think adding a test would be a good idea as it would help prevent potential future changes causing the same problem.

I presume the issue you saw was something along these lines:

from numba import njit, typeof
from numba.core import types
from numba.experimental import structref


@structref.register
class MyStructType(types.StructRef):
    def preprocess_fields(self, fields):
        return tuple()


class MyStruct(structref.StructRefProxy):
    def __new__(cls):
        return structref.StructRefProxy.__new__(cls)

    def __hash__(self):
        return MyStructHashImpl(self)


@njit
def MyStructHashImpl(self):
    return 10


structref.define_proxy(MyStruct, MyStructType, [])


struct_inst = MyStruct()
typeof(struct_inst)

which overflows the stack due to recursion, if so, perhaps this could be used as a basis for a test?

Many thanks.

@stuartarchibald stuartarchibald added 4 - Waiting on author Waiting for author to respond to review Effort - medium Medium size effort needed and removed 3 - Ready for Review labels Jul 18, 2022
@stuartarchibald stuartarchibald added this to the Numba 0.57 RC milestone Jul 18, 2022
@stuartarchibald stuartarchibald self-assigned this Jul 19, 2022

@sklam sklam left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just need a test as @stuartarchibald suggested and the small style nitpicking below

Comment thread numba/core/typing/typeof.py Outdated
@gmarkall

Copy link
Copy Markdown
Member

Also needs a quick flake8 fix.

@stuartarchibald stuartarchibald left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch, two minor suggestions/fixes else looks good.

Comment thread numba/tests/test_struct_ref.py Outdated
Comment thread numba/tests/test_struct_ref.py Outdated
DannyWeitekamp and others added 2 commits August 20, 2022 13:13
Co-authored-by: stuartarchibald <stuartarchibald@users.noreply.github.com>
Co-authored-by: stuartarchibald <stuartarchibald@users.noreply.github.com>

@stuartarchibald stuartarchibald left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch and fixes.

@stuartarchibald stuartarchibald added 4 - Waiting on CI Review etc done, waiting for CI to finish and removed 4 - Waiting on author Waiting for author to respond to review labels Aug 23, 2022
@stuartarchibald

Copy link
Copy Markdown
Contributor

/AzurePipelines run

@azure-pipelines

Copy link
Copy Markdown
Contributor
Azure Pipelines successfully started running 1 pipeline(s).

@stuartarchibald stuartarchibald added 5 - Ready to merge Review and testing done, is ready to merge and removed 4 - Waiting on CI Review etc done, waiting for CI to finish labels Sep 5, 2022
@esc

esc commented Sep 7, 2022

Copy link
Copy Markdown
Member

Thank you for the patch!

@esc
esc merged commit 301aa82 into numba:main Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

5 - Ready to merge Review and testing done, is ready to merge Effort - medium Medium size effort needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants