[SPARK-2315] ensure that functions are cleaned