This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix atomicity violation in network-devp2p #11277
Merged
dvdplm
merged 1 commit into
openethereum:master
from
BurtonQin:fix-atomicity-violation-in-network-devp2p
Dec 10, 2019
Merged
Fix atomicity violation in network-devp2p #11277
dvdplm
merged 1 commit into
openethereum:master
from
BurtonQin:fix-atomicity-violation-in-network-devp2p
Dec 10, 2019
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
It looks like @BurtonQin signed our Contributor License Agreement. 👍 Many thanks, Parity Technologies CLA Bot |
dvdplm
approved these changes
Nov 21, 2019
@@ -220,14 +220,13 @@ impl Connection { | |||
|
|||
/// Register this connection with the IO event loop. | |||
pub fn register_socket<Host: Handler>(&self, reg: Token, event_loop: &mut EventLoop<Host>) -> io::Result<()> { | |||
if self.registered.load(AtomicOrdering::SeqCst) { | |||
if self.registered.compare_and_swap(false, true, AtomicOrdering::SeqCst) { | |||
return Ok(()); | |||
} | |||
trace!(target: "network", "connection register; token={:?}", reg); | |||
if let Err(e) = event_loop.register(&self.socket, reg, self.interest, PollOpt::edge() /* | PollOpt::oneshot() */) { // TODO: oneshot is broken on windows | |||
trace!(target: "network", "Failed to register {:?}, {:?}", reg, e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder why we don't return here (and set self.registered
back to false
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder the same, @arkpar cfb8671#diff-c378e114f762560f980403b3e66392dcL140-L142?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@arkpar I'll go ahead and merge this now, but I'm still curious. :)
ordian
approved these changes
Dec 5, 2019
dvdplm
added
A8-looksgood 🦄
Pull request is reviewed well.
B0-patch-stable 🕷
Pull request should also be back-ported to the stable branch.
B1-patch-beta 🕷🕷
M4-io 💾
Interaction with filesystem/databases.
labels
Dec 10, 2019
dvdplm
added a commit
that referenced
this pull request
Dec 15, 2019
…ate their data instead Merge branch 'master' into dp/chore/kvdb-no-default-column * master: tx-q: enable basic verification of local transactions (#11332) remove null signatures (#11335) ethcore/res: activate agharta on classic 9573000 (#11331) [secretstore] migrate to version 4 (#11322) Enable EIP-2384 for ice age hard fork (#11281) Fix atomicity violation in network-devp2p (#11277)
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
A8-looksgood 🦄
Pull request is reviewed well.
B0-patch-stable 🕷
Pull request should also be back-ported to the stable branch.
M4-io 💾
Interaction with filesystem/databases.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It is to fix an atomicity violation in network-devp2p.
https://github.com/paritytech/parity-ethereum/blob/ee016127686f6cc63815b15d7df5aa22f4edc22c/util/network-devp2p/src/connection.rs#L222-L232
Following the same bug pattern of #5910
In function register_socket:
It seems that the atomic variable is used to guarantee that the inner code will only be executed once in multiple calls.
The function takes &self (instead of &mut self) and I think it may be called in different threads simultaneously.
If that is the case, the following execution path may happen:
The atomicity violation happens because the read and write of one atomic variable are interleaved by another write.
The fix is to use one compare_and_swap to replace the separate load and store, as shown in the fix.
✄ -----------------------------------------------------------------------------