<?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>250569</bug_id>
          
          <creation_ts>2023-01-13 06:10:35 -0800</creation_ts>
          <short_desc>Terminated worker memory leak</short_desc>
          <delta_ts>2024-10-16 08:10:53 -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>JavaScriptCore</component>
          <version>Safari Technology Preview</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>CONFIGURATION CHANGED</resolution>
          
          <see_also>https://bugs.webkit.org/show_bug.cgi?id=272813</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Jake Archibald">jaffathecake</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>ahmad.saleem792</cc>
    
    <cc>alonzakai</cc>
    
    <cc>ap</cc>
    
    <cc>bfulgham</cc>
    
    <cc>calm</cc>
    
    <cc>cdumez</cc>
    
    <cc>ivan.curic</cc>
    
    <cc>mark.lam</cc>
    
    <cc>samddenty</cc>
    
    <cc>sam.verschueren</cc>
    
    <cc>steven</cc>
    
    <cc>webkit-bug-importer</cc>
    
    <cc>youennf</cc>
    
    <cc>ysuzuki</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1925320</commentid>
    <comment_count>0</comment_count>
    <who name="Jake Archibald">jaffathecake</who>
    <bug_when>2023-01-13 06:10:35 -0800</bug_when>
    <thetext>Not sure this is the correct component. This bug is something to do with workers/js/wasm.

1. Go to https://safari-worker-memory-leak-demo.netlify.app
2. Click &quot;draw using threads&quot;, and wait for the result. Repeat this step around 20 times.

I start getting &quot;rangeerror: Out of memory&quot;.

Each run of &quot;draw using threads&quot; spins up a worker, which in turn starts other workers. The root worker is terminated once the operation is complete.

I wonder if the inner workers are not being correctly collected when the parent worker terminates.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1925416</commentid>
    <comment_count>1</comment_count>
    <who name="Ahmad Saleem">ahmad.saleem792</who>
    <bug_when>2023-01-13 12:04:25 -0800</bug_when>
    <thetext>Is this an issue STP160 or before? Because this landed - https://github.com/WebKit/WebKit/commit/0987e578a6a8813156f74ebbf01490e4d31aa8f8

Related to workers termination, which can cause this?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1925430</commentid>
    <comment_count>2</comment_count>
    <who name="Ahmad Saleem">ahmad.saleem792</who>
    <bug_when>2023-01-13 13:08:05 -0800</bug_when>
    <thetext>(In reply to Jake Archibald from comment #0)
&gt; Not sure this is the correct component. This bug is something to do with
&gt; workers/js/wasm.
&gt; 
&gt; 1. Go to https://safari-worker-memory-leak-demo.netlify.app
&gt; 2. Click &quot;draw using threads&quot;, and wait for the result. Repeat this step
&gt; around 20 times.
&gt; 
&gt; I start getting &quot;rangeerror: Out of memory&quot;.
&gt; 
&gt; Each run of &quot;draw using threads&quot; spins up a worker, which in turn starts
&gt; other workers. The root worker is terminated once the operation is complete.
&gt; 
&gt; I wonder if the inner workers are not being correctly collected when the
&gt; parent worker terminates.

Just to update - I can reproduce this in Safari Technology Preview 161 and get &apos;Unhandled Promise Rejection: RangeError: Out of memory&apos; in Console.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1925659</commentid>
    <comment_count>3</comment_count>
    <who name="Jake Archibald">jaffathecake</who>
    <bug_when>2023-01-15 05:26:01 -0800</bug_when>
    <thetext>Might be noteworthy: refreshing the page doesn&apos;t seem to release the memory either.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1926877</commentid>
    <comment_count>4</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2023-01-20 06:11:17 -0800</bug_when>
    <thetext>&lt;rdar://problem/104473516&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1927313</commentid>
    <comment_count>5</comment_count>
    <who name="Ahmad Saleem">ahmad.saleem792</who>
    <bug_when>2023-01-22 13:23:45 -0800</bug_when>
    <thetext>(In reply to Ahmad Saleem from comment #1)
&gt; Is this an issue STP160 or before? Because this landed -
&gt; https://github.com/WebKit/WebKit/commit/
&gt; 0987e578a6a8813156f74ebbf01490e4d31aa8f8
&gt; 
&gt; Related to workers termination, which can cause this?

I did local revert on WebKit trunk by undoing changes of this commit and build WebKit release and use Comment 0 test case but I still got &quot;rangeerror: Out of memory&quot;, so I think this change is not culprit or regression (if this is regression).

Just wanted to update my testing result since I was conscious of my change, so I wanted to confirm, it is not caused by it.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1941488</commentid>
    <comment_count>6</comment_count>
    <who name="Sam Verschueren">sam.verschueren</who>
    <bug_when>2023-03-15 01:23:23 -0700</bug_when>
    <thetext>Just want to step in here to say that I see the exact same behaviour when testing StackBlitz WebContainers (https://blog.stackblitz.com/posts/introducing-webcontainers/) in the latest Safari Technology Preview.

We spin up quite some worker (in combination with wasm) and after running some projects and refreshing the page, I also see the `RangeError: Out of Memory`. Just like Jake indicated, refreshing the page does not release the memory and I basically have to close the browser entirely.

When the error happens, and I try to create a memory object in the DevTools console like this, it also throws.

&gt; new WebAssembly.Memory({initial:10, maximum:65535, shared:true})

However, if I set the `maximum` to something much lower like `10`, it works.

So I tried constructing those memory objects in a loop and see if it crashes at some point, but I was able to easily create 300.000 of those objects.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1949843</commentid>
    <comment_count>7</comment_count>
    <who name="Sam Denty">samddenty</who>
    <bug_when>2023-04-19 14:10:19 -0700</bug_when>
    <thetext>Until a fix for this lands, this can be worked around by doing the following:

1. Create a cloudflare worker with the following:

```js
export default {
  async fetch(request, env) {
    let url
    try {
      url = new URL(decodeURIComponent(new URL(request.url).search.slice(1)))
    } catch {
      return new Response(&quot;must provide a url&quot;, { status: 404 })
    }

    url.searchParams.set(&apos;memory-freed&apos;, 1)

    return Response.redirect(url, 302);
  }
}
```

2. Server-side: On the page using WASM &amp; Workers, if the URL doesn&apos;t contain the query string `memory-freed` do a 302 redirect to `https://WORKER_NAME.USER.workers.dev/?${url_with_query_string}`

3. Worker with above code will redirect back to page with `memory-freed=1` in the URL

4. Server-side: use middleware that detects the `memory-freed=1` in requests, and inject at the top of the document the following:

```
&lt;script&gt;{ const url = new URL(location.href); url.searchParams.delete(&apos;memory-freed&apos;); history.replaceState(null, &apos;&apos;, url) }&lt;/script&gt;
```


5. TADA, this bug is worked around!</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2009267</commentid>
    <comment_count>8</comment_count>
    <who name="Ivan Čurić">ivan.curic</who>
    <bug_when>2024-01-31 04:48:04 -0800</bug_when>
    <thetext>(In reply to Jake Archibald from comment #0)
&gt; Not sure this is the correct component. This bug is something to do with
&gt; workers/js/wasm.
&gt; 
&gt; 1. Go to https://safari-worker-memory-leak-demo.netlify.app
&gt; 2. Click &quot;draw using threads&quot;, and wait for the result. Repeat this step
&gt; around 20 times.
&gt; 
&gt; I start getting &quot;rangeerror: Out of memory&quot;.

@Jake, could you share the source code for this?

On iPhone 13, running iOS 16.7.1 I am unable to run even a single render using 4 threads. After booting up https://rreverser.com/wasm-bindgen-rayon-demo/ in another tab (which doesn&apos;t exhibit this issue), your demo runs out of memory during the wasm load.

Might be related to https://bugs.webkit.org/show_bug.cgi?id=222097</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2009269</commentid>
    <comment_count>9</comment_count>
    <who name="Jake Archibald">jaffathecake</who>
    <bug_when>2024-01-31 04:53:49 -0800</bug_when>
    <thetext>I agree that https://bugs.webkit.org/show_bug.cgi?id=222097 sounds the same.

Ugh, sorry, but I don&apos;t have access to the source code. It was just a quick hack on a laptop I no longer own.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2042865</commentid>
    <comment_count>10</comment_count>
    <who name="Yusuke Suzuki">ysuzuki</who>
    <bug_when>2024-06-24 20:31:50 -0700</bug_when>
    <thetext>Hi! Thanks, I tried it on STP and no longer reproduces.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2068376</commentid>
    <comment_count>11</comment_count>
    <who name="Ivan Čurić">ivan.curic</who>
    <bug_when>2024-10-16 06:47:44 -0700</bug_when>
    <thetext>This issue is still present in 18.0.1:

https://www.youtube.com/watch?v=4W_ScbuM0HI</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2068395</commentid>
    <comment_count>12</comment_count>
    <who name="Mark Lam">mark.lam</who>
    <bug_when>2024-10-16 08:10:53 -0700</bug_when>
    <thetext>(In reply to Ivan Čurić from comment #11)
&gt; This issue is still present in 18.0.1:
&gt; 
&gt; https://www.youtube.com/watch?v=4W_ScbuM0HI

Ivan, if you believe there is still an issue, please file a new bug and include details of:
1. What the issue is.
2. Steps to reproduce the issue, and what are the telltale signs of the issue manifesting (actual behavior) vs not (expected behavior).

Posting a video in a bug that is already resolved will probably not get any action because (1) this bug is already resolved, and (2) no one else knows what you&apos;re intending to show with that video.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>