The Seq.toDS and spark.createDataset(Seq) code is not thread-safe: if the caller-supplied Encoder is used in multiple threads then createDataset's usage of the encoder may lead to incorrect answers because the Encoder's internal mutable state will be updated by from multiple threads.
Here is an example demonstrating the problem:
Due to the thread-safety issue, the above example results in the creation of corrupted records where different input records' fields are co-mingled.
This bug is similar to
SPARK-22355, a related problem in Dataset.collect() (fixed in Spark 2.2.1+).
Fortunately, this has a simple one-line fix (copy the encoder); I'll submit a patch for this shortly.