You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Doc/library/socket.rst
+19-12Lines changed: 19 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1485,28 +1485,35 @@ to sockets.
1485
1485
.. availability:: not WASI.
1486
1486
1487
1487
1488
-
.. method:: socket.close()
1488
+
.. method:: socket.close(shutdown=False)
1489
1489
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).
1495
1494
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
1498
1503
:keyword:`with` statement around them.
1499
1504
1505
+
.. versionadded:: 3.14
1506
+
Added an optional `shutdown` parameter to allow explicit socket shutdown before closing.
1507
+
1500
1508
.. versionchanged:: 3.6
1501
1509
:exc:`OSError` is now raised if an error occurs when the underlying
1502
1510
:c:func:`close` call is made.
1503
1511
1504
1512
.. note::
1505
1513
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
0 commit comments