<p dir="auto">I tried to stream a template for a change but had Problem with the Session.<br>
I best show a little example:</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="public function someAction (Request $request) {
    $request-&gt;getSession()-&gt;start(); // ensure the session is started
    return $this-&gt;stream('template.html.twig');
}"><pre class="notranslate"><span class="pl-k">public</span> <span class="pl-k">function</span> <span class="pl-en">someAction</span> (<span class="pl-smi"><span class="pl-smi">Request</span></span> <span class="pl-s1"><span class="pl-c1">$</span>request</span>) {
    <span class="pl-s1"><span class="pl-c1">$</span>request</span>-&gt;<span class="pl-en">getSession</span>()-&gt;<span class="pl-en">start</span>(); <span class="pl-c">// ensure the session is started</span>
    <span class="pl-k">return</span> <span class="pl-s1"><span class="pl-c1">$</span><span class="pl-smi">this</span></span>-&gt;<span class="pl-en">stream</span>(<span class="pl-s">'template.html.twig'</span>);
}</pre></div>
<div class="highlight highlight-text-html-twig notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Title&lt;/title&gt;
    &lt;/head&gt;
    {% flush %}
    &lt;body&gt;
        {{ app.session.flashBag.all|length }}{# accessing the session now fails #}
    &lt;/body&gt;
&lt;/html&gt;"><pre class="notranslate">&lt;<span class="pl-ent">html</span>&gt;
    &lt;<span class="pl-ent">head</span>&gt;
        &lt;<span class="pl-ent">title</span>&gt;Title&lt;/<span class="pl-ent">title</span>&gt;
    &lt;/<span class="pl-ent">head</span>&gt;
    {% <span class="pl-k">flush</span> %}
    &lt;<span class="pl-ent">body</span>&gt;
        {{ <span class="pl-smi">app</span>.<span class="pl-smi">session</span>.<span class="pl-smi">flashBag</span>.<span class="pl-smi">all</span><span class="pl-k">|</span><span class="pl-c1">length</span> }}<span class="pl-c"><span class="pl-c">{#</span> accessing the session now fails <span class="pl-c">#}</span></span>
    &lt;/<span class="pl-ent">body</span>&gt;
&lt;/<span class="pl-ent">html</span>&gt;</pre></div>
<p dir="auto">This results into a <code class="notranslate">RuntimeException</code>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[1/2] RuntimeException: Failed to start the session because headers have already been sent by &quot;&quot; at line 0.   -"><pre class="notranslate"><code class="notranslate">[1/2] RuntimeException: Failed to start the session because headers have already been sent by "" at line 0.   -
</code></pre></div>
<p dir="auto">I'm using Symfony 2.7.9 with PHP 7.0.2-4+deb.sury.org~trusty+1.</p>
<p dir="auto">UPDATE: This issue has 2 parts. The first one is explained in this post. The second one is explained in <a href="https://github.com/symfony/symfony/issues/6036#issuecomment-10671945" data-hovercard-type="issue" data-hovercard-url="/symfony/symfony/issues/6036/hovercard">this comment</a>.</p>
<p dir="auto">I noticed the website I am developing was sending a cookie in the response, even for anonymous requests (ie: not logged in, and deleted all cookies).</p>
<p dir="auto">After trying everything and failing to find the problem, I created a "hello world" with a fresh install, and there was no cookie.</p>
<p dir="auto">But as soon as I added this, the cookie came back:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{% for flashMessage in app.session.flashbag.get('success') %}
    &lt;div class=&quot;alert alert-success&quot;&gt;
        &lt;button type=&quot;button&quot; class=&quot;close&quot; data-dismiss=&quot;alert&quot;&gt;×&lt;/button&gt;
        {{ flashMessage }}
    &lt;/div&gt;
{% endfor %}"><pre class="notranslate"><code class="notranslate">{% for flashMessage in app.session.flashbag.get('success') %}
    &lt;div class="alert alert-success"&gt;
        &lt;button type="button" class="close" data-dismiss="alert"&gt;×&lt;/button&gt;
        {{ flashMessage }}
    &lt;/div&gt;
{% endfor %}
</code></pre></div>
<p dir="auto">So apparently <code class="notranslate">app.session.flashbag.get()</code> creates the session. I'm sure many other parts of my website are unintentionally creating a session, and that's why I couldn't find the culprit.</p>
<p dir="auto">Is there a reason for this? Isn't it adding overhead that could have been avoided? I imagine this won't affect sites that are login-only, as they will need the session anyway for most of the visits. But sites that can be used anonymously might suffer a bit.</p>