Avoid race condition when waiting for write callback, but session getting closed before that. Hopefully fixes #41