use weak_ptr on a path to reference its parent pathset instead of a bare pointer so crashes dont happen

This commit is contained in:
Jeff Becker 2021-06-04 08:34:41 -04:00
parent 2a76a3d081
commit e4ed53224c
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05
14 changed files with 71 additions and 21 deletions

View File

@ -51,6 +51,12 @@ namespace llarp
return shared_from_this();
}
std::weak_ptr<path::PathSet>
GetWeak() override
{
return weak_from_this();
}
void
BlacklistSNode(const RouterID snode) override;

View File

@ -61,6 +61,12 @@ namespace llarp
return shared_from_this();
}
std::weak_ptr<path::PathSet>
GetWeak() override
{
return weak_from_this();
}
bool
SupportsV6() const override
{

View File

@ -34,6 +34,12 @@ namespace llarp
return shared_from_this();
}
std::weak_ptr<path::PathSet>
GetWeak() override
{
return weak_from_this();
}
void
Thaw() override;

View File

@ -25,10 +25,10 @@ namespace llarp
{
Path::Path(
const std::vector<RouterContact>& h,
PathSet* parent,
std::weak_ptr<PathSet> pathset,
PathRole startingRoles,
std::string shortName)
: m_PathSet(parent), _role(startingRoles), m_shortName(std::move(shortName))
: m_PathSet{pathset}, _role{startingRoles}, m_shortName{std::move(shortName)}
{
hops.resize(h.size());
@ -54,7 +54,7 @@ namespace llarp
// initialize parts of the introduction
intro.router = hops[hsz - 1].rc.pubkey;
intro.pathID = hops[hsz - 1].txID;
if (parent)
if (auto parent = m_PathSet.lock())
EnterState(ePathBuilding, parent->Now());
}
@ -253,7 +253,10 @@ namespace llarp
edge = *failedAt;
r->loop()->call([r, self = shared_from_this(), edge]() {
self->EnterState(ePathFailed, r->Now());
self->m_PathSet->HandlePathBuildFailedAt(self, edge);
if (auto parent = self->m_PathSet.lock())
{
parent->HandlePathBuildFailedAt(self, edge);
}
});
}
@ -272,7 +275,10 @@ namespace llarp
if (st == ePathExpired && _status == ePathBuilding)
{
_status = st;
m_PathSet->HandlePathBuildTimeout(shared_from_this());
if (auto parent = m_PathSet.lock())
{
parent->HandlePathBuildTimeout(shared_from_this());
}
}
else if (st == ePathBuilding)
{
@ -287,7 +293,10 @@ namespace llarp
{
LogInfo("path ", Name(), " died");
_status = st;
m_PathSet->HandlePathDied(shared_from_this());
if (auto parent = m_PathSet.lock())
{
parent->HandlePathDied(shared_from_this());
}
}
else if (st == ePathEstablished && _status == ePathTimeout)
{
@ -371,11 +380,14 @@ namespace llarp
void
Path::Rebuild()
{
std::vector<RouterContact> newHops;
for (const auto& hop : hops)
newHops.emplace_back(hop.rc);
LogInfo(Name(), " rebuilding on ", ShortName());
m_PathSet->Build(newHops);
if (auto parent = m_PathSet.lock())
{
std::vector<RouterContact> newHops;
for (const auto& hop : hops)
newHops.emplace_back(hop.rc);
LogInfo(Name(), " rebuilding on ", ShortName());
parent->Build(newHops);
}
}
void
@ -681,8 +693,12 @@ namespace llarp
bool
Path::HandleHiddenServiceFrame(const service::ProtocolFrame& frame)
{
MarkActive(m_PathSet->Now());
return m_DataHandler && m_DataHandler(shared_from_this(), frame);
if (auto parent = m_PathSet.lock())
{
MarkActive(parent->Now());
return m_DataHandler && m_DataHandler(shared_from_this(), frame);
}
return false;
}
template <typename Samples_t>

View File

@ -87,7 +87,7 @@ namespace llarp
HopList hops;
PathSet* const m_PathSet;
std::weak_ptr<PathSet> m_PathSet;
service::Introduction intro;
@ -95,7 +95,7 @@ namespace llarp
Path(
const std::vector<RouterContact>& routers,
PathSet* parent,
std::weak_ptr<PathSet> parent,
PathRole startingRoles,
std::string shortName);

View File

@ -225,7 +225,8 @@ namespace llarp
auto itr = map.second.find(id);
if (itr != map.second.end())
{
return itr->second->m_PathSet->GetSelf();
if (auto parent = itr->second->m_PathSet.lock())
return parent;
}
return nullptr;
}

View File

@ -434,7 +434,7 @@ namespace llarp
ctx->pathset = self;
std::string path_shortName = "[path " + m_router->ShortName() + "-";
path_shortName = path_shortName + std::to_string(m_router->NextPathBuildNumber()) + "]";
auto path = std::make_shared<path::Path>(hops, self.get(), roles, std::move(path_shortName));
auto path = std::make_shared<path::Path>(hops, GetWeak(), roles, std::move(path_shortName));
LogInfo(Name(), " build ", path->ShortName(), ": ", path->HopsString());
path->SetBuildResultHook([self](Path_ptr p) { self->HandlePathBuilt(p); });

View File

@ -57,7 +57,7 @@ namespace llarp
DoPathBuildBackoff();
public:
AbstractRouter* m_router;
AbstractRouter* const m_router;
SecretKey enckey;
size_t numHops;
llarp_time_t lastBuild = 0s;

View File

@ -117,6 +117,10 @@ namespace llarp
virtual PathSet_ptr
GetSelf() = 0;
/// get a weak_ptr of ourself
virtual std::weak_ptr<PathSet>
GetWeak() = 0;
virtual void
BuildOne(PathRole roles = ePathRoleAny) = 0;

View File

@ -1447,7 +1447,7 @@ namespace llarp
path->Endpoint(),
order,
GenTXID(),
timeout);
timeout + (2 * path->intro.latency));
LogInfo(
"doing lookup for ",
remote,

View File

@ -333,7 +333,6 @@ namespace llarp
bool
OutboundContext::Pump(llarp_time_t now)
{
if (ReadyToSend() and remoteIntro.router.IsZero())
{
SwapIntros();

View File

@ -38,6 +38,12 @@ namespace llarp
return shared_from_this();
}
std::weak_ptr<path::PathSet>
GetWeak() override
{
return weak_from_this();
}
Address
Addr() const;

View File

@ -54,6 +54,12 @@ namespace llarp
return shared_from_this();
}
std::weak_ptr<path::PathSet>
GetWeak() override
{
return weak_from_this();
}
bool
SupportsV6() const override
{

View File

@ -20,7 +20,7 @@ MakePath(std::vector< char > hops)
std::vector< RC_t > pathHops;
for(const auto& hop : hops)
pathHops.push_back(MakeHop(hop));
return std::make_shared< Path_t >(pathHops, nullptr, 0, "test");
return std::make_shared< Path_t >(pathHops, std::weak_ptr<llarp::path::PathSet>{}, 0, "test");
}
TEST_CASE("UniqueEndpointSet_t has unique endpoints", "[path]")