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


##########
python/pyspark/sql/connect/streaming/query.py:
##########
@@ -287,7 +287,10 @@ def __init__(self, sqm: "StreamingQueryManager") -> None:
         self._bus_lock = Lock()
 
     def close(self) -> None:
-        for listener in self._listener_bus:
+        # 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:
             self.remove(listener)

Review Comment:
   **[1] After `spark.stop()`, this loop now reaches the last-listener removal 
on a closed client, so a session with two or more listeners can print a 
misleading warning when it is garbage collected.**
   
   `close()` is called only from `SparkSession.__del__` (session.py:930), and 
`stop()` does not call it: `stop()` releases the session and then closes the 
client. On the server, releasing the session runs `SessionHolder.close()`, 
which calls `streamingServersideListenerHolder.cleanUp()` and sends 
`ResultComplete`, so the handler thread can end normally and leave 
`_listener_bus` as it is. When the session is collected later, `__del__` -> 
`_sqm.close()` -> `remove(<last listener>)` -> `execute_command()` raises 
`NO_ACTIVE_SESSION` from `_stub`, and `remove()` turns it into "Failed to 
remove the listener ... The listener is not removed, please remove it again.". 
The `try/except` in `__del__` does not suppress the warning.
   
   With one listener, this already happened before this PR. With two or more, 
the old loop never reached the last removal, so it was silent. With this PR, it 
is the usual path after `stop()` for any number of listeners, and the advice to 
remove the listener again cannot be followed on a stopped session.
   
   Could `close()` skip the server-side removal and only clear the local bus 
when `self._sqm._session.client.is_closed`, since releasing the session has 
already removed the server-side listener?



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