Skip to content

Conversation

domob1812
Copy link
Contributor

This contains various small code cleanups that make the ZMQ code easier to read and maintain (at least in my opinion). The only functional change is that a potential memory leak is fixed that would have occured when a notifier is removed from the notifiers list after its callback function returned false (which is likely not relevant in practice but still a bug).

@domob1812
Copy link
Contributor Author

I split the patch into three commits which I think make sense logically and for the review. But of course I'm happy to squash any of the changes, or to revert parts of them (as code readability is a matter of taste).

@promag
Copy link
Contributor

promag commented Jul 24, 2018

Concept ACK.

From a quick view LGTM.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jul 27, 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:

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.

@domob1812
Copy link
Contributor Author

Rebased

@kostyantyn
Copy link
Contributor

What do you think about wrapping the code with zmq namespace and removing ZMQ/zmq prefix from class/function names?
Basically when the location of the file is a/b/c.cpp then we have:

namespace a {
namespace b {
class C {}
}
}

@domob1812
Copy link
Contributor Author

@kostyantyn: At least for now, there seems to be not much namespace usage at all in the Bitcoin code - so I'm not sure if we should change that lightly. Also, note that zmq is the namespace that is also used by ZMQ's own C++ language bindings; they are currently not used, but I proposed to change that (#13957).

Also, note that C++ namespaces are different from Java's, so that some people advise against nesting (e.g. exact correspondence to the directory structure as you suggest).

@laanwj
Copy link
Member

laanwj commented Aug 31, 2018

Concept ACK

@jmcorgan do you mind taking a look here?

@domob1812
Copy link
Contributor Author

Rebased

@domob1812 domob1812 force-pushed the zmq-cleanup branch 2 times, most recently from c4ce449 to 95c442d Compare November 6, 2018 08:18
Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

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

Concept ACK.

@domob1812
Copy link
Contributor Author

@hebasto: For some reason, Github won't let me reply this directly to your comment above. The guard is not necessary, since it is present already where the src/zmq files themselves are used - e.g. in src/init.cpp.

@domob1812
Copy link
Contributor Author

Rebased. Is there anything missing for me to do here? I think this is a real improvement to the quality of the ZMQ code.

@fanquake
Copy link
Member

@jmcorgan Are you able to take a look at this?

@domob1812
Copy link
Contributor Author

Rebase done, and I've now cleaned up the commit history as well (squashed in the "fixup" commits as appropriate). Also split out the TryForEachAndRemoveFailed refactor into its own commit.

@hebasto
Copy link
Member

hebasto commented Sep 6, 2020

The first commit 707c480 is not compiled for me. Make iterator type auto?

This is a pure refactoring of zmqnotificationinterface to make the
code easier to read and maintain.  It replaces explicit iterators
with C++11 for-each loops where appropriate and uses std::unique_ptr
to make memory ownership more explicit.
This factors out the common logic to run over all ZMQ notifiers, call a
function on them, and remove them from the list if the function fails is
extracted to a helper method.

Note that this also fixes a potential memory leak:  When a notifier was
removed previously after its callback returned false, it would just be
removed from the list without destructing the object.  This is now done
correctly by std::unique_ptr behind the scenes.
Instead of returning a raw pointer from CZMQNotifierFactory and
implicitly requiring the caller to know that it has to take ownership,
return a std::unique_ptr to make this explicit.

This also changes the typedef for CZMQNotifierFactory to use the new
C++11 using syntax, which makes it (a little) less cryptic.
zmqconfig.h is currently not really needed anywhere, except that
it declares zmqError (which is then defined in
zmqnotificationinterface.cpp).  Note in particular that there is
no need to conditionally include zmq.h only if ZMQ is enabled, because
the place in the core code where the ZMQ library itself is included
(init.cpp) is conditional already on that.

This commit removes zmqconfig.h and replaces it by a much simpler
zmqutil.h library for zmqError.  The definition of the function is
moved to the matching (newly created) zmqutil.cpp.
@domob1812
Copy link
Contributor Author

The first commit 707c480 is not compiled for me. Make iterator type auto?

Sorry for that, it is fixed now. The overall PR is not changed (except I've rebased it to current master), but now each individual commit builds.

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

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

ACK 166ddcb.

Mind making the latest commit 166ddcb a sripted-diff with the following script:

sed -i 's/SendMessage/SendZmqMessage/g' src/zmq/zmqpublishnotifier.*

?

@fanquake fanquake requested a review from instagibbs September 15, 2020 06:45
Windows headers define SendMessage as a macro, which leads to problems
with the method name "SendMessage".  To circumvent this, we rename the
method to "SendZmqMessage".

-BEGIN VERIFY SCRIPT-
sed -i 's/SendMessage/SendZmqMessage/g' src/zmq/zmqpublishnotifier.*
-END VERIFY SCRIPT-
@domob1812
Copy link
Contributor Author

Mind making the latest commit 166ddcb a sripted-diff with the following script:

Good point, done.

@instagibbs
Copy link
Member

utACK 6fe2ef2

I restarted one travis job that appeared to be a spurious timeout.

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

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

re-ACK 6fe2ef2, only the latest commit got a scripted-diff since my previous review.

@fanquake fanquake merged commit 831b0ec into bitcoin:master Sep 19, 2020
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Sep 20, 2020
6fe2ef2 scripted-diff: Rename SendMessage to SendZmqMessage. (Daniel Kraft)
a3ffb6e Replace zmqconfig.h by a simple zmqutil. (Daniel Kraft)
7f2ad1b Use std::unique_ptr for CZMQNotifierFactory. (Daniel Kraft)
b93b9d5 Simplify and fix notifier removal on error. (Daniel Kraft)
e15b1cf Various cleanups in zmqnotificationinterface. (Daniel Kraft)

Pull request description:

  This contains various small code cleanups that make the ZMQ code easier to read and maintain (at least in my opinion).  The only functional change is that a potential memory leak is fixed that would have occured when a notifier is removed from the `notifiers` list after its callback function returned `false` (which is likely not relevant in practice but still a bug).

ACKs for top commit:
  instagibbs:
    utACK 6fe2ef2
  hebasto:
    re-ACK 6fe2ef2, only the latest commit got a scripted-diff since my [previous](bitcoin#13686 (review)) review.

Tree-SHA512: 8206f8713bf3698d7cd4cb235f6657dc1c4dd920f50a8c5f371a559dd17ce5ab6d94d6281165eef860a22fc844a6bb25489ada12c83ebc780efd7ccdc0860f70
@domob1812 domob1812 deleted the zmq-cleanup branch September 21, 2020 07:48
domob1812 added a commit to xaya/xaya that referenced this pull request Sep 21, 2020
Merged bitcoin/bitcoin#13686 with Xaya's
ZMQ extensions.
ftrader pushed a commit to bitcoin-cash-node/bitcoin-cash-node that referenced this pull request Apr 14, 2021
Co-authored-by: Calin Culianu <calin.culianu@gmail.com>

Summary
---

This is a backport of [Core#13686](bitcoin/bitcoin#13686).

While working on !1045 I noticed some un-idiomatic C++ usage and a
potential memory leak. I went and looked at the Core repo and it turns
out they have since updated the ZMQ subsystem to be better and they
fixed the leak.

Original commit message:

    This contains various small code cleanups that make the ZMQ code easier
    to read  and maintain (at least in my opinion). The only functional change
    is that a potential memory leak is fixed that would have occured when a
    notifier is removed from the notifiers list after its callback function
    returned false (which is likely not relevant in practice but still a bug).

Note that this depends on MR !1045 (it is a commit on top of it).

I adapted this code a little bit to alphabetize the headers and also
other minor tiny nits. It's 99% the Core commit, essentially.

Test Plan
---

- `ninja all check`
- `test/functional/test_runner.py interface_zmq`
- `test/functional/test_runner.py rpc_zmq`
kwvg added a commit to kwvg/dash that referenced this pull request Aug 28, 2021
PastaPastaPasta added a commit to dashpay/dash that referenced this pull request Aug 31, 2021
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 11, 2021
Summary:
This is a pure refactoring of zmqnotificationinterface to make the
code easier to read and maintain.  It replaces explicit iterators
with C++11 for-each loops where appropriate and uses std::unique_ptr
to make memory ownership more explicit.

This is a backport of [[bitcoin/bitcoin#13686 | core#13686]] [1/5]
bitcoin/bitcoin@e15b1cf

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D10282
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 11, 2021
Summary:
This factors out the common logic to run over all ZMQ notifiers, call a
function on them, and remove them from the list if the function fails is
extracted to a helper method.

Note that this also fixes a potential memory leak:  When a notifier was
removed previously after its callback returned false, it would just be
removed from the list without destructing the object.  This is now done
correctly by std::unique_ptr behind the scenes.

This is a backport of [[bitcoin/bitcoin#13686 | core#13686]] [2/5]
bitcoin/bitcoin@b93b9d5

Depends on D10282

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D10283
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 11, 2021
Summary:
Instead of returning a raw pointer from CZMQNotifierFactory and
implicitly requiring the caller to know that it has to take ownership,
return a std::unique_ptr to make this explicit.

This also changes the typedef for CZMQNotifierFactory to use the new
C++11 using syntax, which makes it (a little) less cryptic.

This is a backport of [[bitcoin/bitcoin#13686 | core#13686]] [3/5]
bitcoin/bitcoin@7f2ad1b

Depends on D10283

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D10284
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 11, 2021
Summary:
zmqconfig.h is currently not really needed anywhere, except that
it declares zmqError (which is then defined in
zmqnotificationinterface.cpp).  Note in particular that there is
no need to conditionally include zmq.h only if ZMQ is enabled, because
the place in the core code where the ZMQ library itself is included
(init.cpp) is conditional already on that.

This commit removes zmqconfig.h and replaces it by a much simpler
zmqutil.h library for zmqError.  The definition of the function is
moved to the matching (newly created) zmqutil.cpp.

This is a backport of [[bitcoin/bitcoin#13686 | core#13686]]
bitcoin/bitcoin@a3ffb6e

See D7579 for why blockdb.h is included instead of validation.h in zmqpublishnotifier.cpp

Depends on D10284

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D10285
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 11, 2021
Summary:
Windows headers define SendMessage as a macro, which leads to problems
with the method name "SendMessage".  To circumvent this, we rename the
method to "SendZmqMessage".

This is a backport of [[bitcoin/bitcoin#13686 | core#13686]] [5/5]
bitcoin/bitcoin@6fe2ef2

Depends on D10285

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D10286
@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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants