<p dir="auto">Hi,<br>
I have a suite of tests that takes ~3 minutes <strong>without</strong> using the awesome video feature and ~4+ minutes (133%) <strong>with</strong> the video feature.</p>
<p dir="auto">I looked at the logs and saw that each context closing takes few more seconds than without video.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="2021-02-08T10:52:04.726Z pw:api =&gt; browserContext.close started
2021-02-08T10:52:15.962Z pw:api &lt;= browserContext.close succeeded
Success notification after 11 seconds"><pre class="notranslate"><code class="notranslate">2021-02-08T10:52:04.726Z pw:api =&gt; browserContext.close started
2021-02-08T10:52:15.962Z pw:api &lt;= browserContext.close succeeded
Success notification after 11 seconds
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="2021-02-08T10:51:11.580Z pw:api =&gt; browserContext.close started
2021-02-08T10:51:20.406Z pw:api &lt;= browserContext.close succeeded
Success notification after 9 seconds"><pre class="notranslate"><code class="notranslate">2021-02-08T10:51:11.580Z pw:api =&gt; browserContext.close started
2021-02-08T10:51:20.406Z pw:api &lt;= browserContext.close succeeded
Success notification after 9 seconds
</code></pre></div>
<p dir="auto">Is there a way to prevent this duration increase?</p>
<p dir="auto"><strong>Context:</strong></p>
<ul dir="auto">
<li>Playwright Version: ^1.8.0-next-1611032356000</li>
<li>Operating System: MacOS 10.15.7</li>
<li>Node.js version: v12.18.3</li>
<li>Browser: Chromium</li>
</ul>


<p dir="auto"><strong>Describe the bug</strong><br>
Hi, guys! I’ve added video recording to my tests as described in the documentation. Between tests I want to close all contexts except one, because I need sometimes 2, sometimes 1 context (I use approach 1 context = 1 page, because it’s easier to maintain for my application).</p>
<p dir="auto">So, the problem is: when I’m closing context after 2-contexts test I see:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="  pw:api =&gt; browserContext.close started +1ms
  pw:api &lt;= browserContext.close succeeded +51s"><pre class="notranslate"><code class="notranslate">  pw:api =&gt; browserContext.close started +1ms
  pw:api &lt;= browserContext.close succeeded +51s
</code></pre></div>
<p dir="auto">It takes 50-60 seconds, so it's too long. I have a lot of such "switches".<br>
I can provide additional logs if you tell me what to do. Could you please help? Maybe I'm doing something wrong.</p>
<p dir="auto">Thanks in advance!</p>
<p dir="auto"><strong>Code Snippet</strong></p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// how I create context and page
let parameters = {};
if (config.get('recordFailedTestVideo')) {
    parameters = {
        recordVideo: {
            dir: config.get('videosDir'),
        },
    };
}

const ctx = await this._instance.newContext(parameters);
this._contexts.push({
    ctx: ctx,
    occupied: true,
});

const page = await ctx.newPage();
page.setDefaultTimeout(Timeouts.FifteenSecondsTimeout);
this._pages.push({
    page: page,
    occupied: true,
});

// how I close contexts
for (let i = 1; i &lt; this._contexts.length; i++) {
      await this._contexts[i].ctx.close(); // here is the problem
      this._contexts.splice(i, 1);
      this._pages.splice(i, 1);
}"><pre class="notranslate"><span class="pl-c">// how I create context and page</span>
<span class="pl-k">let</span> <span class="pl-s1">parameters</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">;</span>
<span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">config</span><span class="pl-kos">.</span><span class="pl-en">get</span><span class="pl-kos">(</span><span class="pl-s">'recordFailedTestVideo'</span><span class="pl-kos">)</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-s1">parameters</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span>
        <span class="pl-c1">recordVideo</span>: <span class="pl-kos">{</span>
            <span class="pl-c1">dir</span>: <span class="pl-s1">config</span><span class="pl-kos">.</span><span class="pl-en">get</span><span class="pl-kos">(</span><span class="pl-s">'videosDir'</span><span class="pl-kos">)</span><span class="pl-kos">,</span>
        <span class="pl-kos">}</span><span class="pl-kos">,</span>
    <span class="pl-kos">}</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-k">const</span> <span class="pl-s1">ctx</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_instance</span><span class="pl-kos">.</span><span class="pl-en">newContext</span><span class="pl-kos">(</span><span class="pl-s1">parameters</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_contexts</span><span class="pl-kos">.</span><span class="pl-en">push</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
    <span class="pl-c1">ctx</span>: <span class="pl-s1">ctx</span><span class="pl-kos">,</span>
    <span class="pl-c1">occupied</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-k">const</span> <span class="pl-s1">page</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-s1">ctx</span><span class="pl-kos">.</span><span class="pl-en">newPage</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-s1">page</span><span class="pl-kos">.</span><span class="pl-en">setDefaultTimeout</span><span class="pl-kos">(</span><span class="pl-v">Timeouts</span><span class="pl-kos">.</span><span class="pl-c1">FifteenSecondsTimeout</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_pages</span><span class="pl-kos">.</span><span class="pl-en">push</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
    <span class="pl-c1">page</span>: <span class="pl-s1">page</span><span class="pl-kos">,</span>
    <span class="pl-c1">occupied</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-c">// how I close contexts</span>
<span class="pl-k">for</span> <span class="pl-kos">(</span><span class="pl-k">let</span> <span class="pl-s1">i</span> <span class="pl-c1">=</span> <span class="pl-c1">1</span><span class="pl-kos">;</span> <span class="pl-s1">i</span> <span class="pl-c1">&lt;</span> <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_contexts</span><span class="pl-kos">.</span><span class="pl-c1">length</span><span class="pl-kos">;</span> <span class="pl-s1">i</span><span class="pl-c1">++</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
      <span class="pl-k">await</span> <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_contexts</span><span class="pl-kos">[</span><span class="pl-s1">i</span><span class="pl-kos">]</span><span class="pl-kos">.</span><span class="pl-c1">ctx</span><span class="pl-kos">.</span><span class="pl-en">close</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// here is the problem</span>
      <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_contexts</span><span class="pl-kos">.</span><span class="pl-en">splice</span><span class="pl-kos">(</span><span class="pl-s1">i</span><span class="pl-kos">,</span> <span class="pl-c1">1</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
      <span class="pl-smi">this</span><span class="pl-kos">.</span><span class="pl-c1">_pages</span><span class="pl-kos">.</span><span class="pl-en">splice</span><span class="pl-kos">(</span><span class="pl-s1">i</span><span class="pl-kos">,</span> <span class="pl-c1">1</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto"><strong>Update:</strong> here is repo to reproduce: <a href="https://github.com/viraxslot/close-context-problem.git">https://github.com/viraxslot/close-context-problem.git</a><br>
Hope it'll help.</p>