Skip to content
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
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ change updates the page that describes it, not the README.
## Commands

```sh
make setup # once: the switch hypha-postgres-eio, all dependencies
make setup # once: the switch postgres-eio, all dependencies
make test # starts the test Postgres in Podman, runs every test
make lint # formatting, odoc, the release build, and Hypha's rules by litany
make fmt # format in place
Expand Down
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changes

## 0.5.5 (2026-12-03)

- A statement given up on, by a timeout, a cancellation or a raise from the
caller's callback, is cancelled at the server before the connection's
socket closes, not after: a pooling proxy can drop a cancel that arrives
after the client has disconnected. The connection takes no other
statement meanwhile.

## 0.5.4 (2026-12-03)

- A connection that fails while it is being opened keeps the backtrace of
Expand Down
6 changes: 3 additions & 3 deletions agents/pages/guide/copy.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ let export conn sink =

`copy_in` streams an Eio source, and `copy_out` folds each chunk into
`chunk` as it arrives. Any other statement is refused, after it has run.
If `chunk` raises, the connection is closed, the statement cancelled at
the server, and the exception propagates. A cancellation of `copy_in`
closes the connection.
If `chunk` raises, the statement is cancelled at the server, the
connection closed, and the exception propagates. A cancellation of
`copy_in` closes the connection.

A COPY given to `query` or `script` is refused, and the connection stays
usable.
7 changes: 5 additions & 2 deletions agents/pages/guide/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ string and parameters. A pool makes a closed connection again by itself at
its next lend.

On a timeout, a cancellation, or an exception from a callback, the running
statement is first cancelled at the server, as `cancel` does; the request
is given 5 s, or the connection's timeout if that is shorter.
statement is cancelled at the server, as `cancel` does; the request is
given 5 s, or the connection's timeout if that is shorter. The connection
takes no other statement from then on, and its socket closes once the
request is done, since a pooling proxy drops a cancel for a client already
gone.

## The server's error

Expand Down
2 changes: 1 addition & 1 deletion agents/pages/guide/queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ let recent conn ~since ~limit =
```

Rows are folded as they arrive and never buffered. If `row` raises, the
connection is closed, the statement cancelled at the server, and the
statement is cancelled at the server, the connection closed, and the
exception propagates.

`columns`, when given, is called once before any row, even with no rows,
Expand Down
6 changes: 3 additions & 3 deletions docs/src/content/docs/guide/copy.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ format, CSV say, and move its bytes unchanged:

`copy_in` streams an Eio source, and `copy_out` folds each chunk into
`chunk` as it arrives. Any other statement is refused, after it has run.
If `chunk` raises, the connection is closed, the statement cancelled at
the server, and the exception propagates. A cancellation of `copy_in`
closes the connection.
If `chunk` raises, the statement is cancelled at the server, the
connection closed, and the exception propagates. A cancellation of
`copy_in` closes the connection.

A COPY given to `query` or `script` is refused, and the connection stays
usable.
7 changes: 5 additions & 2 deletions docs/src/content/docs/guide/errors.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ string and parameters. A pool makes a closed connection again by itself at
its next lend.

On a timeout, a cancellation, or an exception from a callback, the running
statement is first cancelled at the server, as `cancel` does; the request
is given 5 s, or the connection's timeout if that is shorter.
statement is cancelled at the server, as `cancel` does; the request is
given 5 s, or the connection's timeout if that is shorter. The connection
takes no other statement from then on, and its socket closes once the
request is done, since a pooling proxy drops a cancel for a client already
gone.

## The server's error

Expand Down
2 changes: 1 addition & 1 deletion docs/src/content/docs/guide/queries.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ and reads back what Postgres writes:
<Code code={recentOrders} lang="ocaml" />

Rows are folded as they arrive and never buffered. If `row` raises, the
connection is closed, the statement cancelled at the server, and the
statement is cancelled at the server, the connection closed, and the
exception propagates.

`columns`, when given, is called once before any row, even with no rows,
Expand Down
31 changes: 21 additions & 10 deletions src/connection.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1189,20 +1189,24 @@ let cancel t = send_cancel t ~within:t.timeout

(* The answer queue *)

(* What was sent is lost for [e]; what was only queued was never sent. *)
let break t e =
(* What was sent is lost for [e]; what was only queued was never sent.
The connection takes nothing more, and the link is handed back with its
socket still open. *)
let detach t e =
match t.link with
| None -> ()
| None -> None
| Some link ->
t.link <- None;
close_quietly link.socket;
Buffer.reset link.out;
let lose q e =
Queue.iter (fun r -> r.lost e) q;
Queue.clear q
in
lose link.sent e;
lose link.unsent Closed
lose link.unsent Closed;
Some link

let break t e = Option.iter (fun link -> close_quietly link.socket) (detach t e)

(* A cancel request is a connect, perhaps a TLS handshake, and one small
message on a server that answers within milliseconds; 5 s is far past
Expand All @@ -1223,12 +1227,19 @@ let cancel_given_up t =

(* 54.2.8: a timeout, a cancellation or a raise from the caller's callback
leaves the statement running at the server, which would run it to its
end for nobody, so it is cancelled there as the connection closes,
shielded, since a cancelled caller is one it is sent for. *)
end for nobody, so it is cancelled there, shielded, since a cancelled
caller is one it is sent for. The connection takes nothing more before
the cancel goes, so the cancel can stop no statement sent after; its
socket closes only after, since a pooling proxy drops a cancel for a
client already gone. *)
let stop t e =
break t e;
Eio.Cancel.protect (fun () ->
ignore (cancel_given_up t : (unit, error) result))
let link = detach t e in
Fun.protect
~finally:(fun () ->
Option.iter (fun link -> close_quietly link.socket) link)
(fun () ->
Eio.Cancel.protect (fun () ->
ignore (cancel_given_up t : (unit, error) result)))

(* Closing, or an exception from the caller's callback, leaves what was
sent with no answer anybody reads. *)
Expand Down
4 changes: 2 additions & 2 deletions src/connection.mli
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ val query :
(** Runs one statement by the extended protocol. [params] bind [$1], [$2], ...
as text, [None] for NULL. [columns] is called once before any row, even with
no rows. [row] folds each row as it arrives ([None] is NULL); rows are not
buffered. If [row] raises, the connection is closed, the statement cancelled
at the server, and the exception propagates.
buffered. If [row] raises, the statement is cancelled at the server, the
connection closed, and the exception propagates.

[binary] (false) requests binary for the types in {!Value.binary}, text for
the rest; {!Value} decodes either. This needs column types before Bind,
Expand Down
76 changes: 76 additions & 0 deletions test/test_postgres_eio.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3002,6 +3002,78 @@ let a_cancel_request_left_unanswered_is_given_up () =
Alcotest.(check bool) "closed" true (Pg.closed t);
Alcotest.(check bool) "and returned before the server let go" false !let_go

(* The cancel for a statement given up on reaches the server while the
session it names is still open: a pooling proxy can drop a cancel that
arrives after the client has disconnected. The fake notes, as the
CancelRequest arrives, whether the session has already ended. *)
let a_cancel_is_sent_before_the_connection_closes () =
with_eio @@ fun env sw ->
let clock = Eio.Stdenv.mono_clock env in
let ended = ref false and cancelled_while_open = ref None in
let port, _ =
a_fake_postgres env sw
~on_cancel:(fun () -> cancelled_while_open := Some (not !ended))
~on_end:(fun ~terminated:_ -> ended := true)
()
in
let t = connect env sw (fake port) in
Pg.set_timeout t ~timeout:None;
Eio.Fiber.first
(fun () ->
ignore
(Pg.execute t "select pg_sleep(60)" ~params:[]
: (Pg.Tag.t, Pg.error) result))
(fun () -> Eio.Time.Mono.sleep_span clock Mtime.Span.(200 * ms));
Alcotest.(check bool) "closed" true (Pg.closed t);
Alcotest.(check (option bool))
"the cancel came while the session was open" (Some true)
!cancelled_while_open

(* While that cancel is on its way the session stays open, but the
connection takes nothing more: a statement sent then could run at the
server, be cancelled in the given-up one's place, and answer [`Lost]
where nothing was done. The fake holds the CancelRequest until another
fiber has tried a statement. *)
let nothing_is_sent_while_a_cancel_is_on_its_way () =
with_eio @@ fun env sw ->
let clock = Eio.Stdenv.mono_clock env in
let cancelling, now_cancelling = Eio.Promise.create () in
let tried, now_tried = Eio.Promise.create () in
let queries = ref [] in
let port, _ =
a_fake_postgres env sw
~on_query:(fun sql ->
queries := sql :: !queries;
"")
~on_cancel:(fun () ->
Eio.Promise.resolve now_cancelling ();
Eio.Promise.await tried)
()
in
let t = connect env sw (fake port) in
Pg.set_timeout t ~timeout:None;
let answer = ref None in
Eio.Fiber.both
(fun () ->
Eio.Fiber.first
(fun () ->
ignore
(Pg.execute t "select pg_sleep(60)" ~params:[]
: (Pg.Tag.t, Pg.error) result))
(fun () -> Eio.Time.Mono.sleep_span clock Mtime.Span.(200 * ms)))
(fun () ->
Eio.Promise.await cancelling;
Eio.Fiber.first
(fun () -> answer := Some (Pg.script t "select 1"))
(fun () -> Eio.Time.Mono.sleep_span clock Mtime.Span.(200 * ms));
Eio.Promise.resolve now_tried ());
Alcotest.(check bool)
"refused as closed" true
(match !answer with
| Some (Error (`Connection `Closed)) -> true
| Some _ | None -> false);
Alcotest.(check (list string)) "and never sent" [] !queries

(* target_session_attrs: read-write skips a standby; standby finds one. *)
let the_kind_of_host_asked_for () =
with_eio @@ fun env sw ->
Expand Down Expand Up @@ -4817,6 +4889,10 @@ let connection_cases =
a_raising_row_function_is_cancelled_at_the_server;
Alcotest.test_case "54.2.8 a cancel request left unanswered is given up"
`Quick a_cancel_request_left_unanswered_is_given_up;
Alcotest.test_case "54.2.8 a cancel is sent before the connection closes"
`Quick a_cancel_is_sent_before_the_connection_closes;
Alcotest.test_case "54.2.8 nothing is sent while a cancel is on its way"
`Quick nothing_is_sent_while_a_cancel_is_on_its_way;
Alcotest.test_case "a reset keeps its parameters" `Quick
a_reset_keeps_its_parameters;
Alcotest.test_case "a silent server times out" `Quick
Expand Down