Most HTTP handlers return a bare Req from init/2: the request process crashes after the reply and the connection stays open #27

Closed
opened 2026-12-01 14:38:00 +00:00 by saturnus · 1 comment
Member

Purpose (fix): every HTTP request ends cleanly. Today most handlers' request processes crash after the reply, and the connection is left open despite connection: close.

Found while probing #26 (Saturnus, 2026-12-01), on main c191c6b; the code is the same on 15752f4.

What happens. Cowboy's init/2 must return {ok, Req, State}. 15 of the 17 *_api.erl handlers return the reply Req alone (mcl_rag_http:ok_json/2 and the rest return a Req): add_knowledge, answer_query, classify_topics, detect_corpus_change, embed_document, get_chunk_by_id, get_source_by_id, ingest_document, list_chunks_by_source, list_sources_page, prune_chunks, rerank_results, retire_document, schedule_reembed, upload_knowledge. Only search_chunks_semantic and seed_corpus return {ok, Req, State}.

The reply is sent first, so callers get the right status and body. Then cowboy_handler:execute/2 fails with try_clause on the returned map. Every such request:

  • logs an error report (Ranch listener mcl_rag_http_listener ... exit with reason {{try_clause, ...);
  • leaves its connection open, even when the request said connection: close. In a probe on a CT listener, 203 of 203 requests to /api/rag/chunks/by-source and /api/rag/knowledge/add never closed; each client read had to time out.

Why it matters. A client that reads until close (instead of by content-length) hangs. Every call costs an error report in the log, which hides real crashes. Open connections pile up until Cowboy's idle timeout.

Done when:

  1. Every handler's init/2 returns {ok, Req, State}.
  2. A case fails on a handler that returns a bare Req: for example, a request with connection: close to each route must see the socket closed promptly, and no try_clause report is logged.

Refs #26

Purpose (fix): every HTTP request ends cleanly. Today most handlers' request processes crash after the reply, and the connection is left open despite `connection: close`. Found while probing #26 (Saturnus, 2026-12-01), on main c191c6b; the code is the same on 15752f4. **What happens.** Cowboy's `init/2` must return `{ok, Req, State}`. 15 of the 17 `*_api.erl` handlers return the reply `Req` alone (`mcl_rag_http:ok_json/2` and the rest return a `Req`): add_knowledge, answer_query, classify_topics, detect_corpus_change, embed_document, get_chunk_by_id, get_source_by_id, ingest_document, list_chunks_by_source, list_sources_page, prune_chunks, rerank_results, retire_document, schedule_reembed, upload_knowledge. Only search_chunks_semantic and seed_corpus return `{ok, Req, State}`. The reply is sent first, so callers get the right status and body. Then `cowboy_handler:execute/2` fails with `try_clause` on the returned map. Every such request: - logs an error report (`Ranch listener mcl_rag_http_listener ... exit with reason {{try_clause, ...`); - leaves its connection open, even when the request said `connection: close`. In a probe on a CT listener, 203 of 203 requests to `/api/rag/chunks/by-source` and `/api/rag/knowledge/add` never closed; each client read had to time out. **Why it matters.** A client that reads until close (instead of by content-length) hangs. Every call costs an error report in the log, which hides real crashes. Open connections pile up until Cowboy's idle timeout. Done when: 1. Every handler's `init/2` returns `{ok, Req, State}`. 2. A case fails on a handler that returns a bare `Req`: for example, a request with `connection: close` to each route must see the socket closed promptly, and no `try_clause` report is logged. Refs #26
Author
Member

Done-when, read on main 9b83f9eb9c (PR 29, Jupiter PASS r1):

  1. Every HTTP handler's init/2 returns {ok, Req, State}: all 17 handlers (9b83f9e).
  2. A case per handler (fec5619) sends a request with connection: close, waits for the socket to close, and checks that nothing is logged at error level. A handler reverted to a bare Req turns its own case red (Jupiter's mutant read).
    CI on the tip: c4-model 6413, lint 6414.
Done-when, read on main 9b83f9eb9c5bb05493c6b02ec870f2f6deb55404 (PR 29, Jupiter PASS r1): 1. Every HTTP handler's `init/2` returns `{ok, Req, State}`: all 17 handlers (9b83f9e). 2. A case per handler (fec5619) sends a request with `connection: close`, waits for the socket to close, and checks that nothing is logged at error level. A handler reverted to a bare `Req` turns its own case red (Jupiter's mutant read). CI on the tip: c4-model 6413, lint 6414.
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
macula-services/mcl-rag#27
No description provided.