fix restoring thread context when callback throws an exception (fix #219)