Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: prevent QHull message stream being closed twice #20611

Merged
merged 2 commits into from May 3, 2024

Conversation

andfoy
Copy link
Contributor

@andfoy andfoy commented Apr 29, 2024

Reference issue

This makes part of the current effort to remove the GIL from Python. After numpy/numpy#26348 and this PR, all the test suite passes successfully when ran locally.

What does this implement/fix?

This PR prevents releasing the MessageStream object in QHull twice. This may occur because the deallocation logic is duplicated both in the close and the __dealloc__ methods (there's a note mentioning that this is required to support PyPy). Before, a call to close followed by __dealloc__ would segfault, since close would have closed the MessageStream before __dealloc__ would have attempted to close it, thus trying to free a null pointer.

Additional information

This behaviour occurs independent of locking, since the closing attempt would have occurred without checking if self._qh != NULL

@github-actions github-actions bot added scipy.spatial Cython Issues with the internal Cython code base defect A clear bug or issue that prevents SciPy from being installed or used as expected labels Apr 29, 2024
@rgommers rgommers added the no-GIL Items related to supporting free-threaded builds of CPython label Apr 29, 2024
@rgommers rgommers added this to the 1.14.0 milestone May 1, 2024
Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

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

Thanks @andfoy. I don't think this fix is quite correct. self._messages is a separate object from self._qh, and can be non-null even if _qh is null. The logic in __init__ is:

        self._qh = NULL
        self._messages = MessageStream()
        # now do a bunch of things, including validation steps that can raise exceptions
        self._qh = malloc(...)

So this change would leave the instantiated MessageStream open in case an exception is raised before _qh is allocated, leaking the memory stream or file handle.

MessageStream.close actually looks safe to call multiple times. Is the problem you are seeing calling it twice and something inside the state of the MessageStream object not existing anymore, or is it that inside _Qhull self._messages itself no longer exists?

@andfoy
Copy link
Contributor Author

andfoy commented May 1, 2024

Thanks for bringing these points up @rgommers, now that you mentioned it, the MessageStream object pointer is being set to NULL at some point, which I found to be correlated to not be NULL when _qh is still valid.

I'll add a self._messages != NULL check then.

Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM now.

@rgommers rgommers changed the title FIX: Prevent QHull message stream being closed twice BUG: prevent QHull message stream being closed twice May 3, 2024
@rgommers rgommers merged commit d0ad5aa into scipy:main May 3, 2024
28 of 30 checks passed
@rgommers rgommers mentioned this pull request May 3, 2024
10 tasks
@andfoy andfoy deleted the prevent_qhull_message_close branch May 3, 2024 12:01
@tylerjereddy tylerjereddy added the backport-candidate This fix should be ported by a maintainer to previous SciPy versions. label May 3, 2024
@tylerjereddy tylerjereddy modified the milestones: 1.14.0, 1.13.1 May 5, 2024
tylerjereddy pushed a commit to tylerjereddy/scipy that referenced this pull request May 5, 2024
@tylerjereddy tylerjereddy removed the backport-candidate This fix should be ported by a maintainer to previous SciPy versions. label May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Cython Issues with the internal Cython code base defect A clear bug or issue that prevents SciPy from being installed or used as expected no-GIL Items related to supporting free-threaded builds of CPython scipy.spatial
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants