mengxr commented on a change in pull request #24986: [SPARK-28185][ML][SQL]
Trigger pandas iterator UDF closing stuff when iterator stop early
URL: https://github.com/apache/spark/pull/24986#discussion_r298195130
##########
File path: python/pyspark/sql/tests/test_pandas_udf_scalar.py
##########
@@ -850,6 +850,36 @@ def test_close(batch_iter):
with self.assertRaisesRegexp(Exception, "reached finally block"):
self.spark.range(1).select(test_close(col("id"))).collect()
+ def test_scalar_iter_udf_close_early(self):
+ tmp_dir = tempfile.mkdtemp()
+
+ @pandas_udf('int', PandasUDFType.SCALAR_ITER)
+ def test_close(batch_iter):
+ generator_exit_caught = False
+ try:
+ for batch in batch_iter:
+ yield batch
+ time.sleep(0.5)
+ except GeneratorExit as ge:
+ generator_exit_caught = True
+ raise ge
+ finally:
+ assert generator_exit_caught, "Generator exit exception was
not caught."
+ open(tmp_dir + '/reach_finally_block', 'a').close()
+
+ with QuietTest(self.sc):
+ with
self.sql_conf({"spark.sql.execution.arrow.maxRecordsPerBatch": 1,
+ "spark.sql.pandas.udf.buffer.size": 4}):
+ self.spark.range(10).repartition(1) \
+ .select(test_close(col("id"))).limit(2).collect()
+ # sleep here because python udf worker will take some time to
detect
+ # jvm side socket closed and then will trigger `GenerateExit`
raised.
+ time.sleep(3.0)
Review comment:
Change it to wait until timeout pattern.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]