Backported postfixes for CORE-3935 & CORE-3944: helps avoid segfaults/deadlocks when shutting down firebird

git-svn-id: svn+ssh://svn.code.sf.net/p/firebird/code/firebird/branches/B2_5_Release@58522 65644016-39b1-43b1-bf79-96bc8fe82c15