Skip to content

Commit 532ad03

Browse files
jayaddisonAA-Turnerpicnixz
authored
linkcheck: Make the new 'timeout' status opt-in (#12023)
Co-authored-by: Adam Turner <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]>
1 parent db7ba52 commit 532ad03

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ Bugs fixed
108108
responses as broken.
109109
Patch by James Addison.
110110
* #11868: linkcheck: added a distinct ``timeout`` reporting status code.
111+
This can be enabled by setting :confval:`linkcheck_report_timeouts_as_broken`
112+
to ``False``.
111113
Patch by James Addison.
112114
* #11869: Refresh the documentation for the ``linkcheck_timeout`` setting.
113115
Patch by James Addison.

doc/usage/configuration.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2971,6 +2971,21 @@ Options for the linkcheck builder
29712971

29722972
.. versionadded:: 7.3
29732973

2974+
.. confval:: linkcheck_report_timeouts_as_broken
2975+
2976+
When an HTTP response is not received from a webserver before the configured
2977+
:confval:`linkcheck_timeout` expires,
2978+
the current default behaviour of Sphinx is to treat the link as 'broken'.
2979+
To report timeouts using a distinct report code of ``timeout``,
2980+
set :confval:`linkcheck_report_timeouts_as_broken` to ``False``.
2981+
2982+
From Sphinx 8.0 onwards, timeouts that occur while checking hyperlinks
2983+
will be reported using the new 'timeout' status code.
2984+
2985+
.. xref RemovedInSphinx80Warning
2986+
2987+
.. versionadded:: 7.3
2988+
29742989

29752990
Options for the XML builder
29762991
---------------------------

sphinx/builders/linkcheck.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ def init(self) -> None:
7575
)
7676
warnings.warn(deprecation_msg, RemovedInSphinx80Warning, stacklevel=1)
7777

78+
if self.config.linkcheck_report_timeouts_as_broken:
79+
deprecation_msg = (
80+
"The default value for 'linkcheck_report_timeouts_as_broken' will change "
81+
'to False in Sphinx 8, meaning that request timeouts '
82+
"will be reported with a new 'timeout' status, instead of as 'broken'. "
83+
'This is intended to provide more detail as to the failure mode. '
84+
'See https://github.com/sphinx-doc/sphinx/issues/11868 for details.'
85+
)
86+
warnings.warn(deprecation_msg, RemovedInSphinx80Warning, stacklevel=1)
87+
7888
def finish(self) -> None:
7989
checker = HyperlinkAvailabilityChecker(self.config)
8090
logger.info('')
@@ -302,6 +312,10 @@ def __init__(self, config: Config,
302312
self.retries: int = config.linkcheck_retries
303313
self.rate_limit_timeout = config.linkcheck_rate_limit_timeout
304314
self._allow_unauthorized = config.linkcheck_allow_unauthorized
315+
if config.linkcheck_report_timeouts_as_broken:
316+
self._timeout_status = 'broken'
317+
else:
318+
self._timeout_status = 'timeout'
305319

306320
self.user_agent = config.user_agent
307321
self.tls_verify = config.tls_verify
@@ -444,7 +458,7 @@ def _check_uri(self, uri: str, hyperlink: Hyperlink) -> tuple[str, str, int]:
444458
break
445459

446460
except RequestTimeout as err:
447-
return 'timeout', str(err), 0
461+
return self._timeout_status, str(err), 0
448462

449463
except SSLError as err:
450464
# SSL failure; report that the link is broken.
@@ -669,6 +683,7 @@ def setup(app: Sphinx) -> ExtensionMetadata:
669683
app.add_config_value('linkcheck_anchors_ignore_for_url', (), '', (tuple, list))
670684
app.add_config_value('linkcheck_rate_limit_timeout', 300.0, '')
671685
app.add_config_value('linkcheck_allow_unauthorized', True, '')
686+
app.add_config_value('linkcheck_report_timeouts_as_broken', True, '', bool)
672687

673688
app.add_event('linkcheck-process-uri')
674689

tests/test_builders/test_build_linkcheck.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,13 @@ def test_too_many_requests_retry_after_without_header(app, capsys):
856856
)
857857

858858

859-
@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver', freshenv=True)
859+
@pytest.mark.sphinx(
860+
'linkcheck', testroot='linkcheck-localserver', freshenv=True,
861+
confoverrides={
862+
'linkcheck_report_timeouts_as_broken': False,
863+
'linkcheck_timeout': 0.01,
864+
}
865+
)
860866
def test_requests_timeout(app):
861867
class DelayedResponseHandler(BaseHTTPRequestHandler):
862868
protocol_version = "HTTP/1.1"
@@ -867,7 +873,6 @@ def do_GET(self):
867873
self.send_header("Content-Length", "0")
868874
self.end_headers()
869875

870-
app.config.linkcheck_timeout = 0.01
871876
with serve_application(app, DelayedResponseHandler):
872877
app.build()
873878

0 commit comments

Comments
 (0)