<html>
    <head>
      <base href="https://bugs.freedesktop.org/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Priority</th>
          <td>medium
          </td>
        </tr>

        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Surfaceless contexts are not properly released by DRI drivers"
   href="https://bugs.freedesktop.org/show_bug.cgi?id=74563">74563</a>
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>mesa-dev@lists.freedesktop.org
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Surfaceless contexts are not properly released by DRI drivers
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux (All)
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>alexandros.frantzis@canonical.com
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>x86-64 (AMD64)
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>git
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Other
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>Mesa
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=93443" name="attach_93443" title="Example that exhibits the issue">attachment 93443</a> <a href="attachment.cgi?id=93443&action=edit" title="Example that exhibits the issue">[details]</a></span>
Example that exhibits the issue

Releasing a surfaceless EGL context doesn't properly release the underlying DRI
context, sometimes leading to crashes. I have attached an example that exhibits
such a crash (run it in a VT).

Without a fix, the example crashes when the main thread tries to make context2
current, because mesa tries to _mesa_flush the invalid (destroyed in the other
thread) context1, left as current in the main thread by the previous make
current operation (and not cleaned by releasing the egl context).

The problem seems to be that the driUnbindContext() function doesn't call the
driver unbind function if the context is surfaceless. Moving the call to the
driver function before the drawable validity checks fixes things for me (see
attached patch), although I am not familiar enough with that code to be
confident that this is indeed the right approach.

Note the absence of _mesa_make_current() calls for context releases in the
logs:

# MESA_VERBOSE=api ./mesa_surfaceless_context_crash
T1: Make current 1
Mesa: _mesa_make_current()
T1: Release current 1
T1: after Release current 1
Main: Make current 1
Mesa: _mesa_make_current()
Main: Release current 1
Main: after Release current 1
Mesa: _mesa_make_current()
Mesa: glDeleteObjectARB(0)
Mesa: glDeleteObjectARB(0)
Mesa: glDeleteTextures 1
Mesa: _mesa_make_current()
Mesa: _mesa_make_current()
Main: Make current 2
<crash>

I am using latest Mesa git with the i965 driver.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are the assignee for the bug.</li>
      </ul>
    </body>
</html>