Skip to content
Open
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
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changes

## Unreleased

- 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
closes, not after: a pooling proxy can drop a cancel that arrives after
the client has disconnected.

## 0.5.2 (2026-11-10)

- postgres-eio is published to open-opam, `opam repo add open-opam
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.
5 changes: 3 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,9 @@ 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 first cancelled at the server, as `cancel` does, and the
connection closed after; the request is given 5 s, or the connection's
timeout if that is shorter.

## 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
10 changes: 7 additions & 3 deletions src/connection.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1217,9 +1217,13 @@ let cancel_given_up t =
end for nobody, so it is cancelled there as the connection closes,
shielded, since a cancelled caller is one it is sent for. *)
let stop t e =
break t e;
Eio.Cancel.protect (fun () ->
ignore (cancel_given_up t : (unit, error) result))
(* Cancel before closing: a pooling proxy can drop a cancel request for a
client session that has already disconnected. *)
Fun.protect
~finally:(fun () -> break t e)
(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 @@ -138,8 +138,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
26 changes: 26 additions & 0 deletions test/test_postgres_eio.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2987,6 +2987,30 @@ 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:[]))
(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

(* 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 @@ -4794,6 +4818,8 @@ 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 "a reset keeps its parameters" `Quick
a_reset_keeps_its_parameters;
Alcotest.test_case "a silent server times out" `Quick
Expand Down