@Override
        public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output) {
          Metrics metrics = MetricsFactory.getInstance();
          if (metrics != null) {
            metrics.decrementCounter(MetricsConstant.OPEN_CONNECTIONS);
          }

          final ThriftCLIServerContext context = (ThriftCLIServerContext) serverContext;
          final Optional<SessionHandle> sessionHandle = context.getSessionHandle();

          if (sessionHandle.isPresent()) {
            // Normally, the client should politely inform the server it is
            // closing its session with Hive before closing its network
            // connection. However, if the client connection dies for any reason
            // (load-balancer round-robin configuration, firewall kills
            // long-running sessions, bad client, failed client, timed-out
            // client, etc.) then the server will close the connection without
            // having properly cleaned up the Hive session (resources,
            // configuration, logging etc.). That needs to be cleaned up now.
            LOG.warn(
                "Client connection bound to {} unexpectedly closed: closing this Hive session to release its resources. "
                    + "The connection processed {} total messages during its lifetime of {}ms. Inspect the client connection "
                    + "for time-out, firewall killing the connection, invalid load balancer configuration, etc.",
                sessionHandle, context.getMessagesProcessedCount(), context.getDuration().toMillis());
            try {
              final boolean close = cliService.getSessionManager().getSession(sessionHandle.get()).getHiveConf()
                  .getBoolVar(ConfVars.HIVE_SERVER2_CLOSE_SESSION_ON_DISCONNECT);
              if (close) {
                cliService.closeSession(sessionHandle.get());
              } else {
                LOG.warn("Session not actually closed because configuration {} is set to false",
                    ConfVars.HIVE_SERVER2_CLOSE_SESSION_ON_DISCONNECT.varname);
              }
            } catch (HiveSQLException e) {
              LOG.warn("Failed to close session", e);
            }
          } else {
            // There is no session handle because the client gracefully closed
            // the session *or* because the client had some issue and never was
            // able to create one in the first place
            if (context.getSessionCount() == 0) {
              LOG.info("A client connection was closed before creating a Hive session. "
                  + "Most likely it is a client that is connecting to this server then "
                  + "immediately closing the socket (i.e., TCP health check or port scanner)");
            }
          }
        }
This is called by the Thrift server when the underlying client
connection is cleaned up by the server because the connection has
been closed.
service/src/java/org/apache/hive/service/cli/thrift/ThriftBinaryCLIService.java