# Sandbox GRPC exception : "Failed to parse submission"

**URL:** <https://forum.canton.network/t/sandbox-grpc-exception-failed-to-parse-submission/2595>\
**Category:** App Development\
**Tags:** sandbox\
**Created:** [May 12, 2021, 4:47pm UTC](https://forum.canton.network/t/sandbox-grpc-exception-failed-to-parse-submission/2595 "2021-05-12T16:47:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Weir](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/brian_weir/32/71_2.png) [@Brian\_Weir](https://forum.canton.network/u/Brian_Weir)\
**Post date:** [May 12, 2021, 4:47pm UTC](https://forum.canton.network/t/sandbox-grpc-exception-failed-to-parse-submission/2595/1 "2021-05-12T16:47:15Z")

</div>

Hello team,

I’m encountering a GRPC exception when submitting a single create command to a locally running sandbox :

> ERROR: Internal error: Failed to parse submission, correlationId=a147bf5f-ea20-4cb2-bce3-63e71bccd4e5 (context: {readAs=, submittedAt=2021-05-12T16:16:51.066Z, applicationId=daml-script, deduplicateUntil=2021-05-13T16:16:51.066Z, actAs=[Buyer], commandId=a147bf5f-ea20-4cb2-bce3-63e71bccd4e5})  
> ERROR: Unhandled internal error (context: {readAs=, submittedAt=2021-05-12T16:16:51.066Z, applicationId=daml-script, deduplicateUntil=2021-05-13T16:16:51.066Z, actAs=[Buyer], commandId=a147bf5f-ea20-4cb2-bce3-63e71bccd4e5})  
> io.grpc.StatusRuntimeException: INTERNAL: Failed to parse submission, correlationId=a147bf5f-ea20-4cb2-bce3-63e71bccd4e5  
> at io.grpc.Status.asRuntimeException(Status.java:525)  
> at com.daml.platform.apiserver.services.ApiSubmissionService.handleSubmissionResult(ApiSubmissionService.scala:167)  
> at com.daml.platform.apiserver.services.ApiSubmissionService.$anonfun$deduplicateAndRecordOnLedger$2(ApiSubmissionService.scala:138)  
> at scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:33)  
> at scala.concurrent.impl.Promise.$anonfun$transform$1(Promise.scala:33)  
> at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:64)  
> at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1402)  
> at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)  
> at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)  
> at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)  
> at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:175)  
> WARN: Service responded with error for submitting command with context (Future(),a147bf5f-ea20-4cb2-bce3-63e71bccd4e5). Status of command is unknown. watching for completion…  
> io.grpc.StatusRuntimeException: INTERNAL: Failed to parse submission, correlationId=a147bf5f-ea20-4cb2-bce3-63e71bccd4e5  
> at io.grpc.Status.asRuntimeException(Status.java:525)  
> at com.daml.platform.apiserver.services.ApiSubmissionService.handleSubmissionResult(ApiSubmissionService.scala:167)  
> at com.daml.platform.apiserver.services.ApiSubmissionService.$anonfun$deduplicateAndRecordOnLedger$2(ApiSubmissionService.scala:138)  
> at scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:33)  
> at scala.concurrent.impl.Promise.$anonfun$transform$1(Promise.scala:33)  
> at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:64)  
> at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1402)  
> at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)  
> at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)  
> at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)  
> at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:175)

To recreate this issue, update the “unrollDates” function at [contingent-claims](https://github.com/digital-asset/contingent-claims/blob/66e4e064c4283ef24f7e4bb77e05d3aba418e14e/test/daml/Test/Initialization.daml#L24) to “… (unrollDates 2021 **2030** …” followed by building and starting the sandbox (“daml start --sandbox-option “-s””)

I have tried playing with some of the sandbox configurations, such as “–max-inbound-message-size” but I cannot get elevate this exception.

Thank you in advance for all of your help!

Kind Regards,  
Brian

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [May 12, 2021, 5:32pm UTC](https://forum.canton.network/t/sandbox-grpc-exception-failed-to-parse-submission/2595/2 "2021-05-12T17:32:02Z")

</div>

Hi @Brian_Weir,  
This example hits the protobuf recursion limit. With some manually added log statements (which should clearly not be required so we’ll look into that) you get a failure of the form

```haskell
Protocol message had too many levels of nesting. May be malicious. Use CodedInputStream.setRecursionLimit() to increase the depth limit.

```

Having taken a quick look at the code it seems like the list produced by `unrollDates` gets turned into something like

```haskell
a `And` b ` c `And`

```

with the level of nesting of that tree structure corresponding to the length of the list. Protobuf limits the recursion levels (I think 100 is the default) so this blows up.

Afaik you cannot bump this limit yourself at the moment and that only really shifts the limit rather than solving the problem.

To solve this reliably you have to avoid deeply nested structures. There are a few options here depending on your app:

1. Lists are represented as lists in protobuf not as recursive structures so use those instead of your own recursive structures where it makes sense. E.g, in your example having an `And` structure that has a list of children instead of composing it out of binary operators might work.
2. Balancing trees can reduce the recursion depth, e.g, instead of `And a (And b (And c d))` use `And (And a b) (And c d)`.
3. If all else fails, you can manually add an indirection: Instead of referencing your children directly, have a `Map NodeId Node` and then each node references `NodeId`s as the children instead of `Node`s.

As mentioned above we will definitely look into improving the error here.

---

<div class="post-metadata">

**Author:** ![gerolf](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gerolf/32/358_2.png) [@gerolf](https://forum.canton.network/u/gerolf)\
**Post date:** [May 12, 2021, 6:08pm UTC](https://forum.canton.network/t/sandbox-grpc-exception-failed-to-parse-submission/2595/3 "2021-05-12T18:08:02Z")

</div>

Thanks for flagging this, @Brian_Weir. And thanks for looking into it, @cocreature. The error should definitely be caught and reported not as `Internal Error` but something more sensible.

---

<div class="post-metadata">

**Author:** ![Brian\_Weir](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/brian_weir/32/71_2.png) [@Brian\_Weir](https://forum.canton.network/u/Brian_Weir)\
**Post date:** [May 12, 2021, 6:52pm UTC](https://forum.canton.network/t/sandbox-grpc-exception-failed-to-parse-submission/2595/4 "2021-05-12T18:52:06Z")

</div>

Thanks @cocreature for your quick reply and feedback on how to avoid this issue, very much appreciated!!

---

<div class="post-metadata">

**Author:** ![Brian\_Weir](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/brian_weir/32/71_2.png) [@Brian\_Weir](https://forum.canton.network/u/Brian_Weir)\
**Post date:** [May 17, 2021, 10:01am UTC](https://forum.canton.network/t/sandbox-grpc-exception-failed-to-parse-submission/2595/5 "2021-05-17T10:01:58Z")

</div>

> [@cocreature](#):
>
> Afaik you cannot bump this limit yourself at the moment and that only really shifts the limit rather than solving the problem.

Hi @cocreature, I’ve discussed this with the team and they’ve raised a valid point that we (and our clients) should be able to configure the “Recursion Limit”. For example, there’s other initiatives outside of the `Contingent Claims` such as `ISDA` which contain recursive data structures which could also run into this limit.

The documentation of `Protobuf` states the main purpose of this recursive limit is (see [here](https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.io.coded_stream#recursion-limit)):

> To prevent corrupt or malicious messages from causing stack overflows, we must keep track of the depth of recursion when parsing embedded messages and groups.

If a client has a valid use case for having a recursive structure, I don’t believe we should force them to change their implementation due to the default setup of the underlying serialisation library, especially since the purpose of the limit is to `prevent corrupt or malicious messages`.

Do you agree that we should open a GitHub issue to make the recursion limit configurable by the client ?

On a side note, when I create a data structure with more levels of recursion, I get a different error message :

> Caused by: io.grpc.StatusRuntimeException: INVALID\_ARGUMENT: Command interpretation error in LF-DAMLe: Provided value exceeds maximum nesting level of 100

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [May 17, 2021, 11:18am UTC](https://forum.canton.network/t/sandbox-grpc-exception-failed-to-parse-submission/2595/6 "2021-05-17T11:18:49Z")

</div>

I don’t think adding a method to configure this solves the issue for a few reasons:

1. You’ve now made your code non-portable. In general, users don’t have the option to configure the limit. An obvious example of that is Daml Hub but it’s by far not the only one. In a distributed setting you probably don’t control the settings on other participants. And even in a centralized setting the person that controls things like CLI flags can be different from the developers that produce & upload their Daml code.
2. Deeply nested values often have relatively poor performance properties.
3. While you can bump the protobuf limit there are other places where we do not support arbitrarily deep nesting and you will run into stackoverflow or similar.

---

<div class="post-metadata">

**Author:** ![Luciano](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/luciano/32/319_2.png) [@Luciano](https://forum.canton.network/u/Luciano)\
**Post date:** [May 17, 2021, 11:23am UTC](https://forum.canton.network/t/sandbox-grpc-exception-failed-to-parse-submission/2595/7 "2021-05-17T11:23:13Z")

</div>

Am I right in guessing that this

> [@cocreature](#):
>
> Afaik you cannot bump this limit yourself

means it’s a constant somewhere in protobuf? And that means that changing it would require re-compiling the protobuf dependency?

And furthermore, if that is the case, would it mean that we would break compatibility with other protobuf-compliant programs (e.g. if somebody decided to write their own GRPC client instead of using the official one)?

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [May 17, 2021, 11:27am UTC](https://forum.canton.network/t/sandbox-grpc-exception-failed-to-parse-submission/2595/8 "2021-05-17T11:27:34Z")

</div>

> [@Luciano](#):
>
> means it’s a constant somewhere in protobuf? And that means that changing it would require re-compiling the protobuf dependency?

No, it doesn’t require patching the protobuf library at least not for Java. You can configure it but not without changing the code of your ledger (e.g., sandbox) and recompiling it. For `grpc-java`, you configure it via `CodedInputStream::setRecursionLimit`. Similar on the client side.

I don’t think protobuf specifies a general limit and e.g., the Haskell implementation has no recursion limits at all afaik. That said, Java has the limit and most other bindings (e.g. python) go via the C/C++ library which also has a limit (which is also configurable I believe) so you do have to make sure they sync up but you can change it.

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stephen/32/102_2.png) [@Stephen](https://forum.canton.network/u/Stephen)\
**Post date:** [May 17, 2021, 7:02pm UTC](https://forum.canton.network/t/sandbox-grpc-exception-failed-to-parse-submission/2595/9 "2021-05-17T19:02:12Z")

</div>

With the development of Daml-LF, we realized the various problems with setting very large recursion limits, such as (2) and (3) mentioned by @cocreature, and decided to

1. set a “reasonable” limit,
2. set it as the limit consistently, and
3. declare values that exceed the limit as _not Daml_.

In light of this choice, I would think of having options to set the recursion limit as akin to `-fdefer-type-errors` in GHC. Sure, it disables an error, but that does not mean the thing you have successfully run is valid code/payload, and you shouldn’t be surprised at strange errors occurring at strange times.
