[02/10] remote: Eliminate remote_hostio_close_cleanup

Message ID 61a8b543-5e01-bb52-09c3-298363c0bef8@redhat.com
State New, archived
Headers

Commit Message

Pedro Alves May 16, 2018, 6:10 p.m. UTC
  On 05/16/2018 06:37 PM, Tom Tromey wrote:
>>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:
> 
> Pedro> +  ~scoped_remote_fd ()
> Pedro> +  {
> Pedro> +    if (m_fd != -1)
> Pedro> +      {
> Pedro> +	int remote_errno;
> Pedro> +	remote_hostio_close (find_target_at (process_stratum),
> Pedro> +			     m_fd, &remote_errno);
> 
> The only danger here is if remote_hostio_close can throw.  However, this
> was already a danger (in theory) before the patch -- there is a rule
> (perhaps unwritten and/or unenforced) that one cannot throw during a
> "do_cleanups".

Yeah, a cleanup throwing leaves things in a bad state.

I've been ignoring this whole "destructors shouldn't throw" thing
in RAII-fying patches for that reason, but it wouldn't hurt to
start handling it properly.

I don't think there's anything that can be done in this case other than
swallowing the exception.  We already call remote_hostio_close
explicitly in the normal paths, so the destructor trying to close
the file is just for the already-unwinding-due-to-some-other-exception
case.

> I didn't look but some assurance that this can't happen would be good to
> have.

So it seems to me that the below would be the way to go.  WDYT?
  

Comments

Tom Tromey May 16, 2018, 6:20 p.m. UTC | #1
>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> So it seems to me that the below would be the way to go.  WDYT?

Yeah, seems like a good idea.

Tom
  

Patch

diff --git a/gdb/remote.c b/gdb/remote.c
index bc4815c67e..9761332ee4 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -12213,8 +12213,17 @@  public:
     if (m_fd != -1)
       {
 	int remote_errno;
-	remote_hostio_close (find_target_at (process_stratum),
-			     m_fd, &remote_errno);
+	try
+	  {
+	    remote_hostio_close (find_target_at (process_stratum),
+				 m_fd, &remote_errno);
+	  }
+	catch (...)
+	  {
+	    /* Swallow exception before it escapes the dtor.  If
+	       something goes wrong, likely the connection is gone,
+	       and there's nothing else that can be done.  */
+	  }
       }
   }