# File lib/phusion_passenger/analytics_logger.rb, line 118
                def close(flush_to_disk = false)
                        @connection.synchronize do
                                begin
                                        # We need an ACK here. See abstract_request_handler.rb finalize_request.
                                        @connection.channel.write("closeTransaction", @txn_id,
                                                AnalyticsLogger.timestamp_string, true)
                                        result = @connection.channel.read
                                        if result != ["ok"]
                                                raise "Expected logging agent to respond with 'ok', but got #{result.inspect} instead"
                                        end
                                        if flush_to_disk
                                                @connection.channel.write("flush")
                                                result = @connection.channel.read
                                                if result != ["ok"]
                                                        raise "Invalid logging agent response #{result.inspect} to the 'flush' command"
                                                end
                                        end
                                rescue SystemCallError, IOError => e
                                        @connection.disconnect
                                        DebugLogging.warn("Error communicating with the logging agent: #{e.message}")
                                rescue Exception => e
                                        @connection.disconnect
                                        raise e
                                ensure
                                        @connection.unref
                                        @connection = nil
                                end
                        end if @connection
                end