buffet: XMPP channel sockets are left behind unclosed

When an XMPP channel is closed the underlying TCP socket isn't closed
properly.

The main problem is that when we reset the connection, we do not call
NotificationChannel::Stop() to close the underlying socket. However,
chromeos::FileStream also fails to close the file descriptor in case
when Stream::Close wasn't called explicitly. And TlsStream wasn't
closing TLS session automatically if Stream::Close hasn't been called
manually. In all fairness, a stream should be closed manually, since
this is the only way to process possible errors on close, but still
we should not leak resources (like file descriptors) if the Close
hasn't been called and a stream is destroyed.

BUG=brillo:1134
TEST=`FEATURES=test emerge-link libchromeos buffet`

Change-Id: Ic71a883bc6e9e51a49249c79b4106efd0520ab68
Reviewed-on: https://chromium-review.googlesource.com/273346
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Vitaly Buka <vitalybuka@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/device_registration_info.cc b/buffet/device_registration_info.cc
index 3435881..f31495c 100644
--- a/buffet/device_registration_info.cc
+++ b/buffet/device_registration_info.cc
@@ -295,6 +295,9 @@
   // TODO(avakulenko): Move this into a notification channel factory and out of
   // this class completely. Also to be added the secondary (poll) notification
   // channel.
+  if (primary_notification_channel_)
+    primary_notification_channel_->Stop();
+
   primary_notification_channel_.reset(
       new XmppChannel{config_->robot_account(),
                       access_token_,
diff --git a/buffet/notification/xmpp_channel.h b/buffet/notification/xmpp_channel.h
index 4afb5df..73daee3 100644
--- a/buffet/notification/xmpp_channel.h
+++ b/buffet/notification/xmpp_channel.h
@@ -95,7 +95,7 @@
   std::string access_token_;
 
   chromeos::StreamPtr raw_socket_;
-  chromeos::StreamPtr tls_stream_;
+  chromeos::StreamPtr tls_stream_;  // Must follow |raw_socket_|.
 
   // Read buffer for incoming message packets.
   std::vector<char> read_socket_data_;