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
Use Sock in CNode #23604
Use Sock in CNode #23604
Conversation
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, 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. |
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.
Concept and light code-review ACK ba8cc5b
The first commit seems unrelated to the other two, I guess this will be useful in a later chopped off PR?
ba8cc5b
to
846e995
Compare
Invalidates (light) ACK from @theStack
Oh, good catch! The idea is to use |
Previously invalidated (light) ACK from @theStack |
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.
Light re-ACK 78cda19
(as per git range-diff ba8cc5b4...78cda19b
)
Invalidates (light) ACK from @theStack |
Concept ACK. CI failures are unrelated. |
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.
ACK 0ec56c6
ACK d22a477 |
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.
ACK 1477f3a.
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.
tACK 1477f3a
Great explanation of why use shared_ptr
instead of std::unique_ptr
for m_sock
This way it can be used in `ConsumeNode()`.
Invalidates ACKs from @jonatack, @theStack, @stratospher, @w0xlt |
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.
ACK af7fc28 🧦
break; | ||
nBytes = send(node.hSocket, reinterpret_cast<const char*>(data.data()) + node.nSendOffset, data.size() - node.nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT); | ||
} | ||
nBytes = node.m_sock->Send(reinterpret_cast<const char*>(data.data()) + node.nSendOffset, data.size() - node.nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT); |
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.
Probably a silly question: LOCK(node.m_sock_mutex);
applies even to node.m_sock->Send(...)
operation but then here one can return contained sockets out of the function.
I understand that m_sock_mutex
guards only m_sock
member read & write operations but not necessarily the contained socket. Is that correct or not?
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.
That is a good question.
Before this PR the mutex protects the value of the socket variable - that is, to make sure it does not change from e.g. 7
(it is a file descriptor) to INVALID_SOCKET
in the middle of constructs like:
if (socket != INVALID_SOCKET) {
... do something with the socket, assuming it is valid ...
}
After this PR, it is the same with the exception that the variable is not a bare integer anymore, but a shared_ptr
and an empty such pointer is used to designate what was designated before by INVALID_SOCKET
.
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.
LGTM, one request
Change `CNode` to use a pointer to `Sock` instead of a bare `SOCKET`. This will help mocking / testing / fuzzing more code.
-BEGIN VERIFY SCRIPT- sed -i -e 's/cs_hSocket/m_sock_mutex/g' $(git grep -l cs_hSocket) -END VERIFY SCRIPT-
Invalidates ACK from @jonatack Previously invalidated ACKs from @theStack, @stratospher, @w0xlt |
re-ACK ef5014d changes since last review are the removal of an unneeded dtor and the addition of a style commit |
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.
reACK ef5014d
@@ -2994,11 +2997,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, const | |||
m_serializer = std::make_unique<V1TransportSerializer>(V1TransportSerializer()); | |||
} | |||
|
|||
CNode::~CNode() |
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.
Apologies if this is a silly question but just wanted to clarify - removing this destructor, it's implicit that the shared_ptr for Sock will be reset and the Sock's destructor will responsible for closing the socket?
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.
See https://en.cppreference.com/w/cpp/language/destructor "Implicitly-declared destructor".
The default destructor for CNode will call all the destructors of all of it's members. The destructor for shared_ptr will reduce the reference counter, and then if that reference counter is zero, the destructor for Sock will be called. If the RC is not zero, then the Sock will continue to exist on the heap (presumably being used by another thread), until the owner of that shared_ptr calls the destructor.
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.
utACK ef5014d, I have reviewed the code, and believe it makes sense to merge
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.
Since my last ACK was only "light" and happened weeks ago, so I decided to do a full review of this PR again instead of looking at the range-diff. Changes LGTM!
Cod-review ACK ef5014d
This is a piece of #21878, chopped off to ease review.
Change
CNode
to use a pointer toSock
instead of a bareSOCKET
.This will help mocking / testing / fuzzing more code.