Skip to content

Commit

Permalink
Fix timing-related crashes in the web plugin (tenzir#3553)
Browse files Browse the repository at this point in the history
This change fixes an obscure crash when a web server is shutting down
while receiving requests at the same time.
  • Loading branch information
Daniel Kostuj authored Oct 6, 2023
2 parents 3b4b4b4 + f74594e commit 8e0c76b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/next/bug-fixes/3553.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The web server will not crash when receiving requests during shutdown anymore.
4 changes: 4 additions & 0 deletions plugins/web/src/restinio_response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ restinio_response::restinio_response(request_handle_t&& handle,
response_(request_->create_response<restinio::user_controlled_output_t>()) {
response_.append_header(restinio::http_field::content_type,
content_type_to_string(endpoint.content_type));
response_.header().status_code(
restinio::http_status_code_t{restinio::status_code::internal_server_error});
}

restinio_response::~restinio_response() {
Expand All @@ -46,6 +48,8 @@ void restinio_response::append(std::string body) {
}

void restinio_response::finish(caf::expected<std::string> body) {
response_.header().status_code(
restinio::http_status_code_t{restinio::status_code::ok});
auto text = body ? *body : fmt::format("{}", body.error());
body_size_ += text.size();
response_.append_body(std::move(text));
Expand Down
3 changes: 2 additions & 1 deletion plugins/web/src/server_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,14 +463,15 @@ auto server_command(const tenzir::invocation& inv, caf::actor_system& system)
return stop;
});
// Shutdown
self->send_exit(dispatcher, caf::exit_reason::user_shutdown);
self->wait_for(dispatcher);
std::visit(
detail::overload{
[](auto& server) {
restinio::initiate_shutdown(*server);
},
},
server);
self->send_exit(dispatcher, caf::exit_reason::user_shutdown);
for (auto& handler : handlers)
self->send_exit(handler, caf::exit_reason::user_shutdown);
server_thread.join();
Expand Down

0 comments on commit 8e0c76b

Please sign in to comment.