From 5c03d92457590575af5a8af44f8b4f7c85e63e89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Fri, 29 Sep 2017 18:43:13 +0200 Subject: [PATCH] Distributed_db: fix concurency issue Concurent request of the same ressource might insert multiple 'Pending request' in the request tracking table. Resulting, only one of them will ever be satisfied and some worker might be stuck for ever. We avoid this be removing any cooperation between lookup and insertion in the table. --- src/node/shell/distributed_db_functors.ml | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/node/shell/distributed_db_functors.ml b/src/node/shell/distributed_db_functors.ml index 26c9bcaa9..8b70525fc 100644 --- a/src/node/shell/distributed_db_functors.ml +++ b/src/node/shell/distributed_db_functors.ml @@ -151,14 +151,23 @@ end = struct match Memory_table.find s.memory k with | exception Not_found -> begin Disk_table.read_opt s.disk k >>= function - | None -> - let waiter, wakener = Lwt.wait () in - Memory_table.add s.memory k (Pending (wakener, param)) ; - Scheduler.request s.scheduler peer k ; - waiter | Some v -> Lwt.return v + | None -> + match Memory_table.find s.memory k with + | exception Not_found -> begin + let waiter, wakener = Lwt.wait () in + Memory_table.add s.memory k (Pending (wakener, param)) ; + Scheduler.request s.scheduler peer k ; + waiter + end + | Pending (w, _) -> + Scheduler.request s.scheduler peer k ; + Lwt.waiter_of_wakener w + | Found v -> Lwt.return v end - | Pending (w, _) -> Lwt.waiter_of_wakener w + | Pending (w, _) -> + Scheduler.request s.scheduler peer k ; + Lwt.waiter_of_wakener w | Found v -> Lwt.return v let prefetch s ?peer k param = Lwt.ignore_result (fetch s ?peer k param)