Fix crash when execing invalid RPC

When executing an invalid RPC, the temporary datastore wasn't being
properly canceled.

Issue: https://tree.taiga.io/project/jktjkt-netconf-cli/issue/209
Change-Id: I56e301c509781275bd960f4cd846d649507a875d
diff --git a/src/proxy_datastore.cpp b/src/proxy_datastore.cpp
index f212d45..8f86831 100644
--- a/src/proxy_datastore.cpp
+++ b/src/proxy_datastore.cpp
@@ -5,6 +5,7 @@
  *
 */
 #include <boost/algorithm/string/predicate.hpp>
+#include "UniqueResource.hpp"
 #include "proxy_datastore.hpp"
 #include "yang_schema.hpp"
 
@@ -74,10 +75,10 @@
     if (!m_inputDatastore) {
         throw std::runtime_error("No RPC/action input in progress");
     }
+    auto cancelOnReturn = make_unique_resource([] {}, [this] { cancel(); });
     auto inputData = m_inputDatastore->getItems("/");
 
     auto out = m_datastore->execute(*m_inputPath, inputData);
-    cancel();
 
     return out;
 }
diff --git a/tests/datastore_access.cpp b/tests/datastore_access.cpp
index ececd0c..9eeed2a 100644
--- a/tests/datastore_access.cpp
+++ b/tests/datastore_access.cpp
@@ -17,6 +17,7 @@
 using OnInvalidSchemaPathDelete = DatastoreException;
 using OnInvalidSchemaPathMove = sysrepo::sysrepo_exception;
 using OnInvalidRpcPath = sysrepo::sysrepo_exception;
+using OnInvalidRpcInput = sysrepo::sysrepo_exception;
 using OnKeyNotFound = void;
 using OnExec = void;
 #elif defined(netconf_BACKEND)
@@ -24,6 +25,7 @@
 using OnInvalidSchemaPathDelete = std::runtime_error;
 using OnInvalidSchemaPathMove = std::runtime_error;
 using OnInvalidRpcPath = std::runtime_error;
+using OnInvalidRpcInput = std::runtime_error;
 using OnKeyNotFound = std::runtime_error;
 using OnExec = void;
 #include "netconf_access.hpp"
@@ -35,6 +37,7 @@
 using OnInvalidSchemaPathDelete = DatastoreException;
 using OnInvalidSchemaPathMove = DatastoreException;
 using OnInvalidRpcPath = DatastoreException;
+using OnInvalidRpcInput = std::logic_error;
 using OnKeyNotFound = DatastoreException;
 using OnExec = std::logic_error;
 #else
@@ -1024,6 +1027,13 @@
         {
             catching<OnInvalidRpcPath>([&] { datastore->execute("/example-schema:non-existing", DatastoreAccess::Tree{}); });
         }
+
+        SECTION("invalid RPC exec resets temporary datastore")
+        {
+            proxyDatastore.initiate("/example-schema:setIp");
+            catching<OnInvalidRpcInput>([&] { auto output = proxyDatastore.execute(); });
+            REQUIRE(proxyDatastore.inputDatastorePath() == std::nullopt);
+        }
     }
 
     SECTION("action")
diff --git a/tests/example-schema.yang b/tests/example-schema.yang
index 0db4c4f..44baf80 100644
--- a/tests/example-schema.yang
+++ b/tests/example-schema.yang
@@ -167,6 +167,15 @@
 
     rpc noop {}
 
+    rpc setIp {
+      input {
+        leaf ip {
+          mandatory true;
+          type string;
+        }
+      }
+    }
+
     list selectedNumbers {
         key 'value';
         leaf value {