Joel
Joel

Reputation: 23140

In Corda flow, java.lang.IllegalStateException: Attempted to initiateFlow() twice in the same InitiatingFlow

I have the following Corda flow, where I pass in a list of Partys and attempt to initiate a flow session with each one:

@InitiatingFlow
@StartableByRPC
class MyFlow(val parties: List<Party>) : FlowLogic<Unit>() {
    @Suspendable
    override fun call() {
        // Flow logic.

        val flowSessions = parties.forEach { party ->
            initiateFlow(party)
        }

        // Flow logic.
    }
}

However, when I call it, I get the following exception:

java.lang.IllegalStateException: Attempted to initiateFlow() twice in the same InitiatingFlow com.template.TwoTransactionsFlow@1ba8d137 for the same party O=PartyB, L=London, C=GB. This isn't supported in this version of Corda. Alternatively you may initiate a new flow by calling initiateFlow() in an @InitiatingFlow sub-flow.

What is the cause of this exception?

Upvotes: 1

Views: 199

Answers (1)

Joel
Joel

Reputation: 23140

This exception indicates that you have attempted to initiate a flow session twice with the same party within the same flow context, in this case because the parties list you're passing into the flow contains duplicates. This is not allowed.

You should either:

  1. Re-use existing sessions with a given party (i.e. create a single FlowSession with PartyA and use the same flow session to send PartyA information twice)
  2. Subflow into another @InitiatingFlow and create the FlowSession there (each @InitiatingFlow and its in-lined subflows share the same flow context)

Upvotes: 1

Related Questions