Skip to content

Fix dangling pointer after client.close #527

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

Conversation

matias-martini
Copy link
Contributor

@matias-martini matias-martini commented Mar 11, 2023

Context

Hey there!

Firstly, I want to express my appreciation for the hard work and dedication that you have put into maintaining this project. Your efforts have been incredibly valuable to me and many others in the community.

Well, as reported in the issue #519, we ran into an unexpected Segmentation Fault Error while verifying whether a client was dead after closing it. Upon investigation, we discovered (following the leads of @aharpervc ) that the root cause of the issue was that by calling dbclose, we were deallocating memory used by the client structure without setting the pointer to NULL after closing the client connection.
This led to a dangling pointer, which ultimately resulted in the Segmentation Fault error when we tried to check whether the client was dead using TDS dbdead (here).

To prevent this issue from happening again, I've made the necessary updates to ensure that the client pointer is set to NULL after every dbclose. This fix will help us avoid any dangling pointers and keep our code running smoothly.

I also tested this locally both before and after building the gem with the changes and everything is working like a charm now!

client = TinyTds::Client.new(**ops)
client.close
client.dead? # --> True

I wanted to note that while another solution would have checked whether the client was closed (cwrap->closed) before calling dbdead, I didn't feel comfortable leaving any dangling pointers. 😅

Let me know if you have any questions or concerns.

Changelog

  • Considered testing client.dead? after closing the connection in the Client test cases.
  • Fixed dangling pointers after calling dbclose.

@matias-martini matias-martini changed the title Fix dangling pointer after close Fix dangling pointer after client.close Mar 11, 2023
@aharpervc
Copy link
Contributor

This looks like a reasonable fix to me. Can you also update the changelog file, by adding a bullet point in the "unreleased" section the way it's been done in the past?

@matias-martini matias-martini force-pushed the fix-dangling-pointer-after-close branch from 06c5190 to 2b88604 Compare March 18, 2023 04:15
@matias-martini
Copy link
Contributor Author

Great, I've made the changes you requested! Please take a look and let me know if everything looks good to you. If there's anything else you need me to do or if you have any further suggestions, feel free to let me know! 😬

@matias-martini
Copy link
Contributor Author

@aharpervc I've just solved some merge conflicts. Let me know I you need me to do something else to merge this PR 🙃

@aharpervc
Copy link
Contributor

I'd like to have our CI jobs run for it, but I'm not seeing that integration show up on this PR. I'm guessing that's due to the "Build forked pull requests" setting being turned off at the moment... I'll turn it on and see what happens.

@aharpervc
Copy link
Contributor

You might need to push to this branch again, can you try that? For example, you can probably squash this branch down to 1 commit & force push.

Matias Martini added 2 commits May 19, 2023 00:55
We recently encountered an unexpected Segmentation Fault Error when
checking whether a client was dead after closing it.

To consider this in the future, I've updated the test cases for the
client to include this scenario, so we can catch any issues earlier on.
We recently encountered an unexpected Segmentation Fault Error while
checking if a client was dead after closing it. Upon investigation, we
discovered that the issue was caused by deallocating all memory used by
the client structure without setting the pointer to NULL after closing
the client connection through dbclose. This resulted in a dangling
pointer, which led to the Segmentation Fault when we tried to check
whether the client was dead using TDS dbdead.

To prevent this issue from occurring again, I have made the necessary
updates to set the client pointer to NULL after every dbclose. This will
ensure that we avoid any dangling pointers and keep our code running
smoothly.
@matias-martini matias-martini force-pushed the fix-dangling-pointer-after-close branch from ac57db1 to 7fe8c8e Compare May 19, 2023 03:56
@matias-martini
Copy link
Contributor Author

@aharpervc all tests passed! Sorry for the long wait btw, busy month 😅

@aharpervc aharpervc requested a review from andyundso May 19, 2023 15:14
@aharpervc aharpervc self-requested a review May 19, 2023 15:14
Copy link
Contributor

@aharpervc aharpervc left a comment

Choose a reason for hiding this comment

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

This seems fine to me. I think it's probably best as just 1 commit in the master history, any objections to the "squash & merge" feature?

Copy link
Member

@andyundso andyundso left a comment

Choose a reason for hiding this comment

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

Given the context of the issue and the PR description, the changes also look good to me.

@matias-martini nicely applied TDD here (first committing a failing test before committing the fix), so merge-commit and rebase would be good. Still, squashing gives the advantage that you find the fix and the test immediately when browsing the Git history at a later point.

@matias-martini
Copy link
Contributor Author

matias-martini commented May 19, 2023

@aharpervc @andyundso Totally onboard with 'Squash and merge' for a clearer history! 😄

Thanks again for taking the time to review the changes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants