This is an automated email from the ASF dual-hosted git repository.
nickva pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb.git
The following commit(s) were added to refs/heads/main by this push:
new 4b76c3a90 Fix eventsource empty row
4b76c3a90 is described below
commit 4b76c3a902683a8dd6d79fc69a94fbeab002c549
Author: Nick Vatamaniuc <[email protected]>
AuthorDate: Thu Nov 12 18:52:31 2026 -0500
Fix eventsource empty row
An empty chunk in mochiweb is how we tell is the chunked response ended. So
if
we still have to send one final newline ,like we do in the case of
eventsource
to end the stream, we ended the response too early.
This was not noticeable with curl, but it was noticeable in the logs or if
there is a subsequent request on the same connection.
We caught this in the server-side gzip compression PR during the review
since
there the gzip compressor threw an error in the log:
```
curl -XPUT
http://adm:[email protected]:15984/_node/_local/_config/chttpd/response_compression
-d '"true"'
curl -XPUT http://adm:[email protected]:15984/db
curl --compressed
'http://adm:[email protected]:15984/db/_changes?feed=eventsource&timeout=1000'
```
[1] https://github.com/apache/couchdb/pull/6122#issuecomment-6783113475
log
```
[error] 2026-11-11T00:21:04.711596Z [email protected] <0.12581.0> f59f4733a4
req_err(2997429273) unknown_error : not_initialized
[<<"zlib:enqueue_nif/2">>,<<"zlib:enqueue_input_1/2">>,<<"zlib:deflate/3">>,<<"mochiweb_response:write_chunk/2
L129">>,<<"couch_httpd:send_chunk/2">>,<<"couch_httpd:end_json_response/1
L958">>,<<"fabric
_view_changes:go/5 L68">>,<<"chttpd_db:handle_changes_req1/2 L173">>]
```
The bug however, is not gzip related per-se. It was a latent bug lingering
in
the main code base for a while, that why we're fixing it separately here.
---
src/chttpd/src/chttpd.erl | 4 ++++
src/chttpd/src/chttpd_db.erl | 2 ++
src/chttpd/test/eunit/chttpd_changes_test.erl | 27 ++++++++++++++++++++++++++-
3 files changed, 32 insertions(+), 1 deletion(-)
diff --git a/src/chttpd/src/chttpd.erl b/src/chttpd/src/chttpd.erl
index 751cf1201..bbf88db8e 100644
--- a/src/chttpd/src/chttpd.erl
+++ b/src/chttpd/src/chttpd.erl
@@ -955,6 +955,10 @@ send_delayed_error(#delayed_resp{resp = Resp, req = Req},
Reason) ->
log_error_with_stack_trace(Reason),
throw({http_abort, Resp, Reason}).
+close_delayed_json_object(Resp, [], Terminator, 0) ->
+ % Nothing buffered. An empty chunk here would end the response before the
+ % terminator goes out.
+ send_delayed_chunk(Resp, Terminator);
close_delayed_json_object(Resp, Buffer, Terminator, 0) ->
% Use a separate chunk to close the streamed array to maintain strict
% compatibility with earlier versions. See COUCHDB-2724
diff --git a/src/chttpd/src/chttpd_db.erl b/src/chttpd/src/chttpd_db.erl
index 6c29c83f0..9a9fd2c67 100644
--- a/src/chttpd/src/chttpd_db.erl
+++ b/src/chttpd/src/chttpd_db.erl
@@ -238,6 +238,8 @@ changes_callback({timeout, Seq}, #cacc{feed = eventsource,
heartbeat_seq = true}
end;
changes_callback({timeout, _Seq}, #cacc{feed = eventsource} = Acc) ->
maybe_heartbeat(Acc, "event: heartbeat\ndata: \n\n");
+changes_callback({stop, _EndSeq, _Pending}, #cacc{feed = eventsource, buffer =
[]} = Acc) ->
+ chttpd:end_delayed_json_response(Acc#cacc.mochi);
changes_callback({stop, _EndSeq, _Pending}, #cacc{feed = eventsource} = Acc) ->
#cacc{mochi = Resp, buffer = Buf} = Acc,
{ok, Resp1} = chttpd:send_delayed_chunk(Resp, Buf),
diff --git a/src/chttpd/test/eunit/chttpd_changes_test.erl
b/src/chttpd/test/eunit/chttpd_changes_test.erl
index 356767823..3fc7cf0fc 100644
--- a/src/chttpd/test/eunit/chttpd_changes_test.erl
+++ b/src/chttpd/test/eunit/chttpd_changes_test.erl
@@ -71,10 +71,12 @@ changes_test_() ->
?TDEF(t_eventsource_heartbeat_plain),
?TDEF(t_continuous_heartbeat_seq),
?TDEF(t_eventsource_heartbeat_seq),
+ ?TDEF(t_eventsource_timeout),
?TDEF(t_limit_one),
?TDEF(t_since_now),
?TDEF(t_continuous_since_now),
?TDEF(t_longpoll_since_now),
+ ?TDEF(t_zero_buffer),
?TDEF(t_style_all_docs),
?TDEF(t_reverse),
?TDEF(t_continuous_reverse),
@@ -539,6 +541,16 @@ t_eventsource_heartbeat_seq({_, DbUrl}) ->
#{id := LastEventId} = lists:last(Events),
?assertMatch(#{id := LastEventId}, lists:last(WithId)).
+t_eventsource_timeout({_, DbUrl}) ->
+ % When feed stop on nothing more left to send, there should still be a
final newline sent.
+ % In other words it should end on: 'data:...\n\n\n'
+ % On idle (no changes) it would be \n on start and then a final \n so
`\n\n`.
+ Idle = changes_raw(DbUrl, "?feed=eventsource&timeout=10&since=now"),
+ ?assertEqual(~"\n\n", Idle),
+ WithData = changes_raw(DbUrl, "?feed=eventsource&timeout=10"),
+ ?assertEqual(3, length(binary:matches(WithData, ~"data: {"))),
+ ?assertEqual(~"\n\n\n", binary:part(WithData, byte_size(WithData), -3)).
+
t_heartbeat_seq_normal({_, DbUrl}) ->
% Db is small and with default times we don't get any heartbeats
?assertEqual(changes(DbUrl), changes(DbUrl, "?heartbeat_seq=true")),
@@ -592,6 +604,17 @@ t_longpoll_since_now({_, DbUrl}) ->
Params = "?feed=longpoll&timeout=10&since=now",
?assertEqual({8, 0, []}, changes(DbUrl, Params)).
+t_zero_buffer({_, DbUrl}) ->
+ % If the user choses not to buffer, the end should still go out as its
+ % own chunk. When not more rows left and empty chunk sent would terminate
+ % the respone prematurily.
+ Expect = changes(DbUrl),
+ config:set("chttpd", "chunked_response_buffer", "0", false),
+ ?assertEqual(Expect, changes(DbUrl)),
+ ?assertEqual({8, 0, []}, changes(DbUrl, "?since=now")),
+ ?assertEqual({8, 0, []}, changes(DbUrl,
"?feed=longpoll&timeout=10&since=now")),
+ config:delete("chttpd", "chunked_response_buffer", false).
+
t_reverse({_, DbUrl}) ->
Params = "?descending=true",
{Seq, Pending, Rows} = changes(DbUrl, Params),
@@ -1314,6 +1337,7 @@ setup_basic() ->
ok = create_docs(DbUrl, test_docs()),
CfgKey = "changes_doc_ids_optimization_threshold",
ok = config:set("couchdb", CfgKey, "2", _Persist = false),
+ ok = config:set("chttpd", "chunked_response_buffer", "1490", false),
meck:new(couch_changes, [passthrough]),
meck:new(couch_db, [passthrough]),
{Ctx, DbUrl}.
@@ -1321,6 +1345,7 @@ setup_basic() ->
teardown_basic({Ctx, DbUrl}) ->
CfgKey = "changes_doc_ids_optimization_threshold",
ok = config:delete("couchdb", CfgKey, _Persist = false),
+ ok = config:delete("chttpd", "chunked_response_buffer", false),
teardown_ctx({Ctx, DbUrl}).
create_db(Top, Db, Params) ->
@@ -1488,7 +1513,7 @@ eventsource(DbUrl, Params) ->
{Code, Res} = reqraw(get, DbUrl ++ "/_changes" ++ Params),
?assertEqual(200, Code),
Chunks = binary:split(Res, ~"\n\n", [global]),
- Events = [parse_event(C) || C <- Chunks, C =/= <<>>],
+ Events = [parse_event(C) || C <- Chunks, C =/= ~"", C =/= ~"\n"],
lists:partition(fun(#{event := E}) -> E =/= ~"heartbeat" end, Events).
parse_event(Chunk) ->