Skip to content

Conversation

@ajtowns
Copy link
Contributor

@ajtowns ajtowns commented Nov 17, 2020

Ensure nMaxOutboundTotalBytesSentInCycle and nMaxOutboundCycleStartTime are initialized even if CConnman::Start() is not called. Prevents failures in test/fuzz/connman when run under valgrind.

@ajtowns
Copy link
Contributor Author

ajtowns commented Nov 17, 2020

cc @practicalswift @MarcoFalke ; related: #20188 (comment)

@fanquake fanquake added the P2P label Nov 17, 2020
Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

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

Seems fine. The alternative would be to call Start in the fuzz tests, but that spins up quite a few threads.

@DrahtBot
Copy link
Contributor

DrahtBot commented Nov 17, 2020

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.

@maflcko
Copy link
Member

maflcko commented Nov 17, 2020

review ACK c401a1d83f119f11fe0e446193458c5de224adbf

@jnewbery
Copy link
Contributor

Is there any downside to just using default initialization for these data members?

-    uint64_t nMaxOutboundTotalBytesSentInCycle GUARDED_BY(cs_totalBytesSent);
+    uint64_t nMaxOutboundTotalBytesSentInCycle GUARDED_BY(cs_totalBytesSent) {0};
-    uint64_t nMaxOutboundCycleStartTime GUARDED_BY(cs_totalBytesSent);
+    uint64_t nMaxOutboundCycleStartTime GUARDED_BY(cs_totalBytesSent) {0};

It seems to me that Init() should only initialize members that require a data from the connOptions object.

@practicalswift
Copy link
Contributor

Thanks for noticing and fixing this @ajtowns!

Any reason not to use in-class member initialisation?

Like this:

diff --git a/src/net.h b/src/net.h
index 77649247d..d8d7daf64 100644
--- a/src/net.h
+++ b/src/net.h
@@ -478,10 +478,10 @@ private:
     uint64_t nTotalBytesSent GUARDED_BY(cs_totalBytesSent) {0};

     // outbound limit & stats
-    uint64_t nMaxOutboundTotalBytesSentInCycle GUARDED_BY(cs_totalBytesSent);
-    uint64_t nMaxOutboundCycleStartTime GUARDED_BY(cs_totalBytesSent);
-    uint64_t nMaxOutboundLimit GUARDED_BY(cs_totalBytesSent);
-    uint64_t nMaxOutboundTimeframe GUARDED_BY(cs_totalBytesSent);
+    uint64_t nMaxOutboundTotalBytesSentInCycle GUARDED_BY(cs_totalBytesSent) {0};
+    uint64_t nMaxOutboundCycleStartTime GUARDED_BY(cs_totalBytesSent) {0};
+    uint64_t nMaxOutboundLimit GUARDED_BY(cs_totalBytesSent) {0};
+    uint64_t nMaxOutboundTimeframe GUARDED_BY(cs_totalBytesSent) {0};

     // P2P timeout in seconds
     int64_t m_peer_connect_timeout;

Rationale provided in the C++ Core Guidelines: C.48: Prefer in-class initializers to member initializers in constructors for constant initializers :)

@practicalswift
Copy link
Contributor

What @jnewbery said literally 4 seconds before me :)

@maflcko
Copy link
Member

maflcko commented Nov 17, 2020

I presumed that the Start method was provided so that connman could be stopped and started while core is running. Members would be re-initialized. Though, we don't do that right now and I don't think we will, so either approach is fine by me.

Ensure nMaxOutboundTotalBytesSentInCycle and nMaxOutboundCycleStartTime
are initialized even if CConnman::Start() is not called.
@ajtowns ajtowns force-pushed the 202011-connman-fuzz branch from c401a1d to 9d09132 Compare November 17, 2020 16:45
@ajtowns ajtowns changed the title CConnman: move initialization into Init() CConnman: move initialization to declaration Nov 17, 2020
@ajtowns
Copy link
Contributor Author

ajtowns commented Nov 17, 2020

I think it would be a large change to make it reasonable to call Start() twice, so moved init to declaration, without resetting them in Start().

@practicalswift
Copy link
Contributor

ACK 9d09132: patch looks correct!

@maflcko
Copy link
Member

maflcko commented Nov 18, 2020

review ACK 9d09132 , checked that we call Start only once and in the same scope where connman is constructed (AppInitMain) 💸

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

review ACK 9d09132be4ff99f98ca905c342347d5f35f13350 , checked that we call Start only once and in the same scope where connman is constructed (AppInitMain) 💸
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUiqVQv/UC5Tg9YLXIPXSm3OwF820sVE+O+s6Ks4t71B/pzcwfUzq4llOxTx3TDW
nvpgLlCVeJkW79tUby/5YBVzrrgZ80KaGldO3H2eGvdi9ARSD3cX6OtokYNUHmty
ZJXijsEs6zlli3LK3wlBXZ30HNjkSzyLOK9CvnU9/k+MNwKvqSDYQRzHHMRungUU
+fBuc2CVW16WVhgmXMWfQb7QWjMxuqe5JwD6PAuGPftgXYB48uHR8Uys36X1o6fn
TQ1WqoZcDDULp4XIyRxqcC4FhTCBaacLE3ePZC/AzcP7Ga51brW9XuNZC8yFW4su
omaUNR2Sw0XGTqtXD1BeIT00ZbkLyX7BtVi7VMgXVCIrjZBqdPXIK4PBqtydMk7g
S5ocg3QVSLE8lZGPXg3va8u0cs7cu4ylxIyx1iIwDJmItFSv48EVGjk7CVdbWv+c
5zMC9m3XbyYQP8IP59Z2LVstQ8ga+dWCxECTTtT260gwl2CsSCFl59fL4jBWNHvu
hUIVVCRj
=EJrI
-----END PGP SIGNATURE-----

python: error while loading shared libraries: libpython3.8.so.1.0: cannot open shared object file: No such file or directory

@jnewbery
Copy link
Contributor

Code review ACK 9d09132

@fanquake fanquake merged commit 5bcae79 into bitcoin:master Nov 18, 2020
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Nov 18, 2020
9d09132 CConnman: initialise at declaration rather than in Start() (Anthony Towns)

Pull request description:

  Ensure nMaxOutboundTotalBytesSentInCycle and nMaxOutboundCycleStartTime are initialized even if CConnman::Start() is not called. Prevents failures in test/fuzz/connman when run under valgrind.

ACKs for top commit:
  practicalswift:
    ACK 9d09132: patch looks correct!
  MarcoFalke:
    review ACK 9d09132 , checked that we call Start only once and in the same scope where connman is constructed (AppInitMain) 💸
  jnewbery:
    Code review ACK 9d09132

Tree-SHA512: 1c6c893e8c616a91947a8cc295b0ba508af3ecfcdcd94cdc5f95d808cc93c6d1a71fd24dcc194dc583854e9889fb522ca8523043367fb0263370fbcab08c6aaa
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 3, 2022
Summary:
Ensure `nMaxOutboundTotalBytesSentInCycle` and `nMaxOutboundCycleStartTime` are initialized even if `CConnman::Start()` is not called.  Prevents failures in test/fuzz/connman when run under valgrind.

`nTotalBytesRecv` and `nTotalBytesSent` are already initialized in the class definition, and `CConnman::Start` is only called once in `AppInitMain`, so removing the reinitialization in `Start` has no impact.

This is a backport of [[bitcoin/bitcoin#20408 | core#20408]]

Test Plan:
With TSAN:
`ninja && ninja check check-functional`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D10740
@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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants