From 51800e06dec91282b81fc41e56d1e9325849d2c2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ludovic=20Court=C3=A8s?= <ludo@gnu.org>
Date: Tue, 18 Mar 2014 23:17:14 +0100
Subject: [PATCH] Allow recovery from isValidPath RPCs with an invalid path
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Currently, clients cannot recover from an isValidPath RPC with an
invalid path parameter because the daemon closes the connection when
that happens.

More precisely:

  1. in performOp, wopIsValidPath case, ‘readStorePath’ raises an
     ‘Error’ exception;

  2. that exception is caught by the handler in ‘processConnection’;

  3. the handler determines errorAllowed == false, and thus exits after
     sending the message.

This last part is fixed by calling ‘startWork’ early on, as in the patch
below.

The same reasoning could be applied to all the RPCs that take one or
more store paths as inputs, but isValidPath is, by definition, likely to
be passed invalid paths in the first place, so it’s important for this
one to allow recovery.
---
 src/nix-daemon/nix-daemon.cc | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/nix-daemon/nix-daemon.cc b/src/nix-daemon/nix-daemon.cc
index 882078c08..e678c9dfd 100644
--- a/src/nix-daemon/nix-daemon.cc
+++ b/src/nix-daemon/nix-daemon.cc
@@ -294,8 +294,14 @@ static void performOp(bool trusted, unsigned int clientVersion,
 #endif
 
     case wopIsValidPath: {
-        Path path = readStorePath(from);
+	/* 'readStorePath' could raise an error leading to the connection
+	   being closed.  To be able to recover from an invalid path error,
+	   call 'startWork' early, and do 'assertStorePath' afterwards so
+	   that the 'Error' exception handler doesn't close the
+	   connection.  */
+        Path path = readString(from);
         startWork();
+	assertStorePath(path);
         bool result = store->isValidPath(path);
         stopWork();
         writeInt(result, to);
-- 
GitLab