fix crash when server crashes

This commit is contained in:
Green Sky 2024-04-19 11:35:04 +02:00
parent 4a2adff0c8
commit 8fc197423a
No known key found for this signature in database
3 changed files with 19 additions and 9 deletions

View File

@ -32,6 +32,9 @@ if (SOLANACEAE_SDBOT_WEBUI_STANDALONE)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
endif() endif()
#add_compile_options(-fsanitize=undefined)
#link_libraries(-fsanitize=undefined)
# external libs # external libs
add_subdirectory(./external EXCLUDE_FROM_ALL) # before increasing warn levels, sad :( add_subdirectory(./external EXCLUDE_FROM_ALL) # before increasing warn levels, sad :(

View File

@ -194,8 +194,8 @@ float SDBot::iterate(void) {
_current_task = std::nullopt; _current_task = std::nullopt;
} }
if (_current_task.has_value()) { if (_curr_future.has_value()) {
_curr_future.reset(); _curr_future.reset(); // might block and wait
} }
} }
@ -206,8 +206,8 @@ float SDBot::iterate(void) {
if (_cli == nullptr) { if (_cli == nullptr) {
const std::string server_host {_conf.get_string("SDBot", "server_host").value()}; const std::string server_host {_conf.get_string("SDBot", "server_host").value()};
_cli = std::make_unique<httplib::Client>(server_host, _conf.get_int("SDBot", "server_port").value()); _cli = std::make_shared<httplib::Client>(server_host, _conf.get_int("SDBot", "server_port").value());
_cli->set_read_timeout(std::chrono::minutes(5)); _cli->set_read_timeout(std::chrono::minutes(2)); // because of discarding futures, it can block main for a while
} }
nlohmann::json j_body; nlohmann::json j_body;
@ -244,10 +244,17 @@ float SDBot::iterate(void) {
try { try {
const std::string url {_conf.get_string("SDBot", "url_txt2img").value()}; const std::string url {_conf.get_string("SDBot", "url_txt2img").value()};
_curr_future = std::async(std::launch::async, [this, url, body]() -> std::vector<uint8_t> { _curr_future = std::async(std::launch::async, [url, body, cli = _cli]() -> std::vector<uint8_t> {
if (!static_cast<bool>(cli)) {
return {};
}
// TODO: move to endpoint // TODO: move to endpoint
auto res = _cli->Post(url, body, "application/json"); auto res = cli->Post(url, body, "application/json");
std::cout << "SDB http complete " << res->status << " " << res->reason << "\n"; if (!static_cast<bool>(res)) {
std::cerr << "SDB error: post to sd server failed!\n";
return {};
}
std::cerr << "SDB http complete " << res->status << " " << res->reason << "\n";
if ( if (
res.error() != httplib::Error::Success || res.error() != httplib::Error::Success ||
res->status != 200 res->status != 200
@ -262,7 +269,7 @@ float SDBot::iterate(void) {
// cleanup // cleanup
_task_map.erase(_current_task.value()); _task_map.erase(_current_task.value());
_current_task = std::nullopt; _current_task = std::nullopt;
_curr_future.reset(); _curr_future.reset(); // might block and wait
} }
_prompt_queue.pop(); _prompt_queue.pop();

View File

@ -31,7 +31,7 @@ class SDBot : public RegistryMessageModelEventI {
uint64_t _last_task_counter = 0; uint64_t _last_task_counter = 0;
std::optional<uint64_t> _current_task; std::optional<uint64_t> _current_task;
std::unique_ptr<httplib::Client> _cli; std::shared_ptr<httplib::Client> _cli;
std::optional<std::future<std::vector<uint8_t>>> _curr_future; std::optional<std::future<std::vector<uint8_t>>> _curr_future;
std::default_random_engine _rng; std::default_random_engine _rng;