When closing the last window, the application gets terminated no matter what. This means that existing UNO connections are closed too. Proposed solution: The application should not be terminated when there are active UNO connections, when closing the last window. The behavior of "File->Exit LibreOffice" should stay unchanged and indeed exit the application even if there are active connections. See also discussion on mailing list: https://lists.freedesktop.org/archives/libreoffice/2016-September/075226.html
Samuel Mehrbrodt committed a patch related to this issue. It has been pushed to "master": http://cgit.freedesktop.org/libreoffice/core/commit/?id=ecec5524476448d35dd24c5594c2fcbb1a8b6218 tdf#102274 Closing LibreOffice should not kill active UNO connections It will be available in 5.3.0. The patch should be included in the daily builds available at http://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More information about daily builds can be found at: http://wiki.documentfoundation.org/Testing_Daily_Builds Affected users are encouraged to test the fix and report feedback.
With the mailing list discussion in view, I suppose, that the solution you implemented would benefit from a warning when closing last window like that: "You are closing last LibreOffice window. Currently, there are open connections to this instance of LO. Therefore, it will continue running in background until the connections are closed by applications using them. To force terminating LO, use File->Exit LibreOffice". I would go furhter and add a similar warning at File->Exit LibreOffice, this time with options to confirm terminating or not. I'm afraid that this will be opposed as "too intrusive interface". But any "too intrusive interface" could have an option to be disabled ("Don't show this next time") for power users; while for ordinary user (whom this feature is tergeted to, and who are the majority), it could be equally disturbing to find that the application they had closed is still running without notice, and to find that other applications that depend on LO (user may be unaware of this) faild unexpectedly because of his interaction with LO (including Exit LibreOffice). The warning would make things clear, while achieve the goal: allowing LO to continue and allow all windows to be closed.
moving it to NEW
This looks fixed as reported - further improvements into separate bug reports please.