See the glXDestroyContext man page:
If GLX rendering context ctx is not current to any thread,
glXDestroyContext destroys it immediately. Otherwise, ctx is destroyed
when it becomes not current to any thread. In either case, the resource ID
referenced by ctx is freed immediately.
In other words, if we want glXDestroyContext to have the well-defined semantics
of destroying the context before future X commands take effect, we must first
release the GL context before calling it. We were failing to do that, but we
were destroying the drawable immediately after that call, and as a result, the
context was outliving its underlying drawable. This eventually resulted in
X_GLXMakeCurrent: GLXBadContextTag X errors on subsequent glXMakeCurrent calls.