HyukjinKwon opened a new pull request, #778:
URL: https://github.com/apache/spark-connect-rust/pull/778
### What changes were proposed in this pull request?
A follow-up of #761 (SPARK-61755) and #765 (SPARK-61786). Since #765,
`pyspark.sql.connect.proto` always exports the messages of the drop-in's Rust
protobuf runtime (`crates/pyspark-rs/src/pbmsg`). A review found five edge
cases where it differs from upb (protobuf 7.36.2), each reproduced against upb:
| case | upb | before |
|---|---|---|
| `DiscardUnknownFields()` of a deep message (a `limit.input` chain) | works
at 20,000 and 1,000,000 levels on the main thread | SIGSEGV at 20,000 on the
main thread, at 1,500 on a 512 KiB thread |
| a message of nested dicts, `pb.Relation(**nested)` | builds 1,000 levels
on a 512 KiB thread, 20,000 on 8 MiB | SIGSEGV at 200 and 5,000 |
| `extend` of a generator that appends to the same field | `[1, 2]` |
`PanicException: Already borrowed` |
| two threads, one extending from a slow generator while one appends | no
error, 250 items | `PanicException`, 201 items |
| `append(x)` where `x.__index__` reads `len(c)`, or appends | `[0]`;
appends | `RuntimeError`; `PanicException` |
| `sort(key=...)` where the key appends | `[1, 2, 3, 9, 9, 9]` | `[1, 2,
3]`, the appends lost |
| a field number above 2^29-1, or a tag above 32 bits (field 2^32+1 read as
field 1) | `DecodeError: Wire format was corrupt` | accepted, truncated to u32 |
| a map entry with an unknown field, or with a key or value of the wrong
wire type | the map is empty, and the entry is kept, re-encoded, in the
parent's unknown fields | the entry is inserted, and its unknown field dropped |
`PanicException` derives from `BaseException`, so `except Exception` did not
catch it.
The fixes:
- **`message.rs`.** `DiscardUnknownFields` grows the stack, as every other
recursive operation of the runtime does (#677's pattern), and recurses into map
values in Rust. It stops where upb stops, discarding the unknown fields of the
message and of 62 levels below it, as probed. `init_from_mapping` builds each
level under `grow`.
- **`containers.rs`** (rewritten). No borrow is held while Python code runs:
the containers read their descriptor data through an `Arc`, convert and iterate
with no borrow, and mutate under a short one. The order of operations is upb's,
as probed:
- the length is read before an index's `__index__` runs;
- `extend` and slice assignment take their values as a sequence first;
- a failing `extend` resizes back to its start;
- `sort` writes back position by position;
- `remove` compares the live items.
Like upb's, the sequence containers have no `__contains__` (`in` uses the
sequence protocol), which also keeps the comparisons out of a borrow.
- **`codec.rs`.**
- A tag is read as upb reads it: a varint of at most 5 bytes that fits in
32 bits, of a field number of 1 to 2^29-1 (0 is accepted inside unknown groups,
as upb accepts it). It applies to the fields of messages, groups and map
entries.
- A map entry with a field its type lacks, or with a key or value of the
wrong wire type, is re-encoded as upb does: key, then value, then the entry's
unknown fields. It is appended to the parent's unknown fields instead of being
inserted.
- Nested decode errors name the outermost message, as upb's do.
`docs/compatibility.md` describes the two differences left:
- this runtime builds messages of nested dicts at any depth;
- where Python code that a container method runs shortens the container, a
read past its end raises `IndexError`, where upb reads or writes past the end
of its array, which can end the process.
### Why are the changes needed?
So that messages behave as upb's in these cases, and do not crash the
process. `pyspark.ml` and the plugins of the drop-in build their requests with
these messages.
### Does this PR introduce _any_ user-facing change?
Yes, as upb behaves:
- deep messages discard and build without crashing;
- mutating a container from Python code that it runs no longer raises
`PanicException`;
- corrupt tags raise `DecodeError`;
- map entries with unknown fields go to the unknown fields of the message.
### How was this patch tested?
- New tests in `python/tests/test_proto_rust_offline.py` compare with upb:
- `test_tags_of_more_than_32_bits_or_five_bytes_are_corrupt_as_upbs`;
-
`test_map_entries_of_fields_of_no_field_of_their_type_are_unknown_fields_as_upbs`;
- `test_discard_unknown_fields_discards_those_of_63_levels_as_upbs`;
- `test_containers_that_their_python_code_changes_behave_as_upbs`,
including the threads case;
- `test_messages_of_nested_mappings_build_as_upbs`;
- `test_deep_messages_discard_and_build_on_a_small_stack`, which runs in a
subprocess with a 512 KiB thread, so that a crash fails the test rather than
pytest;
- new cases in the existing sequence case tables.
The file has 46 passed. With master's extension, the 10 new or changed
tests fail and pytest survives.
- Probes of about 90 decoding cases and about 100 re-entrancy cases, each in
its own subprocess, give upb's result wherever upb terminates.
- `test_ml_offline.py` (391 passed), `test_dropin_offline.py -k proto` (159
passed), and the offline suite of `coverage.yml`.
- `cargo fmt --all --check`, `cargo +1.100.0 clippy -p pyspark-rs
--all-targets -- -D warnings`, ruff 0.14.0 and `audit_no_stubs.sh`.
Performance on 200k ints:
- a full-scan `in` takes 14.8 ms instead of 4.8 ms, without the native
`__contains__`;
- `extend` takes 10 to 25% longer, as it takes the values as a sequence
first, as upb does;
- slice assignment is twice as fast.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
This pull request and its description were written by Isaac.
--
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]