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

pull/1658/head
Jeff Becker 3 years ago
parent 2a76a3d081
commit e4ed53224c
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

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

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

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

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

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

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

@ -434,7 +434,7 @@ namespace llarp
ctx->pathset = self; ctx->pathset = self;
std::string path_shortName = "[path " + m_router->ShortName() + "-"; std::string path_shortName = "[path " + m_router->ShortName() + "-";
path_shortName = path_shortName + std::to_string(m_router->NextPathBuildNumber()) + "]"; 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()); LogInfo(Name(), " build ", path->ShortName(), ": ", path->HopsString());
path->SetBuildResultHook([self](Path_ptr p) { self->HandlePathBuilt(p); }); path->SetBuildResultHook([self](Path_ptr p) { self->HandlePathBuilt(p); });

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

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

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

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

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

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

@ -20,7 +20,7 @@ MakePath(std::vector< char > hops)
std::vector< RC_t > pathHops; std::vector< RC_t > pathHops;
for(const auto& hop : hops) for(const auto& hop : hops)
pathHops.push_back(MakeHop(hop)); 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]") TEST_CASE("UniqueEndpointSet_t has unique endpoints", "[path]")

Loading…
Cancel
Save