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
{{ message }}
Repository navigation
urllib.request.urlopen leaks exceptions from socket and httplib.client #57945
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/orsenthil'closed_at=Nonecreated_at=<Date2012-01-08.06:22:19.699>labels= ['type-bug', 'library']
title='urllib.request.urlopen leaks exceptions from socket and httplib.client'updated_at=<Date2015-11-16.22:14:15.896>user='https://github.com/jmoy'
The documentation for urlopen says that it raises URLError on error.
But there exist error conditions such as a socket timeout or a bad HTTP status line under which the exception from the underlying library leaks through urllib, thus breaking the promise in the documentation.
I am attaching a test program that demonstrates this bug.
I think the general pattern of wrapping exceptions in other exceptions is rather unfortunate. It makes it harder to examine the original problem (for example the errno attribute) for no actual gain.
jmoy commented on Jan 20, 2012
jmoymannequin
MannequinAuthor
More actions
Antoine, could "raise ... from" be used here? Perhaps also using new subclasses of URLError to allow the exceptions to be caught with finer granularity. That way no information would be lost while at the same time not surprising clients who only catch URLError based on the documentation.
At least one exception which I feel must be wrapped is socket.timeout. Since we allow a timeout argument to urlopen, it doesn't make sense for the fact of the timeout to be announced by an exception from another library.
That's a possible compromise indeed. It's up to Senthil to decide
anyway.
Perhaps also using new subclasses of URLError to allow the exceptions
to be caught with finer granularity. That way no information would be
lost while at the same time not surprising clients who only catch
URLError based on the documentation.
I agree there is a problem with the documentation announcing that all
exceptions will be wrapped. Honestly I would like it better if that
guarantee were dropped. In the meantime I'm not sure what the best
course of action is.
At least one exception which I feel must be wrapped is socket.timeout.
Since we allow a timeout argument to urlopen, it doesn't make sense
for the fact of the timeout to be announced by an exception from
another library.
You still may be interested to know that the request failed because of a
timeout rather than (say) an authentication failure, no?
I have stumbled upon a wrong impression as well by reading the docs, but usually in the code, I tend to catch the specific Exceptions, like timeout instead or URLError when it is known. I saw some libraries following similar pattern too. But that could be changed, if promise in the docs that URLError exception is raised is corrected.
I think, the course of action for this bug could be.
raise ... from .. for the appropriate Exception in 3.3 and appropriate doc changes.
Doc changes in 2.7,3.2 which say indicate the possibility of other exceptions besides URLError.
Having any important information in the msg part of the Exception is a bad idea, because it is seldom relied upon and can be changed anytime.
There is another patch in duplicate bpo-8823, which has simpler exception wrapping code, but does not add wrapping of reply exceptions.
Personally, I wouldn’t endorse any new wrapping HTTP client exceptions, and even if wrapping more socket OS errors would be consistent, it could be a backwards compatibility problem. I think the best way forward is documentation: bpo-25633, bpo-22797.
Activity
jmoy commentedon Jan 8, 2012
The documentation for urlopen says that it raises URLError on error.
But there exist error conditions such as a socket timeout or a bad HTTP status line under which the exception from the underlying library leaks through urllib, thus breaking the promise in the documentation.
I am attaching a test program that demonstrates this bug.
jmoy commentedon Jan 8, 2012
A patch to fix this issue. Catches exceptions from underlying libraries and reraises them as URLError.
I put the class name of the underlying exception in the reason to make it more descriptive.
pitrou commentedon Jan 20, 2012
I think the general pattern of wrapping exceptions in other exceptions is rather unfortunate. It makes it harder to examine the original problem (for example the
errnoattribute) for no actual gain.jmoy commentedon Jan 20, 2012
Antoine, could "raise ... from" be used here? Perhaps also using new subclasses of URLError to allow the exceptions to be caught with finer granularity. That way no information would be lost while at the same time not surprising clients who only catch URLError based on the documentation.
At least one exception which I feel must be wrapped is socket.timeout. Since we allow a timeout argument to urlopen, it doesn't make sense for the fact of the timeout to be announced by an exception from another library.
pitrou commentedon Jan 20, 2012
That's a possible compromise indeed. It's up to Senthil to decide
anyway.
I agree there is a problem with the documentation announcing that all
exceptions will be wrapped. Honestly I would like it better if that
guarantee were dropped. In the meantime I'm not sure what the best
course of action is.
You still may be interested to know that the request failed because of a
timeout rather than (say) an authentication failure, no?
orsenthil commentedon Feb 8, 2012
I have stumbled upon a wrong impression as well by reading the docs, but usually in the code, I tend to catch the specific Exceptions, like timeout instead or URLError when it is known. I saw some libraries following similar pattern too. But that could be changed, if promise in the docs that URLError exception is raised is corrected.
I think, the course of action for this bug could be.
Having any important information in the msg part of the Exception is a bad idea, because it is seldom relied upon and can be changed anytime.
Shall come out with a patch.
vadmium commentedon Nov 16, 2015
There is another patch in duplicate bpo-8823, which has simpler exception wrapping code, but does not add wrapping of reply exceptions.
Personally, I wouldn’t endorse any new wrapping HTTP client exceptions, and even if wrapping more socket OS errors would be consistent, it could be a backwards compatibility problem. I think the best way forward is documentation: bpo-25633, bpo-22797.
orsenthil commentedon Oct 28, 2026
Addressing this as a docs issue, which seems most reasonable way to address this - #160830
gh-57945: Document exceptions not wrapped by urlopen() (#160830)
[3.14] gh-57945: Document exceptions not wrapped by urlopen() (GH-160830
[3.15] gh-57945: Document exceptions not wrapped by urlopen() (GH-160830