I made another test case:
A Terminal session using direct connection and output connection variable (close connection not set), that has an uncaught exception is thrown in its body.
Indeed, the connection is not closed in this case. I made a bug report on this.
The workaround would be to use the approach I mentioned in the previous post: have your initial terminal session have no operations in it. Just establish the connection.