Skip to content

Commit

Permalink
[PROTON] Raise an exception if we try to activate/deactivate a sessio…
Browse files Browse the repository at this point in the history
…n that has not been initialized (#4841)
  • Loading branch information
Jokeren authored Oct 3, 2024
1 parent 112b88d commit 819338d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
12 changes: 12 additions & 0 deletions third_party/proton/csrc/lib/Session/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ makeContextSource(const std::string &contextSourceName) {
}
throw std::runtime_error("Unknown context source: " + contextSourceName);
}

void throwIfSessionNotInitialized(
const std::map<size_t, std::unique_ptr<Session>> &sessions,
size_t sessionId) {
if (!sessions.count(sessionId)) {
throw std::runtime_error("Session has not been initialized: " +
std::to_string(sessionId));
}
}

} // namespace

void Session::activate() {
Expand Down Expand Up @@ -80,6 +90,7 @@ void SessionManager::deactivateSession(size_t sessionId) {
}

void SessionManager::activateSessionImpl(size_t sessionId) {
throwIfSessionNotInitialized(sessions, sessionId);
if (activeSessions[sessionId])
return;
activeSessions[sessionId] = true;
Expand All @@ -89,6 +100,7 @@ void SessionManager::activateSessionImpl(size_t sessionId) {
}

void SessionManager::deActivateSessionImpl(size_t sessionId) {
throwIfSessionNotInitialized(sessions, sessionId);
if (!activeSessions[sessionId]) {
return;
}
Expand Down
25 changes: 25 additions & 0 deletions third_party/proton/test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,28 @@ def foo():
assert child["metrics"]["a"] == 1.0
elif child["frame"]["name"] == "test0":
assert child["metrics"]["a"] == "1"


def test_throw():
# Catch an exception thrown by c++
session_id = 100
with tempfile.NamedTemporaryFile(delete=True, suffix=".hatchet") as f:
activate_error = ""
try:
session_id = proton.start(f.name.split(".")[0])
proton.activate(session_id + 1)
except Exception as e:
activate_error = str(e)
finally:
proton.finalize()
assert "Session has not been initialized: " + str(session_id + 1) in activate_error

deactivate_error = ""
try:
session_id = proton.start(f.name.split(".")[0])
proton.deactivate(session_id + 1)
except Exception as e:
deactivate_error = str(e)
finally:
proton.finalize()
assert "Session has not been initialized: " + str(session_id + 1) in deactivate_error

0 comments on commit 819338d

Please sign in to comment.