<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='xep.xsl'?>
<xep xmlns="">
<header>
  <title>Best Practice for Closing Idle Streams</title>
  <abstract>This document specifies a best practice for closing an XML stream that is perceived to be idle.</abstract>
  
<legal>
<copyright>This XMPP Extension Protocol is copyright © 1999 – 2024 by the <link url="https://xmpp.org/">XMPP Standards Foundation</link> (XSF).</copyright>
<permissions>Permission is hereby granted, free of charge, to any person obtaining a copy of this specification (the "Specification"), to make use of the Specification without restriction, including without limitation the rights to implement the Specification in a software program, deploy the Specification in a network service, and copy, modify, merge, publish, translate, distribute, sublicense, or sell copies of the Specification, and to permit persons to whom the Specification is furnished to do so, subject to the condition that the foregoing copyright notice and this permission notice shall be included in all copies or substantial portions of the Specification. Unless separate permission is granted, modified works that are redistributed shall not contain misleading information regarding the authors, title, number, or publisher of the Specification, and shall not claim endorsement of the modified works by the authors, any organization or project to which the authors belong, or the XMPP Standards Foundation.</permissions>
<warranty>## NOTE WELL: This Specification is provided on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. ##</warranty>
<liability>In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall the XMPP Standards Foundation or any author of this Specification be liable for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising from, out of, or in connection with the Specification or the implementation, deployment, or other use of the Specification (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if the XMPP Standards Foundation or such author has been advised of the possibility of such damages.</liability>
<conformance>This XMPP Extension Protocol has been contributed in full conformance with the XSF's Intellectual Property Rights Policy (a copy of which can be found at &lt;<link url="https://xmpp.org/about/xsf/ipr-policy">https://xmpp.org/about/xsf/ipr-policy</link>&gt; or obtained by writing to XMPP Standards Foundation, P.O. Box 787, Parker, CO 80134 USA).</conformance>
</legal>
  <number>0190</number>
  <status>Obsolete</status>
  <type>Informational</type>
  <sig>Standards</sig>
  <approver>Council</approver>
  <dependencies>
    <spec>XMPP Core</spec>
  </dependencies>
  <supersedes/>
  <supersededby>
    <spec>RFC 6120</spec>
  </supersededby>
  <shortname>N/A</shortname>
  <author>
    <firstname>Carlo</firstname>
    <surname>von Loesch</surname>
    <email>lynX@jabber.getting.psyced.org</email>
    <jid>lynX@ve.symlynX.com</jid>
  </author>
  <revision>
    <version>1.1</version>
    <date>2012-03-06</date>
    <initials>psa</initials>
    <remark><p>Changed status to Obsolete because it is superseded by RFC 6120.</p></remark>
  </revision>
  <revision>
    <version>1.0</version>
    <date>2007-01-04</date>
    <initials>psa</initials>
    <remark>Per a vote of the XMPP Council, advanced status to Active.</remark>
  </revision>
  <revision>
    <version>0.1</version>
    <date>2006-07-26</date>
    <initials>psa</initials>
    <remark><p>Initial version.</p></remark>
  </revision>
  <revision>
    <version>0.0.2</version>
    <date>2006-06-30</date>
    <initials>cvl</initials>
    <remark>Second draft.</remark>
  </revision>
  <revision>
    <version>0.0.1</version>
    <date>2006-05-31</date>
    <initials>cvl</initials>
    <remark>First draft.</remark>
  </revision>
</header>

<section1 topic="Introduction" anchor="intro">
  <p><span class="ref"><link url="http://tools.ietf.org/html/rfc3920">RFC 3920</link></span> <note>RFC 3920: Extensible Messaging and Presence Protocol (XMPP): Core &lt;<link url="http://tools.ietf.org/html/rfc3920">http://tools.ietf.org/html/rfc3920</link>&gt;.</note> describes several ways to terminate an XML stream, but does not always make a clear statement about which to use. This can lead to faulty implementations. In particular, closing a stream that has not been in use for a while is very often achieved using a connection-timeout error, then closing the socket. This can lead to loss of data. Therefore this document proposes a practice that will avoid such data loss.</p>
  <p>Note: The recommendation described herein has been incorporated into <span class="ref"><link url="http://tools.ietf.org/html/rfc6120">RFC 6120</link></span> <note>RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core &lt;<link url="http://tools.ietf.org/html/rfc6120">http://tools.ietf.org/html/rfc6120</link>&gt;.</note>.</p>
</section1>

<section1 topic="How to Close an Idle Stream" anchor="do">
  <section2 topic="Handshake Stream Shutdown">
    <p>As shown in the basic "session" example in the Simplified Stream Examples (4.8 of RFC 3920), it is a valid transaction to close the outgoing stream by sending</p>
	<code><![CDATA[</stream:stream>]]></code>
    <p>then wait for the other entity to close its stream, like this:</p>
	<code><![CDATA[</stream:stream>]]></code>
    <p>and shut down the underlying TCP connection.</p>
    <p>This will ensure that, should the other entity have transmitted any data, it will arrive and be processed before the TCP connection is terminated.</p>
    <p>Special care MUST be taken that under no circumstance further packets may be written to the socket after the stream was closed, until the other side shuts down the socket.</p>
    <p>On the outgoing TCP connection, an implementation MAY do a read-only shutdown of the socket, as long as the other side will safely be able to send its stream termination token.</p>
  </section2>
  <section2 topic="Handshake Failure">
    <p>In case the other entity fails to close the stream within a reasonable time frame, the entity that started the handshake is entitled to terminate the TCP connection. Since the stream has already been closed, it is correct not to produce an error condition.</p>
  </section2>
</section1>

<section1 topic="Implementation Notes" anchor="impl">
 <p>Existing implementations should be updated to use the 'Handshake Stream Shutdown' strategy when shutting down streams that are no longer needed. This strategy is fully backwards-compatible and does not introduce any known communication problems.</p>
</section1>

<section1 topic="Security Considerations" anchor="security">
  <p>This proposal introduces no new security aspects.</p>
</section1>

<section1 topic="IANA Considerations" anchor="iana">
  <p>This proposal requires no interaction with the <span class="ref"><link url="http://www.iana.org/">Internet Assigned Numbers Authority (IANA)</link></span> <note>The Internet Assigned Numbers Authority (IANA) is the central coordinator for the assignment of unique parameter values for Internet protocols, such as port numbers and URI schemes. For further information, see &lt;<link url="http://www.iana.org/">http://www.iana.org/</link>&gt;.</note>.</p>
</section1>

<section1 topic="XMPP Registrar Considerations" anchor="registrar">
  <p>This proposal requires no interaction with the <span class="ref"><link url="https://xmpp.org/registrar/">XMPP Registrar</link></span> <note>The XMPP Registrar maintains a list of reserved protocol namespaces as well as registries of parameters used in the context of XMPP extension protocols approved by the XMPP Standards Foundation. For further information, see &lt;<link url="https://xmpp.org/registrar/">https://xmpp.org/registrar/</link>&gt;.</note>.</p>
</section1>

</xep>
