drogonframework / drogonframework/drogon

newStreamResponse sometimes stops calling the callback

Open
#2,432 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
14.3k
Forks
1.4k
Avg merge
1d 12h
Merged PRs (30d)
15

Description

Hi,

I'm trying to use newStreamResponse and the header says:

> * @param callback function to retrieve the stream data (stream ends when a
> * zero size is returned) the callback will be called with
> * nullptr when the send is finished/interrupted so that it
> * cleans up its internals.

But sometimes the callback is not called and my program gets stuck.
Here is the simplest code I can write to reproduce :

```
#include

std::thread worker;

using namespace drogon;

struct {
std::atomic ready{ false }, done{ false };
std::function callback;
} stream;

static std::chrono::milliseconds pause;

void sendresponse() {
while (not stream.ready) std::this_thread::sleep_for(std::chrono::milliseconds(100));

printf("Handling request\n");
auto cb = [](char* buffer, std::size_t size) {
printf("size: %lld, buffer: %p\n", size, buffer);
if (size == 0) { stream.done = true; return (size_t) 0; };
std::this_thread::sleep_for(pause);
memset(buffer, 'A', size);
return size;
};

auto resp = drogon::HttpResponse::newStreamResponse(cb);
stream.callback(resp);
while (not stream.done) std::this_thread::sleep_for(std::chrono::milliseconds(100));
printf("Done\n");
app().quit();
}

int main(int arc, char* argv[]) {
pause = std::chrono::milliseconds(std::atoi(argv[1]));
worker = std::thread(sendresponse);
app().registerHandler(
"/",
[](const HttpRequestPtr &request,
std::function &&callback) {
stream.callback = callback;
stream.ready = true;
},
{Get});
app().addListener("0.0.0.0", 80).run();
worker.join();
return 0;
}
```

On an other computer I launch "curl -o dummy" and press Ctrl-C after a few seconds.

If launched with 1000 as argument I have these prints

> Handling request
> 20260105 15:42:39.646000 UTC 15692 DEBUG [makeHeaderString] send stream with transfer-encoding chunked - HttpResponseImpl.cc:547
> size: 16384, buffer: 000001D3B70D9FAE
> size: 16384, buffer: 000001D3B70D9FAE
> size: 0, buffer: 0000000000000000
> Done

If launched with 10 as argument I have these prints

> Handling request
> 20260105 15:44:04.411000 UTC 12272 DEBUG [makeHeaderString] send stream with transfer-encoding chunked - HttpResponseImpl.cc:547
> size: 16384, buffer: 000002A60B395FCE
> size: 16384, buffer: 000002A60B395FCE
> size: 16384, buffer: 000002A60B395FCE
> size: 16384, buffer: 000002A60B395FCE
> size: 16384, buffer: 000002A60B395FCE
> size: 16384, buffer: 000002A60B395FCE
> size: 16384, buffer: 000002A60B395FCE

--> Output hangs: the callback is not called anymore

If curl is launched from the same computer where drogon is running, everything works fine

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.