I've been running into a lot of headaches trying to get newVariableInstance to correctly handle suspensions. Currently when a newVariableInstance statement is found, a NewVariableInstanceStart and End unparsers are created. NewVariableInstanceStart will immediately create the newVariableInstance with no value and, if applicable, will create a SuspendableExpression that will calculate the default value. This works as expected but as the NVI's go out of scope we start running into some issues.
The NewVariableIsntanceEnd unparser simply removes the variable instance that was created in NVIStart. It is not performing or checking for any sort of suspension, so NVIEnd is called while the NVIStart suspension is still active. Since the UStateForSuspension object uses the same VariableMap as the main UState object, this results in the variable's value not being correct after it goes out of scope. I'm not sure what a good solution for this would be. I've attempted adding a SuspendableOperation to the NVIEnd unparser to wait until the variable has a value before removing, but this results in a SuspensionDeadlock. I've also attempted doing a deep copy of all the variables for each UStateForSuspension object, but this too results in a SuspensionDeadlock, not to mention that any changes made in one suspension wouldn't be visible to other suspensions. One other thought I had, which I'm not even sure would even address the suspension issue, is to have whatever sequence is containing the NVI statement handle calling the NVIEnd unparser by simply adding it to the end of its sequence child unparsers. Any thoughts on how best to handle this sticky situation of dealing with newVariableInstance and unparsing suspensions?