adityavaish opened a new pull request, #60184:
URL: https://github.com/apache/spark/pull/60184
### What changes were proposed in this pull request?
Move socket creation outside the `try/finally` in PySpark's
`_load_from_socket`.
Only attempt cleanup after the socket and its file object have been
successfully acquired.
Add a JVM-independent `LoadFromSocketTests` suite covering connection
failure, successful
stream consumption, deserialization failure, and failure in the context
consumer.
### Why are the changes needed?
If `_create_local_socket` raises, neither `sockfile` nor `sock` is assigned.
The existing
`finally` block then raises `UnboundLocalError` while attempting to close
`sockfile`,
masking the original error.
This helper is used by operations including RDD and DataFrame collection and
pandas
conversion. Preserving the connection error makes failures diagnosable
without changing
successful collection or cleanup behavior.
### Does this PR introduce _any_ user-facing change?
Yes. When socket creation fails, callers receive the original exception,
such as
`PySparkRuntimeError` with error class `CANNOT_OPEN_SOCKET`, instead of
`UnboundLocalError: cannot access local variable 'sockfile' where it is not
associated with a value`.
Successful operations are unchanged.
### How was this patch tested?
Added four unit tests using mocked socket creation and serialization. The
connection-failure
regression was reproduced against the unfixed implementation: an injected
connection error
was masked by `UnboundLocalError`. With the fix, the test verifies that the
original
`CANNOT_OPEN_SOCKET` exception instance is propagated unchanged.
Ran the following against the working-tree sources in Ubuntu WSL with Python
3.14.4 and
the repository-bundled Py4J 0.10.9.9:
```bash
PYTHONPATH=python:python/lib/py4j-0.10.9.9-src.zip python3 -m unittest \
pyspark.tests.test_util.LoadFromSocketTests \
pyspark.tests.test_util.KeywordOnlyTests \
pyspark.tests.test_util.HandleWorkerExceptionTests
```
All 13 tests passed. No JVM was needed; the full Spark/JVM suite was not run.
Ruff 0.14.8 `check` and `format --check` passed for both changed files, as
did
`git diff --check`.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: GitHub Copilot App 1.0.102-0.unstable.r40949665530.gab1eef5
(GPT-6.1 Sol).
--
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]