Skip to content

Commit 6fcfe2e

Browse files
mattbfbfacebook-github-bot
authored andcommitted
Identify debug sessions with a token
Summary: The `RuntimeAdapter` may be used after `disableDebugging` has been called. To ensure the `RuntimeAdapter` is alive long enough for this use, the Inspector should continue to control `RuntimeAdapter`'s lifetime (which is currently done via a `unique_ptr` that's moved into the Inspector). Callers need a way to identify the `RuntimeAdapter` after it has been moved into the Inspector so that debugging can be disabled via `disableDebugging`. This could be done by switching from a `unique_ptr` to a `shared_ptr` (so the caller can keep a copy), but consumers don't really have a reason to hang onto the `RuntimeAdapter` instance. Instead, leave the `RuntimeAdapter` inside a `unique_ptr`, and have consumers use a token to identify instances. Update all consumers of this API to use this new token interface. Changelog: [Internal] Reviewed By: jpporto Differential Revision: D38513256 fbshipit-source-id: 33580747cd8365d25dbddbe289f0c41141e3bc6a
1 parent 296d7db commit 6fcfe2e

File tree

6 files changed

+28
-33
lines changed

6 files changed

+28
-33
lines changed

ReactCommon/hermes/executor/HermesExecutorFactory.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,21 +149,18 @@ class DecoratedRuntime : public jsi::WithRuntimeDecorator<ReentrancyCheck> {
149149
HermesRuntime &hermesRuntime,
150150
std::shared_ptr<MessageQueueThread> jsQueue)
151151
: jsi::WithRuntimeDecorator<ReentrancyCheck>(*runtime, reentrancyCheck_),
152-
runtime_(std::move(runtime)),
153-
hermesRuntime_(hermesRuntime) {
152+
runtime_(std::move(runtime)) {
154153
#ifdef HERMES_ENABLE_DEBUGGER
155154
std::shared_ptr<HermesRuntime> rt(runtime_, &hermesRuntime);
156155
auto adapter = std::make_unique<HermesExecutorRuntimeAdapter>(rt, jsQueue);
157-
facebook::hermes::inspector::chrome::enableDebugging(
156+
debugToken_ = facebook::hermes::inspector::chrome::enableDebugging(
158157
std::move(adapter), "Hermes React Native");
159-
#else
160-
(void)hermesRuntime_;
161158
#endif
162159
}
163160

164161
~DecoratedRuntime() {
165162
#ifdef HERMES_ENABLE_DEBUGGER
166-
facebook::hermes::inspector::chrome::disableDebugging(hermesRuntime_);
163+
facebook::hermes::inspector::chrome::disableDebugging(debugToken_);
167164
#endif
168165
}
169166

@@ -177,7 +174,9 @@ class DecoratedRuntime : public jsi::WithRuntimeDecorator<ReentrancyCheck> {
177174

178175
std::shared_ptr<Runtime> runtime_;
179176
ReentrancyCheck reentrancyCheck_;
180-
HermesRuntime &hermesRuntime_;
177+
#ifdef HERMES_ENABLE_DEBUGGER
178+
facebook::hermes::inspector::chrome::DebugSessionToken debugToken_;
179+
#endif
181180
};
182181

183182
} // namespace

ReactCommon/hermes/inspector/chrome/ConnectionDemux.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ ConnectionDemux::ConnectionDemux(facebook::react::IInspector &inspector)
6363

6464
ConnectionDemux::~ConnectionDemux() = default;
6565

66-
int ConnectionDemux::enableDebugging(
66+
DebugSessionToken ConnectionDemux::enableDebugging(
6767
std::unique_ptr<RuntimeAdapter> adapter,
6868
const std::string &title) {
6969
std::lock_guard<std::mutex> lock(mutex_);
@@ -94,21 +94,12 @@ int ConnectionDemux::enableDebugging(
9494
std::make_shared<Connection>(std::move(adapter), title, waitForDebugger));
9595
}
9696

97-
void ConnectionDemux::disableDebugging(HermesRuntime &runtime) {
97+
void ConnectionDemux::disableDebugging(DebugSessionToken session) {
9898
std::lock_guard<std::mutex> lock(mutex_);
99-
100-
for (auto &it : conns_) {
101-
int pageId = it.first;
102-
auto &conn = it.second;
103-
104-
if (&(conn->getRuntime()) == &runtime) {
105-
removePage(pageId);
106-
107-
// must break here. removePage mutates conns_, so range-for iterator is
108-
// now invalid.
109-
break;
110-
}
99+
if (conns_.find(session) == conns_.end()) {
100+
return;
111101
}
102+
removePage(session);
112103
}
113104

114105
int ConnectionDemux::addPage(std::shared_ptr<Connection> conn) {

ReactCommon/hermes/inspector/chrome/ConnectionDemux.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <hermes/hermes.h>
1717
#include <hermes/inspector/RuntimeAdapter.h>
1818
#include <hermes/inspector/chrome/Connection.h>
19+
#include <hermes/inspector/chrome/Registration.h>
1920
#include <jsinspector/InspectorInterfaces.h>
2021

2122
namespace facebook {
@@ -36,10 +37,10 @@ class ConnectionDemux {
3637
ConnectionDemux(const ConnectionDemux &) = delete;
3738
ConnectionDemux &operator=(const ConnectionDemux &) = delete;
3839

39-
int enableDebugging(
40+
DebugSessionToken enableDebugging(
4041
std::unique_ptr<RuntimeAdapter> adapter,
4142
const std::string &title);
42-
void disableDebugging(HermesRuntime &runtime);
43+
void disableDebugging(DebugSessionToken session);
4344

4445
private:
4546
int addPage(std::shared_ptr<Connection> conn);

ReactCommon/hermes/inspector/chrome/Registration.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ ConnectionDemux &demux() {
2222

2323
} // namespace
2424

25-
void enableDebugging(
25+
DebugSessionToken enableDebugging(
2626
std::unique_ptr<RuntimeAdapter> adapter,
2727
const std::string &title) {
28-
demux().enableDebugging(std::move(adapter), title);
28+
return demux().enableDebugging(std::move(adapter), title);
2929
}
3030

31-
void disableDebugging(HermesRuntime &runtime) {
32-
demux().disableDebugging(runtime);
31+
void disableDebugging(DebugSessionToken session) {
32+
demux().disableDebugging(session);
3333
}
3434

3535
} // namespace chrome

ReactCommon/hermes/inspector/chrome/Registration.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,24 @@ namespace hermes {
1818
namespace inspector {
1919
namespace chrome {
2020

21+
using DebugSessionToken = int;
22+
2123
/*
2224
* enableDebugging adds this runtime to the list of debuggable JS targets
23-
* (called "pages" in the higher-leavel React Native API) in this process. It
24-
* should be called before any JS runs in the runtime.
25+
* (called "pages" in the higher-level React Native API) in this process. It
26+
* should be called before any JS runs in the runtime. The returned token
27+
* can be used to disable debugging for this runtime.
2528
*/
26-
extern void enableDebugging(
29+
extern DebugSessionToken enableDebugging(
2730
std::unique_ptr<RuntimeAdapter> adapter,
2831
const std::string &title);
2932

3033
/*
3134
* disableDebugging removes this runtime from the list of debuggable JS targets
32-
* in this process.
35+
* in this process. The runtime to remove is identified by the token returned
36+
* from enableDebugging.
3337
*/
34-
extern void disableDebugging(HermesRuntime &runtime);
38+
extern void disableDebugging(DebugSessionToken session);
3539

3640
} // namespace chrome
3741
} // namespace inspector

ReactCommon/hermes/inspector/chrome/tests/ConnectionDemuxTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ TEST(ConnectionDemuxTests, TestEnableDisable) {
124124

125125
// Disable debugging on runtime2. This should remove its page from the list
126126
// and call onDisconnect on its remoteConn
127-
demux.disableDebugging(*runtime2);
127+
demux.disableDebugging(id2);
128128
expectPages(*inspector, {{id1, "page1"}});
129129
remoteData2->expectDisconnected();
130130

0 commit comments

Comments
 (0)