gaogaotiantian commented on code in PR #60150:
URL: https://github.com/apache/spark/pull/60150#discussion_r4636598031
##########
python/pyspark/sql/profiler.py:
##########
@@ -186,19 +192,25 @@ def _start_sampling_thread(self) -> None:
def _sample_loop(self) -> None:
self_tid = threading.get_ident()
while not self._stop_event.wait(self._sampling_interval / 1000):
- for tid, frame in sys._current_frames().items():
+ # Take the frames first, then update the names, so that a thread
started in
+ # between is found. A reused thread id gets the new thread's name.
+ frames = sys._current_frames()
+ self._thread_names.update(
+ (t.ident, t.name) for t in threading.enumerate() if t.ident is
not None
+ )
+ for tid, frame in frames.items():
if tid != self_tid:
- self._add_sample(tid, frame)
+ self._add_sample(self._thread_names.get(tid, str(tid)),
frame)
Review Comment:
A correction to my reply above. A thread that `threading.enumerate()` never
lists goes under `<unknown thread>` only if no listed thread used its id
earlier in the task. I reproduced the other case on Python 3.11 and 3.14: a
`_thread.start_new_thread()` thread that reused the id of a joined `helper`
thread was recorded under `helper`. The code is unchanged; in 0036d4a137b the
comment and the PR description now describe this case.
##########
python/pyspark/sql/profiler.py:
##########
@@ -186,19 +192,25 @@ def _start_sampling_thread(self) -> None:
def _sample_loop(self) -> None:
self_tid = threading.get_ident()
while not self._stop_event.wait(self._sampling_interval / 1000):
- for tid, frame in sys._current_frames().items():
+ # Take the frames first, then update the names, so that a thread
started in
+ # between is found. A reused thread id gets the new thread's name.
Review Comment:
An update to my reply above. The code is still unchanged, but in 0036d4a137b
I rewrote the comments here so that they say only what the code does: the
sampler takes the frames before the names so that every thread in the frames
that `threading.enumerate()` lists by then is named, and a thread is named only
once it is listed with its id; before that, a reused id keeps the name of the
exited thread. Changing that behavior is still left for later.
--
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]