AlejandroMorgante opened a new pull request, #60228:
URL: https://github.com/apache/spark/pull/60228
### What changes were proposed in this pull request?
`awaitTermination`/`awaitAnyTermination` (classic
`StreamingQuery`/`StreamingQueryManager` and their Spark Connect equivalents)
reject a `timeout` with a comparison against `0`, but every comparison against
`nan`/`inf` is `False`, so both silently pass validation. They then blow up
with a raw, unwrapped Python error when converted to milliseconds: `int(nan *
1000)` raises `ValueError: cannot convert float NaN to integer`, `int(inf *
1000)` raises `OverflowError: cannot convert float infinity to integer`. This
PR explicitly rejects both with the existing `VALUE_NOT_POSITIVE` condition,
before the millisecond conversion.
It also switches that millisecond conversion from `int()` (truncating) to
`math.ceil()` (rounding up), so a tiny positive timeout like `0.0001` seconds
no longer rounds down to `0`ms -- which some backends then reject outright as
"has to be positive" -- and instead becomes `1`ms.
### Why are the changes needed?
Passing `nan` or `inf` currently crashes with a raw Python exception instead
of a clear PySpark error, which is confusing and inconsistent with how every
other invalid `timeout` is reported in this codebase. The rounding issue is a
silent correctness bug: a caller who deliberately passes a small positive
timeout gets `0`ms instead, which some backends treat specially or reject,
surprising the caller with an error that doesn't match what they passed.
This was flagged during review on #60215 (a sibling PR that splits the
type-vs-value check for a different, unrelated bug in the same validation
code); it is pre-existing and not introduced by that PR.
### Does this PR introduce _any_ user-facing change?
Yes.
- Before: `query.awaitTermination(float("nan"))` raised a raw `ValueError:
cannot convert float NaN to integer`; `query.awaitTermination(float("inf"))`
raised a raw `OverflowError: cannot convert float infinity to integer`. After:
both raise `[VALUE_NOT_POSITIVE] Value for `timeout` must be positive, got
'float'.` (the same condition already used for other invalid timeouts in this
code; a sibling PR, #60193, separately fixes that message to report the actual
value instead of the type name).
- Before: `query.awaitTermination(0.0001)` sent a `0`ms timeout to the
JVM/Connect backend, which could then reject it as non-positive. After: it
sends `1`ms, honoring the caller's (tiny but positive) request. Timeouts with
no meaningful sub-millisecond remainder (the common case) are unaffected.
### How was this patch tested?
Added
`pyspark.sql.tests.streaming.test_streaming_query_validation.StreamingQueryTimeoutValidationTests`
(8 tests, covering classic and Connect, the Connect ones guarded by
`should_test_connect`): four assert that `nan`/`inf` raise `VALUE_NOT_POSITIVE`
via `checkError`, and four assert that `0.0001` converts to `1`ms by
substituting a fake JVM/Connect handle and capturing the millisecond value it
receives. None of these require a `SparkSession`. Also manually verified normal
values (`5`, `2.6`) still convert to the same millisecond value as before.
Verified locally with `python -m unittest` and `ruff check`; the full CI suite
will run on this PR.
### Was this patch authored or co-authored using generative AI tooling?
Yes. Generated-by: Claude Code (Claude Sonnet 5)
---
This contribution is my original work and I license the work to the project
under the project's open source license.
(First-time contributors here, still learning the contribution flow — happy
to adjust anything that's off.)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]