mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-20 02:55:38 +08:00
Merge pull request #3623 from HerrCai0907/fix/command_shell_use_lot_of_resource
fix: `call_command_posix` in socket mode should return after child process finished
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <sys/errno.h>
|
||||
#include <sys/socket.h>
|
||||
#ifndef __APPLE__
|
||||
#include <sys/prctl.h>
|
||||
#endif
|
||||
@@ -1361,7 +1362,7 @@ std::optional<int> asst::Controller::call_command_win32(const std::string& cmd,
|
||||
|
||||
asst::platform::single_page_buffer<char> pipe_buffer;
|
||||
asst::platform::single_page_buffer<char> sock_buffer;
|
||||
|
||||
|
||||
HANDLE pipe_parent_read = INVALID_HANDLE_VALUE, pipe_child_write = INVALID_HANDLE_VALUE;
|
||||
SECURITY_ATTRIBUTES sa_inherit { .nLength = sizeof(SECURITY_ATTRIBUTES), .bInheritHandle = TRUE };
|
||||
if (!asst::win32::CreateOverlappablePipe(&pipe_parent_read, &pipe_child_write, nullptr, &sa_inherit,
|
||||
@@ -1590,36 +1591,37 @@ std::optional<int> asst::Controller::call_command_posix(const std::string& cmd,
|
||||
}
|
||||
else if (m_child > 0) {
|
||||
// parent process
|
||||
do {
|
||||
if (recv_by_socket) {
|
||||
sockaddr addr {};
|
||||
socklen_t len = sizeof(addr);
|
||||
sock_buffer = asst::platform::single_page_buffer<char>();
|
||||
if (recv_by_socket) {
|
||||
sockaddr addr {};
|
||||
socklen_t len = sizeof(addr);
|
||||
sock_buffer = asst::platform::single_page_buffer<char>();
|
||||
|
||||
int client_socket = ::accept(m_server_sock, &addr, &len);
|
||||
if (client_socket < 0) {
|
||||
Log.error("accept failed:", strerror(errno));
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
ssize_t read_num = ::read(client_socket, sock_buffer.get(), sock_buffer.size());
|
||||
|
||||
while (read_num > 0) {
|
||||
sock_data.insert(sock_data.end(), sock_buffer.get(), sock_buffer.get() + read_num);
|
||||
read_num = ::read(client_socket, sock_buffer.get(), sock_buffer.size());
|
||||
}
|
||||
|
||||
::close(client_socket);
|
||||
break;
|
||||
int client_socket = ::accept(m_server_sock, &addr, &len);
|
||||
if (client_socket < 0) {
|
||||
Log.error("accept failed:", strerror(errno));
|
||||
::kill(m_child, SIGKILL);
|
||||
::waitpid(m_child, &exit_ret, 0);
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
ssize_t read_num = ::read(m_pipe_out[PIPE_READ], pipe_buffer.get(), pipe_buffer.size());
|
||||
|
||||
ssize_t read_num = ::read(client_socket, sock_buffer.get(), sock_buffer.size());
|
||||
while (read_num > 0) {
|
||||
pipe_data.insert(pipe_data.end(), pipe_buffer.get(), pipe_buffer.get() + read_num);
|
||||
read_num = ::read(m_pipe_out[PIPE_READ], pipe_buffer.get(), pipe_buffer.size());
|
||||
sock_data.insert(sock_data.end(), sock_buffer.get(), sock_buffer.get() + read_num);
|
||||
read_num = ::read(client_socket, sock_buffer.get(), sock_buffer.size());
|
||||
}
|
||||
} while (::waitpid(m_child, &exit_ret, WNOHANG) == 0 && !check_timeout());
|
||||
::shutdown(client_socket, SHUT_RDWR);
|
||||
::close(client_socket);
|
||||
}
|
||||
else {
|
||||
do {
|
||||
ssize_t read_num = ::read(m_pipe_out[PIPE_READ], pipe_buffer.get(), pipe_buffer.size());
|
||||
while (read_num > 0) {
|
||||
pipe_data.insert(pipe_data.end(), pipe_buffer.get(), pipe_buffer.get() + read_num);
|
||||
read_num = ::read(m_pipe_out[PIPE_READ], pipe_buffer.get(), pipe_buffer.size());
|
||||
}
|
||||
} while (::waitpid(m_child, &exit_ret, WNOHANG) == 0 && !check_timeout());
|
||||
}
|
||||
::waitpid(m_child, &exit_ret, 0); // if ::waitpid(m_child, &exit_ret, WNOHANG) == 0, repeat it will cause ECHILD, so not check the return value
|
||||
}
|
||||
else {
|
||||
// failed to create child process
|
||||
|
||||
Reference in New Issue
Block a user