Skip to content

Conversation

@jimpo
Copy link
Contributor

@jimpo jimpo commented Aug 30, 2018

As noted by @ryanofsky and mitigated in #13894 and #14071, the index destructor previously had a memory violation by accessing its own address in call to UnregisterValidationInterface. This is problematic even if Start is never called on the index. The new runner class is an RAII-style interface for sync thread management and validation interface registration.

I'm not sure if this is a good idea or an unnecessary abstraction since the init module still has responsibility for starting/stopping via global pointers. I do think it's a better separation of concerns though. Looking for feedback before polishing up the PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

s/activing/activating

@maflcko
Copy link
Member

maflcko commented Nov 6, 2018

Doesn't compile on xenial gcc and clang-3.2?

@jimpo
Copy link
Contributor Author

jimpo commented Nov 8, 2018

@MarcoFalke That was weird. Fixed it.

@DrahtBot
Copy link
Contributor

DrahtBot commented Nov 9, 2018

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #10443 (Add fee_est tool for debugging fee estimation code by ryanofsky)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@bitcoin bitcoin deleted a comment from DrahtBot Nov 9, 2018
@bitcoin bitcoin deleted a comment from DrahtBot Nov 9, 2018
@bitcoin bitcoin deleted a comment from DrahtBot Nov 9, 2018
@bitcoin bitcoin deleted a comment from DrahtBot Nov 9, 2018
@jimpo jimpo force-pushed the index-runner branch 2 times, most recently from c7df45c to 0c67d14 Compare January 10, 2019 17:58
The index destructor previously had a memory violation by accessing
its own address in call to UnregisterValidationInterface. The new
runner class is an RAII-style interface for sync thread management
and validation interface registration.
Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

Looking at this I'm struggling to see what problem it is supposed to solve. It seems to be pulling code out of the BaseIndex::Start and BaseIndex::Stop methods into a separate IndexRunner::IndexRunner class. But it isn't clear what advantages this gives, since now for every BaseIndex instance that exists, there needs to be a parallel IndexRunner instance, and there needs to be a new global std::map, so you can find the IndexRunner pointer from the BaseIndex pointer.

This just seems more complicated than before, and of course requires more code. I see that this PR was created in response to a bug that either previously existed or previously could have existed. Is there still a bug that this can help prevent now? Or some other advantage this brings?

@jimpo
Copy link
Contributor Author

jimpo commented Mar 1, 2019

@ryanofsky Thanks for taking a look.

The bug definitely still exists, so one way or another the call to Stop must be removed in the destructor of BaseIndex. This manifests for me in #14121.

The idea here is to have an RAII wrapper in the places where the control flow is not managed through globals. Concretely, the only place we see some benefit is in the unit tests, where the explicit Stop call can be removed. The global map is just there because in bitcoind, the index lifecycle is manages through global functions.

The much simpler alternative here is to just require that Stop be called before the index goes out of scope as part of its contract, which is pretty much as it is now (except that the Stop in the destructor can be removed). Conceptually, the RAII wrapper seems cleaner and makes it easier to handle exceptions in general, though it doesn't seem to provide much value given how indexes are actually managed by bitcoind now.

@ryanofsky
Copy link
Contributor

Is this bug where UnregisterValidationInterface can't be called from the destructor because it accesses the virtual function table, which is not valid during destruction? If so, I believe this should have been fixed by 2196c51 in #13743. Right now if I comment out txindex.Stop() in txindex_tests.cpp, tests still seem to pass.

@jimpo
Copy link
Contributor Author

jimpo commented Mar 2, 2019

@ryanofsky Oh, amazing. While I still like this refactor aesthetically, it's not worth it. Thanks!

@jimpo jimpo closed this Mar 2, 2019
@jimpo jimpo deleted the index-runner branch March 2, 2019 21:54
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants