diff --git a/src/hpsh/hpsh.cpp b/src/hpsh/hpsh.cpp index 815abedc..6700190b 100644 --- a/src/hpsh/hpsh.cpp +++ b/src/hpsh/hpsh.cpp @@ -146,7 +146,14 @@ namespace hpsh return (write(ctx.control_fds[1], HPSH_CTR_TERMINATE, 10) < 0) ? -1 : 0; } - int execute(std::string_view id, std::string_view pubkey, std::string_view message) + void remove_user_commands(std::string_view user_pubkey) + { + std::scoped_lock lock(ctx.command_mutex); + ctx.commands.remove_if([&](const command_context &command) + { return command.user_pubkey == user_pubkey; }); + } + + int execute(std::string_view id, std::string_view user_pubkey, std::string_view message) { if (ctx.is_shutting_down) return -1; @@ -203,9 +210,11 @@ namespace hpsh return -1; } + close(child_fds[0]); + { std::scoped_lock lock(ctx.command_mutex); - ctx.commands.push_back(command_context{std::string(id), std::string(pubkey), {child_fds[0], child_fds[1]}, std::string(), false}); + ctx.commands.push_back(command_context{std::string(id), std::string(user_pubkey), {child_fds[0], child_fds[1]}, std::string(), false}); } return 0; @@ -219,6 +228,8 @@ namespace hpsh { if (ctx.commands.size() > 0) { + std::scoped_lock lock(ctx.command_mutex); + auto itr = ctx.commands.begin(); while (itr != ctx.commands.end()) { @@ -262,7 +273,7 @@ namespace hpsh std::scoped_lock lock(usr::ctx.users_mutex); // Find the user session by user pubkey. - const auto user_itr = usr::ctx.users.find(itr->pubkey); + const auto user_itr = usr::ctx.users.find(itr->user_pubkey); if (user_itr != usr::ctx.users.end()) // match found { const usr::connected_user &user = user_itr->second; @@ -272,10 +283,10 @@ namespace hpsh user.session.send(msg); } } - { - std::scoped_lock lock(ctx.command_mutex); - itr = ctx.commands.erase(itr); - } + + // Close the file descriptor and remove the command from context. + close(itr->child_fds[1]); + itr = ctx.commands.erase(itr); } else { @@ -283,10 +294,7 @@ namespace hpsh } } } - else - { - util::sleep(1000); - } + util::sleep(1000); } } } \ No newline at end of file diff --git a/src/hpsh/hpsh.hpp b/src/hpsh/hpsh.hpp index 11a5d7a8..24dd2c9d 100644 --- a/src/hpsh/hpsh.hpp +++ b/src/hpsh/hpsh.hpp @@ -10,7 +10,7 @@ namespace hpsh struct command_context { std::string id; - std::string pubkey; + std::string user_pubkey; int child_fds[2]; std::string response; bool read_completed = false; @@ -37,7 +37,9 @@ namespace hpsh int send_terminate_message(); - int execute(std::string_view id, std::string_view pubkey, std::string_view message); + void remove_user_commands(std::string_view user_pubkey); + + int execute(std::string_view id, std::string_view user_pubkey, std::string_view message); void response_watcher(); } diff --git a/src/usr/usr.cpp b/src/usr/usr.cpp index 096646dd..14a6760b 100644 --- a/src/usr/usr.cpp +++ b/src/usr/usr.cpp @@ -420,8 +420,14 @@ namespace usr */ int remove_user(const std::string &pubkey) { - std::scoped_lock lock(ctx.users_mutex); - ctx.users.erase(pubkey); + { + std::scoped_lock lock(ctx.users_mutex); + ctx.users.erase(pubkey); + } + // Remove any hpsh commands sent by the user. + if (hpsh::ctx.is_initialized) + hpsh::remove_user_commands(pubkey); + return 0; } diff --git a/test/bin/hpsh b/test/bin/hpsh index 4a225764..46426404 100755 Binary files a/test/bin/hpsh and b/test/bin/hpsh differ