Skip to content

Commit 697b56e

Browse files
committed
Protect cancel with a separate mutex + finishing bool removed
1 parent 2eb75e2 commit 697b56e

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

lib/postgresql.ml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -967,9 +967,9 @@ module Connection (Mutex : Mutex) = struct
967967
else Gc.finalise Stub.finish my_conn
968968
in
969969
let conn_mtx = Mutex.create () in
970-
let finishing = ref false in
970+
let cancel_mtx = Mutex.create () in
971971
let check_null () =
972-
if !finishing || Stub.conn_isnull my_conn then
972+
if Stub.conn_isnull my_conn then
973973
failwith "Postgresql.check_null: connection already finished"
974974
in
975975
let wrap_conn f =
@@ -981,11 +981,20 @@ module Connection (Mutex : Mutex) = struct
981981
f my_conn)
982982
~finally:(fun _ -> Mutex.unlock conn_mtx)
983983
in
984+
let wrap_cancel f =
985+
protectx
986+
~f:(fun _ ->
987+
Mutex.lock cancel_mtx;
988+
check_null ();
989+
(* Check again in case the world has changed *)
990+
f my_conn)
991+
~finally:(fun _ -> Mutex.unlock cancel_mtx)
992+
in
984993
let signal_error conn =
985994
raise (Error (Connection_failure (Stub.error_message conn)))
986995
in
987996
let request_cancel () =
988-
wrap_conn (fun _ ->
997+
wrap_cancel (fun _ ->
989998
match Stub.request_cancel my_conn with
990999
| None -> ()
9911000
| Some err -> raise (Error (Cancel_failure err)))
@@ -1012,8 +1021,7 @@ module Connection (Mutex : Mutex) = struct
10121021
object (self (* Main routines *))
10131022
method finish =
10141023
wrap_conn (fun c ->
1015-
Stub.finish c;
1016-
finishing := true)
1024+
Stub.finish c)
10171025

10181026
method try_reset =
10191027
wrap_conn (fun conn ->

0 commit comments

Comments
 (0)