<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>145347</bug_id>
          
          <creation_ts>2015-05-23 10:47:05 -0700</creation_ts>
          <short_desc>[GTK] Crashes when SoupSession is destroyed in exit handler</short_desc>
          <delta_ts>2015-08-06 13:12:16 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>WebKitGTK</component>
          <version>528+ (Nightly build)</version>
          <rep_platform>PC</rep_platform>
          <op_sys>Linux</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          <see_also>https://bugzilla.redhat.com/show_bug.cgi?id=1208872</see_also>
    
    <see_also>https://bugzilla.redhat.com/show_bug.cgi?id=1222802</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Michael Catanzaro">mcatanzaro</reporter>
          <assigned_to name="Michael Catanzaro">mcatanzaro</assigned_to>
          <cc>andersca</cc>
    
    <cc>cgarcia</cc>
    
    <cc>danw</cc>
    
    <cc>mcatanzaro</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1097103</commentid>
    <comment_count>0</comment_count>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2015-05-23 10:47:05 -0700</bug_when>
    <thetext>We have a couple downstream reports of crashes that occur when g_object_unref destroys a SoupSession in an exit handler. E.g.

Truncated backtrace:
Thread no. 1 (10 frames)
 #0 free at /lib64/libc.so.6
 #1 _gnutls_free_dh_info at /lib64/libgnutls.so.28
 #2 _gnutls_free_auth_info at /lib64/libgnutls.so.28
 #3 gnutls_deinit at /lib64/libgnutls.so.28
 #4 g_tls_connection_gnutls_finalize at /usr/lib64/gio/modules/libgiognutls.so
 #6 soup_io_stream_finalize at /lib64/libsoup-2.4.so.1
 #8 soup_socket_finalize at /lib64/libsoup-2.4.so.1
 #10 soup_connection_disconnect at /lib64/libsoup-2.4.so.1
 #11 soup_session_abort at /lib64/libsoup-2.4.so.1
 #12 soup_session_dispose at /lib64/libsoup-2.4.so.1

(Truncated, but soup_session_dispose is called by g_object_unref is called by __run_exit_handlers.)

I spent some time checking libsoup, WebKit, Epiphany, and even GLib for all uses of atexit and on_exit, but didn&apos;t find anything suspicious. So my suspicion falls on SoupNetworkSession::defaultSession(). I guess that will be destroyed in an exit handler because it is static? Maybe we should leak it using NeverDestroyed&lt;SoupNetworkSession&gt; instead? This is kind of wild speculation, but it seems reasonable....

We have another backtrace where __run_exit_handlers calls g_object_unref calls soup_session_dispose (as in the backtrace above), then soup_session_dispose calls g_source_destroy calls g_source_destroy_internal calls g_mutex_lock, then boom. That surely is happening because the mutex is already locked, so it&apos;s some thread-safety issue for sure.

Full backtraces downstream (in the See Also field).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1097104</commentid>
    <comment_count>1</comment_count>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2015-05-23 10:52:28 -0700</bug_when>
    <thetext>(In reply to comment #0)
&gt; Maybe we should leak
&gt; it using NeverDestroyed&lt;SoupNetworkSession&gt; instead?

Maybe that is a terrible idea, since I didn&apos;t check to see what all SoupSession does when it&apos;s destroyed, since we&apos;d be relying on it never doing anything important even if it doesn&apos;t currently, and since I haven&apos;t even established what the issue is.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1112930</commentid>
    <comment_count>2</comment_count>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2015-07-28 16:49:32 -0700</bug_when>
    <thetext>(In reply to comment #0) 
&gt; I spent some time checking libsoup, WebKit, Epiphany, and even GLib for all
&gt; uses of atexit and on_exit, but didn&apos;t find anything suspicious. So my
&gt; suspicion falls on SoupNetworkSession::defaultSession(). I guess that will
&gt; be destroyed in an exit handler because it is static? Maybe we should leak
&gt; it using NeverDestroyed&lt;SoupNetworkSession&gt; instead? This is kind of wild
&gt; speculation, but it seems reasonable....

Yes, static variables are destroyed in exit handlers. So that must be where this crash is coming from.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1112933</commentid>
    <comment_count>3</comment_count>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2015-07-28 16:52:37 -0700</bug_when>
    <thetext>So if we don&apos;t destroy the SoupSession, surely that could result in us leaving a TLS session open forever. That would annoy the server.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1112939</commentid>
    <comment_count>4</comment_count>
    <who name="Anders Carlsson">andersca</who>
    <bug_when>2015-07-28 17:03:40 -0700</bug_when>
    <thetext>(In reply to comment #3)
&gt; So if we don&apos;t destroy the SoupSession, surely that could result in us
&gt; leaving a TLS session open forever. That would annoy the server.

Doesn&apos;t Linux clean up sockets when the process exits?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1113008</commentid>
    <comment_count>5</comment_count>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2015-07-28 19:24:30 -0700</bug_when>
    <thetext>(In reply to comment #4)
&gt; Doesn&apos;t Linux clean up sockets when the process exits?

Of course. So it shouldn&apos;t matter, you&apos;re right.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1113039</commentid>
    <comment_count>6</comment_count>
    <who name="Carlos Garcia Campos">cgarcia</who>
    <bug_when>2015-07-28 22:45:33 -0700</bug_when>
    <thetext>I think libsoup requires that the session is properly finalized for some reason I don&apos;t remember, Dan?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1113067</commentid>
    <comment_count>7</comment_count>
    <who name="Dan Winship">danw</who>
    <bug_when>2015-07-29 03:43:00 -0700</bug_when>
    <thetext>(In reply to comment #6)
&gt; I think libsoup requires that the session is properly finalized for some
&gt; reason I don&apos;t remember, Dan?

Nope. If you&apos;re trying to find leaks with valgrind you need to destroy everything, but otherwise it doesn&apos;t matter more for SoupSession than for anything else.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1113113</commentid>
    <comment_count>8</comment_count>
      <attachid>257757</attachid>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2015-07-29 10:56:27 -0700</bug_when>
    <thetext>Created attachment 257757
Patch</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1113316</commentid>
    <comment_count>9</comment_count>
      <attachid>257757</attachid>
    <who name="Carlos Garcia Campos">cgarcia</who>
    <bug_when>2015-07-29 23:03:13 -0700</bug_when>
    <thetext>Comment on attachment 257757
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=257757&amp;action=review

&gt; Source/WebCore/platform/network/soup/SoupNetworkSession.cpp:40
&gt; +#include &lt;wtf/NeverDestroyed.h&gt;

I&apos;m surprised you don&apos;t need to include this in the header for the friend class NeverDestroyed&lt;SoupNetworkSession&gt;;

&gt; Source/WebCore/platform/network/soup/SoupNetworkSession.cpp:55
&gt; +    // There are thread-safety issues involved in the destruction of the SoupSession, bug #145347.

Do not add this comment, using NeverDestroyed is the common thing for a singleton implementation like this in WebKit, the current code was the exception, because I thought we had to properly cleanup the soup session, as we also used to do in wk1 (using an ugly atexit handler)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1113380</commentid>
    <comment_count>10</comment_count>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2015-07-30 09:16:49 -0700</bug_when>
    <thetext>(In reply to comment #9)
&gt; I&apos;m surprised you don&apos;t need to include this in the header for the friend
&gt; class NeverDestroyed&lt;SoupNetworkSession&gt;;

You can friend classes that don&apos;t exist, so it&apos;s pointless to include the header. I am not sure if that is a language feature or a language defect, but it is what it is....

&gt; &gt; Source/WebCore/platform/network/soup/SoupNetworkSession.cpp:55
&gt; &gt; +    // There are thread-safety issues involved in the destruction of the SoupSession, bug #145347.
&gt; 
&gt; Do not add this comment, using NeverDestroyed is the common thing for a
&gt; singleton implementation like this in WebKit, the current code was the
&gt; exception, because I thought we had to properly cleanup the soup session, as
&gt; we also used to do in wk1 (using an ugly atexit handler)

OK.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1113382</commentid>
    <comment_count>11</comment_count>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2015-07-30 09:21:51 -0700</bug_when>
    <thetext>Committed r187586: &lt;http://trac.webkit.org/changeset/187586&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1115419</commentid>
    <comment_count>12</comment_count>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2015-08-06 13:12:16 -0700</bug_when>
    <thetext>Note that if the TLS session is not closed cleanly, a well-behaved server would detect a truncation attack. I think in practice, this should not be a problem for any real server, but it&apos;s not ideal....</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>257757</attachid>
            <date>2015-07-29 10:56:27 -0700</date>
            <delta_ts>2015-07-29 23:03:13 -0700</delta_ts>
            <desc>Patch</desc>
            <filename>bug-145347-20150729125504.patch</filename>
            <type>text/plain</type>
            <size>2493</size>
            <attacher name="Michael Catanzaro">mcatanzaro</attacher>
            
              <data encoding="base64">U3VidmVyc2lvbiBSZXZpc2lvbjogMTg3NTQ4CmRpZmYgLS1naXQgYS9Tb3VyY2UvV2ViQ29yZS9D
aGFuZ2VMb2cgYi9Tb3VyY2UvV2ViQ29yZS9DaGFuZ2VMb2cKaW5kZXggYTEyNmUyMDhiZDAyNzQ4
OWI4MDMzMWU1YjU1MzE3OTNmZWEwZDViZi4uMTlkNjQyZGI2ODQyYjFlNDEyNDc1MjM3NmE0OGU1
NDgyZWE5YTBlMSAxMDA2NDQKLS0tIGEvU291cmNlL1dlYkNvcmUvQ2hh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IHN0YXRpYyBOZXZlckRlc3Ryb3llZDxT
b3VwTmV0d29ya1Nlc3Npb24+IG5ldHdvcmtTZXNzaW9uKHNvdXBDb29raWVKYXIoKSk7CiAgICAg
cmV0dXJuIG5ldHdvcmtTZXNzaW9uOwogfQogCmRpZmYgLS1naXQgYS9Tb3VyY2UvV2ViQ29yZS9w
bGF0Zm9ybS9uZXR3b3JrL3NvdXAvU291cE5ldHdvcmtTZXNzaW9uLmggYi9Tb3VyY2UvV2ViQ29y
ZS9wbGF0Zm9ybS9uZXR3b3JrL3NvdXAvU291cE5ldHdvcmtTZXNzaW9uLmgKaW5kZXggOWM4Y2Zh
YTYxNmE3NGJjNGUwNzM3N2NlY2FiYTgyNTA1NGRiM2Q4ZS4uYWZhNGNkZTFhOTIyMTM3NzdhYTc3
NjA3ODBmNDA3YWI2YTE0ODA3YyAxMDA2NDQKLS0tIGEvU291cmNlL1dlYkNvcmUvcGxhdGZvcm0v
bmV0d29yay9zb3VwL1NvdXBOZXR3b3JrU2Vzc2lvbi5oCisrKyBiL1NvdXJjZS9XZWJDb3JlL3Bs
YXRmb3JtL25ldHdvcmsvc291cC9Tb3VwTmV0d29ya1Nlc3Npb24uaApAQCAtNzAsNiArNzAsOCBA
QCBwdWJsaWM6CiAgICAgdm9pZCBzZXRBY2NlcHRMYW5ndWFnZXMoY29uc3QgVmVjdG9yPFN0cmlu
Zz4mKTsKIAogcHJpdmF0ZToKKyAgICBmcmllbmQgY2xhc3MgTmV2ZXJEZXN0cm95ZWQ8U291cE5l
dHdvcmtTZXNzaW9uPjsKKwogICAgIFNvdXBOZXR3b3JrU2Vzc2lvbihTb3VwQ29va2llSmFyKik7
CiAgICAgU291cE5ldHdvcmtTZXNzaW9uKFNvdXBTZXNzaW9uKik7CiAK
</data>
<flag name="review"
          id="282931"
          type_id="1"
          status="+"
          setter="cgarcia"
    />
    <flag name="commit-queue"
          id="282998"
          type_id="3"
          status="-"
          setter="cgarcia"
    />
          </attachment>
      

    </bug>

</bugzilla>