Skip to content

Conversation

TheBlueMatt
Copy link
Contributor

Generally holding locks while making callbacks is a bad idea, and for my eventual per-CNodeState locks. Also makes DEBUG_LOCKORDER more strict in a way that it assumes so best to enforce the requirement that locks are released in the order they are taken.

These two commits were pulled out of #10652 as they were entirely unconnected from the rest. Note that there was already a comment on the ForEachNode commit there.

@gmaxwell
Copy link
Contributor

hm this may require carefully auditing every use to determine if the code in question isn't being protected by an assumed holding of cs_vnodes.

@TheBlueMatt
Copy link
Contributor Author

Indeed. I believe it is the case that none of them are needed, but I may be incorrect. Note that folks may want to hold off on review, @cfields indicated he may have a more complete solution to the general mess that is our manual CNode refcounting.

@theuni
Copy link
Member

theuni commented Jun 29, 2017

No, please go ahead and review. I've worked on this several times now and haven't come up with anything that could be considered an improvement. I'm currently taking another look, but that shouldn't block this unless I manage to come up with something in the next day or so.

@promag
Copy link
Contributor

promag commented Oct 28, 2017

Ping @theuni @TheBlueMatt, are you working on this?

src/net.cpp Outdated
break;
}
}
return found != nullptr && NodeFullyConnected(found) && func(found);
bool res = found != nullptr && NodeFullyConnected(found) && func(found);
Copy link
Member

Choose a reason for hiding this comment

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

This still calls func while cs_vNodes is held. Is that intentional?

src/net.cpp Outdated
}
}
for (CNode* node : vNodes_copy) {
if (NodeFullyConnected(node))
Copy link
Member

Choose a reason for hiding this comment

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

Nit: either { } around if body, or body on the same line (in multiple places).

@TheBlueMatt TheBlueMatt force-pushed the 2017-06-cnodestateaccessors-5 branch from e4f477b to 7f1ae14 Compare October 31, 2017 15:50
@TheBlueMatt
Copy link
Contributor Author

Addressed @sipa's comments.

@TheBlueMatt TheBlueMatt force-pushed the 2017-06-cnodestateaccessors-5 branch from 7f1ae14 to 8b4a18f Compare October 31, 2017 16:27
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.

utACK 50ef0b0

return found != nullptr && NodeFullyConnected(found) && func(found);
bool res = false;
if (found != nullptr) {
res = NodeFullyConnected(found) && func(found);
Copy link
Contributor

Choose a reason for hiding this comment

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

This will leak reference count if func throws. Suggest using RAII helper to prevent this, see NodeRef in eeb85c6. NodeRef also simplifies the ForEachNode implementations there.

}

void CConnman::ForEachNode(std::function<void (CNode* pnode)> func)
{
std::vector<CNode*> vNodes_copy;
Copy link
Contributor

Choose a reason for hiding this comment

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

These should just call ForEachNodeThen with a null post functions so this code doesn't have to be repeated so many times. Again see eeb85c6.

#define LEAVE_CRITICAL_SECTION(cs) \
{ \
(cs).unlock(); \
LeaveCritical((void*)(&cs)); \
Copy link
Contributor

Choose a reason for hiding this comment

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

This should just say LeaveCritical(&(cs)). cs should be parenthesized since it's a macro argument, and the cast is unneeded.

@TheBlueMatt
Copy link
Contributor Author

Superceded by #11604

@TheBlueMatt TheBlueMatt closed this Nov 4, 2017
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
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.

7 participants