fix potential deadlock in dlerror buffer handling at thread exit