Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronious write stream handling fix #1000

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions include/crow/http_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -545,19 +545,25 @@ namespace crow

inline void do_write_sync(std::vector<asio::const_buffer>& buffers)
{
error_code ec;
asio::write(adaptor_.socket(), buffers, ec);

asio::write(adaptor_.socket(), buffers, [&](error_code ec, std::size_t) {
if (!ec)
{
return false;
}
else
{
CROW_LOG_ERROR << ec << " - happened while sending buffers";
CROW_LOG_DEBUG << this << " from write (sync)(2)";
return true;
}
});
this->res.clear();
this->res_body_copy_.clear();
if (this->continue_requested)
{
this->continue_requested = false;
}
else
{
this->parser_.clear();
}

if (ec)
{
CROW_LOG_ERROR << ec << " - happened while sending buffers";
CROW_LOG_DEBUG << this << " from write (sync)(2)";
}
}

void cancel_deadline_timer()
Expand Down
12 changes: 12 additions & 0 deletions tests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2171,15 +2171,21 @@ TEST_CASE("middleware_session")
TEST_CASE("bug_quick_repeated_request")
{
SimpleApp app;
std::uint8_t explicitTimeout = 200;
app.timeout(explicitTimeout);

CROW_ROUTE(app, "/")
([&] {
std::this_thread::sleep_for(std::chrono::seconds(1));
return "hello";
});

auto _ = app.bindaddr(LOCALHOST_ADDRESS).port(45451).run_async();
app.wait_for_server_start();
std::string sendmsg = "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n";

auto start = std::chrono::high_resolution_clock::now();

asio::io_context ic;
{
std::vector<std::future<void>> v;
Expand All @@ -2198,6 +2204,12 @@ TEST_CASE("bug_quick_repeated_request")
}));
}
}

auto end = std::chrono::high_resolution_clock::now();
std::chrono::seconds testDuration = std::chrono::duration_cast<std::chrono::seconds>(end - start);

CHECK(testDuration.count() < explicitTimeout);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is a time of one second and 200s really needed for the test?
Lets assume a 100ms delay, then we would at least end up with 20s (worst case).


app.stop();
} // bug_quick_repeated_request

Expand Down
Loading