Skip to content

Commit

Permalink
fix lock leak in session_container_impl
Browse files Browse the repository at this point in the history
  • Loading branch information
t-horikawa committed Apr 18, 2024
1 parent d7514a5 commit 4d78934
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/tateyama/session/resource/container_impl.cpp
Expand Up @@ -19,8 +19,18 @@
namespace tateyama::session::resource {

bool session_container_impl::register_session(std::shared_ptr<session_context_impl> const& session) {
foreach([](const std::shared_ptr<session_context>&) {});
std::unique_lock lock{mtx_};

for (auto&& it = session_contexts_.begin(), last = session_contexts_.end(); it != last;) {
if (auto sp = (*it).lock(); sp) {
++it;
} else {
it = session_contexts_.erase(it);
}
}
if (session_contexts_.find(session) != session_contexts_.end()) {
return false;
}
session_contexts_.emplace(session);
return true;
}
Expand Down
18 changes: 13 additions & 5 deletions src/tateyama/session/resource/container_impl.h
Expand Up @@ -47,19 +47,27 @@ class session_container_impl : public tateyama::session::session_container {
*/
virtual ~session_container_impl() = default;

bool register_session(std::shared_ptr<session_context_impl> const& session);

void foreach(const std::function<void(const std::shared_ptr<session_context>&)>& func) override;

session_container_impl(session_container_impl const&) = delete;
session_container_impl(session_container_impl&&) = delete;
session_container_impl& operator = (session_container_impl const&) = delete;
session_container_impl& operator = (session_container_impl&&) = delete;

/**
* @brief registers a new session context.
* @param session the session context to register
* @return true if the target session is successfully registered
* @return false if the target session is not registered
* because another session with such the numeric ID already exists in this container
* @note Session labels may duplicate in this container
*/
bool register_session(std::shared_ptr<session_context_impl> const& session);

void foreach(const std::function<void(const std::shared_ptr<session_context>&)>& func) override;

private:
std::set<std::weak_ptr<session_context_impl>, address_compare> session_contexts_{};

std::mutex mtx_{};
mutable std::mutex mtx_{};
};

}

0 comments on commit 4d78934

Please sign in to comment.