Skip to content

Commit 819338d

Browse files
authored
[PROTON] Raise an exception if we try to activate/deactivate a session that has not been initialized (#4841)
1 parent 112b88d commit 819338d

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

third_party/proton/csrc/lib/Session/Session.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ makeContextSource(const std::string &contextSourceName) {
4040
}
4141
throw std::runtime_error("Unknown context source: " + contextSourceName);
4242
}
43+
44+
void throwIfSessionNotInitialized(
45+
const std::map<size_t, std::unique_ptr<Session>> &sessions,
46+
size_t sessionId) {
47+
if (!sessions.count(sessionId)) {
48+
throw std::runtime_error("Session has not been initialized: " +
49+
std::to_string(sessionId));
50+
}
51+
}
52+
4353
} // namespace
4454

4555
void Session::activate() {
@@ -80,6 +90,7 @@ void SessionManager::deactivateSession(size_t sessionId) {
8090
}
8191

8292
void SessionManager::activateSessionImpl(size_t sessionId) {
93+
throwIfSessionNotInitialized(sessions, sessionId);
8394
if (activeSessions[sessionId])
8495
return;
8596
activeSessions[sessionId] = true;
@@ -89,6 +100,7 @@ void SessionManager::activateSessionImpl(size_t sessionId) {
89100
}
90101

91102
void SessionManager::deActivateSessionImpl(size_t sessionId) {
103+
throwIfSessionNotInitialized(sessions, sessionId);
92104
if (!activeSessions[sessionId]) {
93105
return;
94106
}

third_party/proton/test/test_api.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,28 @@ def foo():
146146
assert child["metrics"]["a"] == 1.0
147147
elif child["frame"]["name"] == "test0":
148148
assert child["metrics"]["a"] == "1"
149+
150+
151+
def test_throw():
152+
# Catch an exception thrown by c++
153+
session_id = 100
154+
with tempfile.NamedTemporaryFile(delete=True, suffix=".hatchet") as f:
155+
activate_error = ""
156+
try:
157+
session_id = proton.start(f.name.split(".")[0])
158+
proton.activate(session_id + 1)
159+
except Exception as e:
160+
activate_error = str(e)
161+
finally:
162+
proton.finalize()
163+
assert "Session has not been initialized: " + str(session_id + 1) in activate_error
164+
165+
deactivate_error = ""
166+
try:
167+
session_id = proton.start(f.name.split(".")[0])
168+
proton.deactivate(session_id + 1)
169+
except Exception as e:
170+
deactivate_error = str(e)
171+
finally:
172+
proton.finalize()
173+
assert "Session has not been initialized: " + str(session_id + 1) in deactivate_error

0 commit comments

Comments
 (0)