From e3a7c0bf31e8a51ed51bfac273ca5d43247e8a7f Mon Sep 17 00:00:00 2001 From: sanguinor88 <188408926+Sanguinor88@users.noreply.github.com> Date: Wed, 11 Nov 2026 19:34:31 +0800 Subject: [PATCH] Send the CancelRequest before closing the socket When a statement was given up on (a timeout, a cancellation, or a raise from the caller's callback), stop closed the connection first and only then sent the CancelRequest for it. The server saw the session end before the cancel arrived, and a pooling proxy can drop a cancel for a client session that has already disconnected, so the statement went on running for nobody. stop now sends the cancel first and closes the connection in a finally, so the connection is closed whatever the cancel does. The cancel opens a connection of its own and reads nothing from this one's stream, which no exchange reads once it is given up on; it keeps its shield and its bound of 5 s or the timeout, so a caller waits no longer than before, and a failed cancel is still ignored. The new case, 54.2.8 a cancel is sent before the connection closes, has a fake server note whether the session is still open when the CancelRequest arrives. The docs now give the order: cancelled, then closed. Co-Authored-By: Claude Opus 5.5 --- CHANGES.md | 7 +++++++ docs/src/content/docs/guide/copy.mdx | 6 +++--- docs/src/content/docs/guide/errors.mdx | 5 +++-- docs/src/content/docs/guide/queries.mdx | 2 +- src/connection.ml | 10 +++++++--- src/connection.mli | 4 ++-- test/test_postgres_eio.ml | 26 +++++++++++++++++++++++++ 7 files changed, 49 insertions(+), 11 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index b09f44e..fe53b40 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 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..76ca5e3 100644 --- a/docs/src/content/docs/guide/errors.mdx +++ b/docs/src/content/docs/guide/errors.mdx @@ -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 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 b201b64..6cd0637 100644 --- a/src/connection.ml +++ b/src/connection.ml @@ -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. *) diff --git a/src/connection.mli b/src/connection.mli index feaff3c..ec7b9c9 100644 --- a/src/connection.mli +++ b/src/connection.mli @@ -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, diff --git a/test/test_postgres_eio.ml b/test/test_postgres_eio.ml index 2e9e12b..b3569aa 100644 --- a/test/test_postgres_eio.ml +++ b/test/test_postgres_eio.ml @@ -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 -> @@ -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