I think you also need to call `StateTransferLock.waitForTopology(command.getTopologyId() + 1)` or something like that to avoid resending the prepare with the same topology id. 
In 5.1 we had a similar retry mechanism and StateTransferInProgressException had a field with the expected topology in it, but topology differences > 1 should be very rare so that might be overkill.