Skip to content

Commit

Permalink
Fix crash in additional_fanout when calling traverse
Browse files Browse the repository at this point in the history
Summary: As above

Reviewed By: alikhtarov

Differential Revision: D59609681

fbshipit-source-id: 6b4b1f942957451fe3f6e062a0410327bb3ee139
  • Loading branch information
Stuart Clark authored and facebook-github-bot committed Jul 11, 2024
1 parent 3befde1 commit 730b9c8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
13 changes: 10 additions & 3 deletions mcrouter/ServiceInfo-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,21 @@ class RouteHandlesCommandDispatcher {
public:
bool dispatch(
size_t typeId,
const std::shared_ptr<ProxyRequestContextWithInfo<RouterInfo>>& ctx,
folly::StringPiece key,
const ProxyRoute<RouterInfo>& proxyRoute,
std::string& outStr) {
return dispatcher_.dispatch(typeId, *this, key, proxyRoute, outStr);
return dispatcher_.dispatch(typeId, *this, ctx, key, proxyRoute, outStr);
}

template <class Request>
static void processMsg(
RouteHandlesCommandDispatcher<RouterInfo>& me,
const std::shared_ptr<ProxyRequestContextWithInfo<RouterInfo>>& ctx,
folly::StringPiece key,
const ProxyRoute<RouterInfo>& proxyRoute,
std::string& outStr) {
outStr = me.processMsgInternal<Request>(key, proxyRoute);
outStr = me.processMsgInternal<Request>(ctx, key, proxyRoute);
}

private:
Expand All @@ -73,13 +75,15 @@ class RouteHandlesCommandDispatcher {
// Dispatcher class
detail::RouteHandlesCommandDispatcher<RouterInfo>,
// List of types of args to Dispatcher::processMsg()
const std::shared_ptr<ProxyRequestContextWithInfo<RouterInfo>>&,
folly::StringPiece,
const ProxyRoute<RouterInfo>&,
std::string&>
dispatcher_;

template <class Request>
std::string processMsgInternal(
const std::shared_ptr<ProxyRequestContextWithInfo<RouterInfo>>& ctx,
folly::StringPiece key,
const ProxyRoute<RouterInfo>& proxyRoute) {
std::string tree;
Expand Down Expand Up @@ -111,6 +115,7 @@ class RouteHandlesCommandDispatcher {
} else {
request.key_ref() = key;
}
fiber_local<RouterInfo>::setSharedCtx(ctx);
proxyRoute.traverse(request, t);
return tree;
}
Expand Down Expand Up @@ -411,8 +416,10 @@ ServiceInfo<RouterInfo>::ServiceInfoImpl::ServiceInfoImpl(
requestName, typename RouterInfo::RoutableRequests());

std::string res;
auto ctx = ProxyRequestContextWithInfo<RouterInfo>::createRecording(
proxy_, nullptr);
if (!routeHandlesCommandDispatcher_.dispatch(
typeId, key, proxyRoute_, res)) {
typeId, ctx, key, proxyRoute_, res)) {
throw std::runtime_error(
folly::sformat("route: unknown request {}", requestName));
}
Expand Down
4 changes: 3 additions & 1 deletion mcrouter/lib/HashSelector.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ class BucketHashSelector : public HashSelectorBase<HashFunc> {
auto key = folly::to<std::string>(*bucketId);
if (this->clientFanout_) {
auto& ctx = mcrouter::fiber_local<RouterInfo>::getSharedCtx();
key += ctx->userIpAddress();
if (ctx) {
key += ctx->userIpAddress();
}
}
return folly::fibers::runInMainContext(
[this, size, key]() { return this->selectInternal(key, size); });
Expand Down

0 comments on commit 730b9c8

Please sign in to comment.