<?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>230192</bug_id>
          
          <creation_ts>2021-09-11 11:52:21 -0700</creation_ts>
          <short_desc>Decoding audio data and creating a source node causes safari to crash</short_desc>
          <delta_ts>2021-09-13 04:03:15 -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>Web Audio</component>
          <version>Safari 14</version>
          <rep_platform>All</rep_platform>
          <op_sys>All</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          
          <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="Jason">jasonlmcaffee</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>ben</cc>
    
    <cc>cdumez</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1792726</commentid>
    <comment_count>0</comment_count>
    <who name="Jason">jasonlmcaffee</who>
    <bug_when>2021-09-11 11:52:21 -0700</bug_when>
    <thetext>We have an application that plays songs as users learn to play the piano.  We noticed that the app would crash after users would switch between songs a couple of dozen times (depends on which songs).
We isolated the issue to calling context.decodeAudioData and assigning that decoded audio data to a new context.createBufferSource. 

There appears to be a memory leak which causes the app to start to slow down, then causes the browser to crash &amp; restart with &quot;webpage was reloaded because a problem occurred.&quot; on iOS, or message &quot;This webpage was reloaded because it was using significant memory&quot; on desktop.

Verified issue occurs after ~12 iterations on iPhone 11 max pro running iOS 14.3.
Verified issue occurs after ~12 iterations on iPad 5th Gen running iOS 14.7.1.
Verified issue occurs after ~40 iterations on 2018 MacBook Pro running Big Sur 11.3.1.

Code demonstrating the issue
https://codepen.io/jasonmcaffee/pen/KKqmxEX

Relevant code
async function load(){
  const context = new AudioContext();
  const url = &quot;https://dalnn20hi8hmy.cloudfront.net/r1/00/00/59/4i/slice_15716_resourcefile_47285_20200901_22h33m22s_mdt.mp3&quot;;
  const arrayBuffer = await fetchAudioData({url, context});

  //decoding the array buffer and creating a source node causes ios 14.7.1 Safari to crash after ~10 iterations on an iPad 5th gen. also crashes ios 14.3 on iPhone 11 pro max.
  while(!stop){
    const audioBuffer = await decodeAudioData(context, arrayBuffer.slice(0)); //use slice(0) to avoid detached array buffer error.
    const source = createAudioSourceFromAudioBuffer(audioBuffer, context);
    print(`${i++} done creating buffer source.`);
    await sleep(100);
  }
  
}

async function sleep(ms: number){
  return new Promise((resolve)=&gt;{
    setTimeout(resolve, ms);
  });
}


async function fetchAudioData({url, context, signal}: {url: string; context: AudioContext, signal?: AbortSignal}){
  const response = await fetch(url, {cache: &quot;force-cache&quot;, signal});
  const arrayBuffer = await response.arrayBuffer();
  return arrayBuffer;
}  

async function decodeAudioData(context: AudioContext, arrayBuffer: ArrayBuffer): Promise&lt;AudioBuffer&gt;{
  return new Promise((resolve, reject) =&gt; context.decodeAudioData(arrayBuffer, resolve, reject));
}

function createAudioSourceFromAudioBuffer(audioBuffer: AudioBuffer, context: AudioContext | OfflineAudioContext): AudioBufferSourceNode {
  const source = context.createBufferSource();
  source.buffer = audioBuffer;
  return source;
}</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1792909</commentid>
    <comment_count>1</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2021-09-13 04:03:15 -0700</bug_when>
    <thetext>&lt;rdar://problem/83048575&gt;</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>