Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: Simon Farre via Gdb-patches <gdb-patches@sourceware.org>
Subject: Re: [PATCH v4] gdb/Python: Added ThreadExitedEvent
Date: Mon, 06 Jun 2022 06:51:56 -0600	[thread overview]
Message-ID: <87pmjm0xar.fsf@tromey.com> (raw)
In-Reply-To: <20220420082547.759990-1-simon.farre.cx@gmail.com> (Simon Farre via Gdb-patches's message of "Wed, 20 Apr 2022 10:25:47 +0200")

>>>>> Simon Farre via Gdb-patches <gdb-patches@sourceware.org> writes:

> Tom requested that I should probably just emit the thread object;
> I ran into two issues for this, which I could not resolve in this patch;

> 1 - The Thread Object (the python type) checks it's own validity
> by doing a comparison of it's `thread_info* thread` to nullptr. This
> means that any access of it's attributes may (probably, since we are
> in "async" land) throw Python exceptions because the thread has been
> removed from the thread object.

This seems fine to me, because the event emission comes before the
clearing of the field:

+  if (!evregpy_no_listeners_p (gdb_py_events.thread_exited))
+    {
+      if (emit_thread_exit_event (tmp->thread_obj->thread) < 0)
+	gdbpy_print_stack ();
+    }
   tmp->thread_obj->thread = NULL;

The InferiorThread object is valid until that NULL assignment is done.

> 2 - A python user can hold a global reference to an exiting thread. Thus
> in order to have a ThreadExit event that can provide attribute access
> reliably (both as a global reference, but also inside the thread exit
> handler, as we can never guarantee that it's executed _before_ the
> thread_info pointer is removed from the gdbpy thread object),
> the `thread_info *` thread pointer must not be null. However, this
> comes at the cost of gdb.InferiorThread believing it is "valid" - which means,
> that if a user holds takes a global reference to that
> exiting event thread object, they can some time later do `t.switch()` at which
> point GDB will 'explode' so to speak.

I think it will be fine because thread-switching checks validity using
THPY_REQUIRE_VALID.  So once the underlying (gdb) thread is gone, the
InferiorThread will be in the "invalid" state, and all will be fine.

Another way to look at it is that the user can already stash the
InferiorThread somewhere and refer to it later.

The reason I think it's better to pass the InferiorThread to the event
is that (1) it means you can have a map where the key is a thread and
then it is easy to update when a thread exits, and (2) this doesn't lose
anything because the event can always refer to the members of the thread
if that is what is useful.

Tom

      parent reply	other threads:[~2022-06-06 12:52 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-20  8:25 Simon Farre via Gdb-patches
2022-05-02 16:02 ` [PING][PATCH " Simon Farre via Gdb-patches
2022-06-06 12:51 ` Tom Tromey [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87pmjm0xar.fsf@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox