Skip to content

Commit 2f224d4

Browse files
committed
cherry picked "synchronous Write Stream handling fix"
1 parent 00ec9a8 commit 2f224d4

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

Diff for: include/crow/http_connection.h

+18-12
Original file line numberDiff line numberDiff line change
@@ -545,19 +545,25 @@ namespace crow
545545

546546
inline void do_write_sync(std::vector<asio::const_buffer>& buffers)
547547
{
548+
error_code ec;
549+
asio::write(adaptor_.socket(), buffers, ec);
548550

549-
asio::write(adaptor_.socket(), buffers, [&](error_code ec, std::size_t) {
550-
if (!ec)
551-
{
552-
return false;
553-
}
554-
else
555-
{
556-
CROW_LOG_ERROR << ec << " - happened while sending buffers";
557-
CROW_LOG_DEBUG << this << " from write (sync)(2)";
558-
return true;
559-
}
560-
});
551+
this->res.clear();
552+
this->res_body_copy_.clear();
553+
if (this->continue_requested)
554+
{
555+
this->continue_requested = false;
556+
}
557+
else
558+
{
559+
this->parser_.clear();
560+
}
561+
562+
if (ec)
563+
{
564+
CROW_LOG_ERROR << ec << " - happened while sending buffers";
565+
CROW_LOG_DEBUG << this << " from write (sync)(2)";
566+
}
561567
}
562568

563569
void cancel_deadline_timer()

Diff for: tests/unittest.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -2183,15 +2183,21 @@ TEST_CASE("middleware_session")
21832183
TEST_CASE("bug_quick_repeated_request")
21842184
{
21852185
SimpleApp app;
2186+
std::uint8_t explicitTimeout = 200;
2187+
app.timeout(explicitTimeout);
21862188

21872189
CROW_ROUTE(app, "/")
21882190
([&] {
2191+
std::this_thread::sleep_for(std::chrono::seconds(1));
21892192
return "hello";
21902193
});
21912194

21922195
auto _ = app.bindaddr(LOCALHOST_ADDRESS).port(45451).run_async();
21932196
app.wait_for_server_start();
21942197
std::string sendmsg = "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n";
2198+
2199+
auto start = std::chrono::high_resolution_clock::now();
2200+
21952201
asio::io_context ic;
21962202
{
21972203
std::vector<std::future<void>> v;
@@ -2210,6 +2216,12 @@ TEST_CASE("bug_quick_repeated_request")
22102216
}));
22112217
}
22122218
}
2219+
2220+
auto end = std::chrono::high_resolution_clock::now();
2221+
std::chrono::seconds testDuration = std::chrono::duration_cast<std::chrono::seconds>(end - start);
2222+
2223+
CHECK(testDuration.count() < explicitTimeout);
2224+
22132225
app.stop();
22142226
} // bug_quick_repeated_request
22152227

0 commit comments

Comments
 (0)