Fixed issue with cancelling long running queries in server.

JsonGenerators were not properly closed in finally blocks which lead to
malformed json when queries were cancelled by the user.