<?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>283053</bug_id>
          
          <creation_ts>2024-11-13 07:49:52 -0800</creation_ts>
          <short_desc>[GLIB] Invalid write reported by Valgrind on GSocketMonitor::sourceSocketCallback</short_desc>
          <delta_ts>2024-11-14 09:28:34 -0800</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>WPE WebKit</component>
          <version>WebKit Nightly Build</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <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="Lauro Moura">lmoura</reporter>
          <assigned_to name="Lauro Moura">lmoura</assigned_to>
          <cc>bugs-noreply</cc>
    
    <cc>mcatanzaro</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>2074820</commentid>
    <comment_count>0</comment_count>
    <who name="Lauro Moura">lmoura</who>
    <bug_when>2024-11-13 07:49:52 -0800</bug_when>
    <thetext>bug282910 originally reported an invalid memory access in GSocketMonitor::sourceSocketCallback. Originally I thought it was related to the SessionHost destructor being called prematurely, but even after ensuring we&apos;d delete the SessionHost after it returns from the socket message handlers in https://github.com/WebKit/WebKit/pull/36533, the error continues.

From the trace, somehow erasing `monitor-&gt;m_callback` (added in 285339@main) is triggering the destruction of the monitor immediately while still inside its socketSourceCallback. Does not seem to bring issues in release builds, but running under valgrind shows an invalid write while updating m_shouldDestroyCallback

Maybe it&apos;s a matter of ensuring cleaning the callback is the last action in the sourceSocket callback.

```
** (WPEWebProcess:2182124): WARNING **: 23:33:30.451: RemoteInspector failed to connect to inspector server at: 127.0.0.1:40907: GDBus.Error:org.freedesktop.portal.Error.NotAllowed: This call is not available inside the sandbox
==2181500== Invalid write of size 1
==2181500==    at 0x451DC8: WTF::GSocketMonitor::socketSourceCallback(_GSocket*, GIOCondition, WTF::GSocketMonitor*) (GSocketMonitor.cpp:51)
==2181500==    by 0x4BCDBB0: socket_source_dispatch (gsocket.c:4267)
==2181500==    by 0x4DC148D: g_main_dispatch.lto_priv.0 (gmain.c:3344)
==2181500==    by 0x4E20716: UnknownInlinedFun (gmain.c:4152)
==2181500==    by 0x4E20716: g_main_context_iterate_unlocked.isra.0 (gmain.c:4217)
==2181500==    by 0x4DC1F76: g_main_loop_run (gmain.c:4419)
==2181500==    by 0x453161: WTF::RunLoop::run() (RunLoopGLib.cpp:108)
==2181500==    by 0x3781F5: WebDriver::WebDriverService::run(int, char**) (WebDriverService.cpp:186)
==2181500==    by 0x375AED: main (WebDriverMain.cpp:39)
==2181500==  Address 0x7aa7619 is 73 bytes inside a block of size 128 free&apos;d
==2181500==    at 0x484988F: free (vg_replace_malloc.c:985)
==2181500==    by 0x45E459: bmalloc::DebugHeap::free(void*) (DebugHeap.cpp:139)
==2181500==    by 0x45E739: pas_debug_heap_free (DebugHeap.cpp:238)
==2181500==    by 0x4BAB8C: pas_try_deallocate_slow_no_cache (pas_deallocate.c:135)
==2181500==    by 0x3E0BEC: pas_try_deallocate (pas_deallocate.h:209)
==2181500==    by 0x3E0C75: pas_deallocate (pas_deallocate.h:221)
==2181500==    by 0x3E579F: bmalloc_deallocate_inline (bmalloc_heap_inlines.h:592)
==2181500==    by 0x3E5EFC: free (bmalloc.h:187)
==2181500==    by 0x3E5EFC: WTF::fastFree(void*) (FastMalloc.cpp:617)
==2181500==    by 0x3BD846: WTF::RefCounted&lt;WTF::SocketConnection&gt;::operator delete(void*) (RefCounted.h:199)
==2181500==    by 0x3BC218: WTF::RefCounted&lt;WTF::SocketConnection&gt;::deref() const (RefCounted.h:204)
==2181500==    by 0x3BA9D1: WTF::DefaultRefDerefTraits&lt;WTF::SocketConnection&gt;::derefIfNotNull(WTF::SocketConnection*) (Ref.h:62)
==2181500==    by 0x3B89BC: WTF::Ref&lt;WTF::SocketConnection, WTF::RawPtrTraits&lt;WTF::SocketConnection&gt;, WTF::DefaultRefDerefTraits&lt;WTF::SocketConnection&gt; &gt;::~Ref() (Ref.h:82)
==2181500==  Block was alloc&apos;d at
==2181500==    at 0x4846828: malloc (vg_replace_malloc.c:442)
==2181500==    by 0x45E370: bmalloc::DebugHeap::malloc(unsigned long, bmalloc::FailureAction) (DebugHeap.cpp:118)
==2181500==    by 0x45E5C6: pas_debug_heap_malloc (DebugHeap.cpp:223)
==2181500==    by 0x462DF3: pas_debug_heap_allocate (pas_debug_heap.h:106)
==2181500==    by 0x46374E: pas_try_allocate_intrinsic_impl_casual_case (pas_try_allocate_intrinsic.h:105)
==2181500==    by 0x463F44: bmalloc_allocate_impl_casual_case (bmalloc_heap_inlines.h:69)
==2181500==    by 0x464441: bmalloc_allocate_casual (bmalloc_heap.c:64)
==2181500==    by 0x3E4A21: bmalloc_allocate_inline (bmalloc_heap_inlines.h:120)
==2181500==    by 0x3E5995: malloc (bmalloc.h:79)
==2181500==    by 0x3E5995: WTF::fastMalloc(unsigned long) (FastMalloc.cpp:559)
==2181500==    by 0x3B892B: WTF::RefCounted&lt;WTF::SocketConnection&gt;::operator new(unsigned long) (RefCounted.h:199)
==2181500==    by 0x3B51A7: WTF::SocketConnection::create(WTF::GRefPtr&lt;_GSocketConnection&gt;&amp;&amp;, WTF::HashMap&lt;WTF::CString, std::pair&lt;WTF::CString, void (*)(WTF::SocketConnection&amp;, _GVariant*, void*)&gt;, WTF::DefaultHash&lt;WTF::CString&gt;, WTF::HashTraits&lt;WTF::CString&gt;, WTF::HashTraits&lt;std::pair&lt;WTF::CString, void (*)(WTF::SocketConnection&amp;, _GVariant*, void*)&gt; &gt;, WTF::HashTableTraits, (WTF::ShouldValidateKey)0&gt; const&amp;, void*) (SocketConnection.h:41)
==2181500==    by 0x3B6A91: WebDriver::SessionHost::connectToBrowser(std::unique_ptr&lt;WebDriver::ConnectToBrowserAsyncData, std::default_delete&lt;WebDriver::ConnectToBrowserAsyncData&gt; &gt;&amp;&amp;)::{lambda()#1}::operator()()::{lambda(_GObject*, _GAsyncResult*, void*)#1}::operator()(_GObject*, _GAsyncResult*, void*) const (SessionHostGlib.cpp:217)
==2181500== 
```

Tentative patch incoming.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2074877</commentid>
    <comment_count>1</comment_count>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2024-11-13 11:43:52 -0800</bug_when>
    <thetext>(In reply to Lauro Moura from comment #0)
&gt; Tentative patch incoming.

Thanks :)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2075043</commentid>
    <comment_count>2</comment_count>
    <who name="Lauro Moura">lmoura</who>
    <bug_when>2024-11-14 06:00:54 -0800</bug_when>
    <thetext>Pull request: https://github.com/WebKit/WebKit/pull/36635</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2075101</commentid>
    <comment_count>3</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2024-11-14 09:28:32 -0800</bug_when>
    <thetext>Committed 286596@main (5d047d3c9925): &lt;https://commits.webkit.org/286596@main&gt;

Reviewed commits have been landed. Closing PR #36635 and removing active labels.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>