HyukjinKwon commented on code in PR #60423:
URL: https://github.com/apache/spark/pull/60423#discussion_r4571424465
##########
python/pyspark/sql/connect/streaming/query.py:
##########
@@ -287,7 +287,19 @@ def __init__(self, sqm: "StreamingQueryManager") -> None:
self._bus_lock = Lock()
def close(self) -> None:
- for listener in self._listener_bus:
+ if self._sqm._session.client.is_closed:
+ # The client is closed, e.g. by SparkSession.stop(), whose release
of the session
+ # already removed the server side listener, so no removal can or
needs to be sent.
+ # Only clear the local listeners; the handler thread ends with the
event stream.
+ with self._thread_lock:
+ with self._bus_lock:
+ self._listener_bus.clear()
+ self._execution_thread = None
+ return
+ # remove() deletes from _listener_bus, so iterate over a snapshot
taken under the lock.
+ with self._bus_lock:
+ listeners = list(self._listener_bus)
+ for listener in listeners:
Review Comment:
Thanks, agreed. A handler thread keeps the session reachable, so `__del__`
runs only after the handler has ended, by an error that cleared the bus, or by
the `ResultComplete` of the server's cleanup. By then the server-side listener
is gone, so the snapshot matters only for a direct call to `close()`. I updated
the PR description: in practice, the user-facing change is the closed-client
case of [1], and the snapshot fixes a direct `close()` with more than one
listener.
##########
python/pyspark/sql/connect/streaming/query.py:
##########
@@ -287,7 +287,19 @@ def __init__(self, sqm: "StreamingQueryManager") -> None:
self._bus_lock = Lock()
def close(self) -> None:
- for listener in self._listener_bus:
+ if self._sqm._session.client.is_closed:
+ # The client is closed, e.g. by SparkSession.stop(), whose release
of the session
+ # already removed the server side listener, so no removal can or
needs to be sent.
Review Comment:
Thanks, you're right: `_closed` is also set on
`INVALID_HANDLE.SESSION_CHANGED` and on a server-side session ID mismatch,
without a release. Skipping the removal is right in every case because a closed
client cannot send it. I took your wording in 2f82035.
##########
python/pyspark/sql/tests/connect/streaming/test_listener.py:
##########
@@ -74,6 +75,55 @@ def test_rejects_unexpected_response(self):
self.assertEqual(error.exception.getCondition(),
"UNKNOWN_RESPONSE")
[email protected](not should_test_connect, connect_requirement_message)
+class StreamingQueryListenerBusCloseTests(unittest.TestCase):
+ def test_close_removes_every_listener_and_server_listener_once(self):
+ listener_added =
pb2.StreamingQueryListenerEventsResult(listener_bus_listener_added=True)
+ manager = mock.Mock()
+ manager._session.client.is_closed = False
+ manager._session.client.execute_command_as_iterator.return_value =
iter(
+ [{"streaming_query_listener_events_result": listener_added}]
+ )
+ listener_bus = StreamingQueryListenerBus(manager)
+ listeners = [object(), object(), object()]
+ for listener in listeners:
+ listener_bus.append(listener)
+
+ listener_bus.close()
+
+ self.assertEqual(listener_bus._listener_bus, [])
+ self.assertIsNone(listener_bus._execution_thread)
+
manager._session.client.execute_command_as_iterator.assert_called_once()
+ manager._session.client.execute_command.assert_called_once()
+ remove_cmd = manager._session.client.execute_command.call_args[0][0]
+ bus_cmd = remove_cmd.streaming_query_listener_bus_command
+ self.assertTrue(bus_cmd.remove_listener_bus_listener)
+
+ def
test_close_with_a_closed_client_clears_the_listeners_without_a_removal(self):
Review Comment:
Added in 2f82035 as
`test_stopped_session_with_listeners_is_collected_without_a_warning` in
`test_parity_listener.py`. It adds two listeners to a `newSession()`, calls
`stop()`, joins the handler thread, and drops the session. It checks through a
weakref that `gc.collect()` collected it, and that no "Failed to remove the
listener" warning was raised. I ran the same steps against a Spark 4.3.0
Connect server. With this PR, nothing is warned. With 200e25d, which has the
snapshot but not the closed-client check, it warns "Failed to remove the
listener because of exception: [NO_ACTIVE_SESSION] ...".
##########
python/pyspark/sql/tests/connect/streaming/test_listener.py:
##########
@@ -74,6 +75,55 @@ def test_rejects_unexpected_response(self):
self.assertEqual(error.exception.getCondition(),
"UNKNOWN_RESPONSE")
[email protected](not should_test_connect, connect_requirement_message)
+class StreamingQueryListenerBusCloseTests(unittest.TestCase):
+ def test_close_removes_every_listener_and_server_listener_once(self):
+ listener_added =
pb2.StreamingQueryListenerEventsResult(listener_bus_listener_added=True)
+ manager = mock.Mock()
+ manager._session.client.is_closed = False
+ manager._session.client.execute_command_as_iterator.return_value =
iter(
+ [{"streaming_query_listener_events_result": listener_added}]
+ )
+ listener_bus = StreamingQueryListenerBus(manager)
+ listeners = [object(), object(), object()]
+ for listener in listeners:
+ listener_bus.append(listener)
+
+ listener_bus.close()
+
+ self.assertEqual(listener_bus._listener_bus, [])
+ self.assertIsNone(listener_bus._execution_thread)
+
manager._session.client.execute_command_as_iterator.assert_called_once()
+ manager._session.client.execute_command.assert_called_once()
+ remove_cmd = manager._session.client.execute_command.call_args[0][0]
+ bus_cmd = remove_cmd.streaming_query_listener_bus_command
+ self.assertTrue(bus_cmd.remove_listener_bus_listener)
+
+ def
test_close_with_a_closed_client_clears_the_listeners_without_a_removal(self):
+ # After SparkSession.stop(), the release of the session has removed
the server side
+ # listener and the client is closed, so close() sends no removal and
warns nothing.
+ listener_added =
pb2.StreamingQueryListenerEventsResult(listener_bus_listener_added=True)
+ manager = mock.Mock()
+ manager._session.client.is_closed = False
+ manager._session.client.execute_command_as_iterator.return_value =
iter(
+ [{"streaming_query_listener_events_result": listener_added}]
+ )
+ manager._session.client.execute_command.side_effect =
RuntimeError("closed client")
+ listener_bus = StreamingQueryListenerBus(manager)
+ for listener in [object(), object()]:
+ listener_bus.append(listener)
+ manager._session.client.is_closed = True
Review Comment:
Done in 2f82035: the test joins the handler thread before it marks the
client closed.
##########
python/pyspark/sql/connect/streaming/query.py:
##########
@@ -287,7 +287,19 @@ def __init__(self, sqm: "StreamingQueryManager") -> None:
self._bus_lock = Lock()
def close(self) -> None:
- for listener in self._listener_bus:
+ if self._sqm._session.client.is_closed:
+ # The client is closed, e.g. by SparkSession.stop(), whose release
of the session
+ # already removed the server side listener, so no removal can or
needs to be sent.
+ # Only clear the local listeners; the handler thread ends with the
event stream.
+ with self._thread_lock:
+ with self._bus_lock:
+ self._listener_bus.clear()
Review Comment:
Done in 2f82035: `_clear_local_listeners()` clears the bus and the thread
reference. `close()` and the error path of `_query_event_handler` both call it
with `_bus_lock` held, and it takes no `_thread_lock`.
--
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]