dongjoon-hyun commented on code in PR #60423:
URL: https://github.com/apache/spark/pull/60423#discussion_r4571590093


##########
python/pyspark/sql/tests/connect/streaming/test_parity_listener.py:
##########
@@ -282,6 +282,33 @@ def load_event(event_name, table_name):
             # Remove again to verify this won't throw any error
             self.spark.streams.removeListener(test_listener)
 
+    def 
test_stopped_session_with_listeners_is_collected_without_a_warning(self):
+        # stop() releases the session, which removes its server side listener, 
ends the event
+        # stream and the handler thread, and closes the client. 
SparkSession.__del__ then closes
+        # the listener bus, which must send no removal on the closed client, 
and so warn
+        # nothing, with two listeners as with one.
+        import gc
+        import warnings
+        import weakref
+
+        session = self.spark.newSession()
+        session.streams.addListener(TestListenerLocalV1())
+        session.streams.addListener(TestListenerLocalV2())
+        thread = session.streams._sqlb._execution_thread
+        collected = weakref.ref(session)
+        with warnings.catch_warnings(record=True) as caught:
+            warnings.simplefilter("always")
+            session.stop()

Review Comment:
   **[7] In the default `local[4]` test mode, `session.stop()` here also stops 
the local Connect server that the rest of this suite uses.**
   
   CI on 2f82035 shows it in 
[`pyspark-structured-streaming-connect`](https://github.com/HyukjinKwon/spark/actions/runs/43556068064/job/132333073267).
 This test errors after 0.24 seconds. The module then times out after 450 
seconds in `setUp()` of `test_streaming_progress`, where `_cleanup_ml_cache()` 
keeps retrying against the stopped server.
   
   The PR builds do not set `SPARK_CONNECT_TESTING_REMOTE`, so 
`ReusedConnectTestCase` connects with `local[4]`. That starts the server in the 
test process and sets `SPARK_LOCAL_REMOTE`. With that variable set, `stop()` 
calls `PySparkSession._activeSession.stop()` for any Connect session, including 
one from `newSession()`. It also deletes `SPARK_LOCAL_REMOTE` and 
`SPARK_CONNECT_MODE_ENABLED` (session.py:1023-1037). The log does not have the 
traceback of the error. I think it comes from `tearDown()`, which calls 
`self._legacy_sc._jsc.sc()` (connectutils.py:206) after `_jsc` became `None`.
   
   `test_connect_context.py` masks the variable before `stop()` for the same 
reason (test_connect_context.py:131-136). Maybe:
   
   ```python
           import os
           from unittest import mock
           ...
               with mock.patch.dict(os.environ):
                   # Release only this session, and keep the local Connect 
server of this suite.
                   os.environ.pop("SPARK_LOCAL_REMOTE", None)
                   session.stop()
   ```
   
   A separate server through `sc://`, as in your run against 4.3.0, does not 
set the variable, so the problem does not show up there.



##########
python/pyspark/sql/tests/connect/streaming/test_parity_listener.py:
##########
@@ -282,6 +282,33 @@ def load_event(event_name, table_name):
             # Remove again to verify this won't throw any error
             self.spark.streams.removeListener(test_listener)
 
+    def 
test_stopped_session_with_listeners_is_collected_without_a_warning(self):
+        # stop() releases the session, which removes its server side listener, 
ends the event
+        # stream and the handler thread, and closes the client. 
SparkSession.__del__ then closes
+        # the listener bus, which must send no removal on the closed client, 
and so warn
+        # nothing, with two listeners as with one.
+        import gc
+        import warnings
+        import weakref
+
+        session = self.spark.newSession()
+        session.streams.addListener(TestListenerLocalV1())
+        session.streams.addListener(TestListenerLocalV2())

Review Comment:
   **[8] With two listeners, this test also passes on master, so it does not 
cover the warning that this PR removes.**
   
   On master, `close()` removes the first listener, and the loop then ends with 
the second one still on the bus. The last-listener removal is never sent, so 
nothing warns, and the session is still collected once the handler thread has 
ended. On master, only a single listener reaches the removal and warns "Failed 
to remove the listener". That is the user-facing case in the PR description. 
The comment says "with two listeners as with one", but only two run. With both 
counts, the test would fail on master with one listener, and on 200e25d with 
either count:
   
   ```python
           for count in [1, 2]:
               with self.subTest(count=count):
                   session = self.spark.newSession()
                   for _ in range(count):
                       session.streams.addListener(TestListenerLocalV1())
                   # The rest as now.
   ```



-- 
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]

Reply via email to