Skip to content

Commit 365e6a4

Browse files
authored
gh-130902 @Mr-Sunglasses update documentation for http socket close
1 parent 07a2262 commit 365e6a4

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

Doc/library/socket.rst

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,28 +1485,35 @@ to sockets.
14851485
.. availability:: not WASI.
14861486

14871487

1488-
.. method:: socket.close()
1488+
.. method:: socket.close(shutdown=False)
14891489

1490-
Mark the socket closed. The underlying system resource (e.g. a file
1491-
descriptor) is also closed when all file objects from :meth:`makefile`
1492-
are closed. Once that happens, all future operations on the socket
1493-
object will fail. The remote end will receive no more data (after
1494-
queued data is flushed).
1490+
Mark the socket closed. The underlying system resource (e.g., a file descriptor) is also
1491+
closed when all file objects from :meth:`makefile` are closed. Once that happens, all future
1492+
operations on the socket object will fail. The remote end will receive no more data
1493+
(after queued data is flushed).
14951494

1496-
Sockets are automatically closed when they are garbage-collected, but
1497-
it is recommended to :meth:`close` them explicitly, or to use a
1495+
If the `shutdown` parameter is set to ``True``, the socket will first be
1496+
shut down before closing, ensuring that no further data can be sent or received.
1497+
This is useful for properly releasing resources and preventing issues like lingering
1498+
connections or reset by peer (RST) errors in some network conditions. If the parameter is
1499+
ommited or set to false, the function will continue its normal behavior
1500+
1501+
Sockets are automatically closed when they are garbage-collected, but
1502+
it is recommended to :meth:`close` them explicitly, or to use a
14981503
:keyword:`with` statement around them.
14991504

1505+
.. versionadded:: 3.14
1506+
Added an optional `shutdown` parameter to allow explicit socket shutdown before closing.
1507+
15001508
.. versionchanged:: 3.6
15011509
:exc:`OSError` is now raised if an error occurs when the underlying
15021510
:c:func:`close` call is made.
15031511

15041512
.. note::
15051513

1506-
:meth:`close` releases the resource associated with a connection but
1507-
does not necessarily close the connection immediately. If you want
1508-
to close the connection in a timely fashion, call :meth:`shutdown`
1509-
before :meth:`close`.
1514+
:meth:`close` releases the resource associated with a connection
1515+
but does not necessarily close the connection immediately. If you want to close the connection in a timely fashion, call :meth:`shutdown` before :meth:`close`, or use this function with the shutdown parameter like this
1516+
``socket.close(shutdown=True)
15101517
15111518
15121519
.. method:: socket.connect(address)

0 commit comments

Comments
 (0)