From 003d3d3817d1aa70532bdc088a099e25d040832b Mon Sep 17 00:00:00 2001 From: sanguinor88 <188408926+Sanguinor88@users.noreply.github.com> Date: Thu, 3 Dec 2026 21:53:33 +0000 Subject: [PATCH 1/2] Send the CancelRequest before closing the socket A statement given up on, by a timeout, a cancellation or a raise from the caller's callback, was cancelled at the server only after the connection's socket had closed. A pooling proxy finds the session a cancel names by its client, and drops one for a client that has already disconnected, so the statement ran on for nobody. The socket now closes only once the cancel is done. The connection is still detached first, its unsent statements refused as closed: were it to take statements while the cancel is on its way, one could run at the server, be cancelled in the given-up one's place, or answer as lost where nothing was meant to be sent. --- CHANGES.md | 8 +++ agents/pages/guide/copy.md | 6 +- agents/pages/guide/errors.md | 7 ++- agents/pages/guide/queries.md | 2 +- docs/src/content/docs/guide/copy.mdx | 6 +- docs/src/content/docs/guide/errors.mdx | 7 ++- docs/src/content/docs/guide/queries.mdx | 2 +- src/connection.ml | 31 ++++++---- src/connection.mli | 4 +- test/test_postgres_eio.ml | 76 +++++++++++++++++++++++++ 10 files changed, 125 insertions(+), 24 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 873ba1f..c9e458f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,13 @@ # 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'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 diff --git a/agents/pages/guide/copy.md b/agents/pages/guide/copy.md index b00e70c..01002cc 100644 --- a/agents/pages/guide/copy.md +++ b/agents/pages/guide/copy.md @@ -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. diff --git a/agents/pages/guide/errors.md b/agents/pages/guide/errors.md index 9fe2b87..b65bb98 100644 --- a/agents/pages/guide/errors.md +++ b/agents/pages/guide/errors.md @@ -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 diff --git a/agents/pages/guide/queries.md b/agents/pages/guide/queries.md index c0fea06..506d5f3 100644 --- a/agents/pages/guide/queries.md +++ b/agents/pages/guide/queries.md @@ -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, diff --git a/docs/src/content/docs/guide/copy.mdx b/docs/src/content/docs/guide/copy.mdx index 7462d7d..ccfd7b1 100644 --- a/docs/src/content/docs/guide/copy.mdx +++ b/docs/src/content/docs/guide/copy.mdx @@ -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. diff --git a/docs/src/content/docs/guide/errors.mdx b/docs/src/content/docs/guide/errors.mdx index cc74a37..6bfba90 100644 --- a/docs/src/content/docs/guide/errors.mdx +++ b/docs/src/content/docs/guide/errors.mdx @@ -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 diff --git a/docs/src/content/docs/guide/queries.mdx b/docs/src/content/docs/guide/queries.mdx index ae7ae26..2f9ea5d 100644 --- a/docs/src/content/docs/guide/queries.mdx +++ b/docs/src/content/docs/guide/queries.mdx @@ -22,7 +22,7 @@ and reads back what Postgres writes: 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, diff --git a/src/connection.ml b/src/connection.ml index 25986f5..5753125 100644 --- a/src/connection.ml +++ b/src/connection.ml @@ -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 @@ -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. *) diff --git a/src/connection.mli b/src/connection.mli index 2c6cf12..f56321e 100644 --- a/src/connection.mli +++ b/src/connection.mli @@ -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, diff --git a/test/test_postgres_eio.ml b/test/test_postgres_eio.ml index a4d45e0..562e4ea 100644 --- a/test/test_postgres_eio.ml +++ b/test/test_postgres_eio.ml @@ -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 -> @@ -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 From 4b7da34266a7e2074078da2c58706157e1bf424f Mon Sep 17 00:00:00 2001 From: Roman Zaiev Date: Thu, 3 Dec 2026 22:01:56 +0000 Subject: [PATCH 2/2] Release 0.5.5, and name the switch postgres-eio in AGENTS.md --- AGENTS.md | 2 +- CHANGES.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 7a9baa6..45fffbc 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -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 diff --git a/CHANGES.md b/CHANGES.md index c9e458f..2bf35e1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,6 @@ # Changes -## Unreleased +## 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