bpo-25920: Remove socket.getaddrinfo() lock on macOS by vstinner · Pull Request #20177 · python/cpython (original) (raw)
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Conversation15 Commits1 Checks0 Files changed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
[ Show hidden characters]({{ revealButtonHref }})
On macOS, socket.getaddrinfo() no longer uses an internal lock to
prevenet race conditions when calling getaddrinfo(). getaddrinfo is
thread-safe is macOS 10.5, whereas Python 3.9 requires macOS 10.6 or
newer.
The lock was also used on FreeBSD older than 5.3, OpenBSD older than
201311 and NetBSD older than 4.
https://bugs.python.org/issue25920
This change also impacts socket.getnameinfo() and setipaddr() helper which is used by getsockaddrarg() to parse a "socket address": sock.bind, sock.connect, sock.connect_ex, sock.sendto, sock.sendmsg. I'm not sure if it's worth it to list all impacted methods.
With this change, socketmodule.c still uses netdb_lock on platforms which don't provide gethostbyname_r():
#if !defined(HAVE_GETHOSTBYNAME_R) && !defined(MS_WINDOWS)
# define USE_GETHOSTBYNAME_LOCK
#endif
#else |
---|
#define ACQUIRE_GETADDRINFO_LOCK |
#define RELEASE_GETADDRINFO_LOCK |
# include <sys/param.h> |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I notice we use #if defined(__OpenBSD__)
above this include, apparently that's not a problem. We no longer use __FreeBSD_version
or __NetBSD_Version__
, so I suspect the comment and include can be deleted now too?
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea. That would be unrelated to this PR, so open a separated issue/PR if you want to update a comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is part of this PR, since the code deleted in this PR was the last code that used the preprocessor defines from sys/param.h. This comment and include were correct before, but now that lines 203+ are deleted, this comment and include should also be deleted.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh ok, now I understood your comment, ok. It seems like you are right, so I also removed the include.
PR rebased to amend the commit messaged to a fix a typo (see previous review comments).
I tested manually my PR on FreeBSD: it works as expected, I can still build Python and test_socket pass.
But I had to revert commit 0da5466: it's a regression, see bpo-26317.
@ronaldoussoren @ned-deily: I plan to merge this PR into master (but not backport it to 3.9) at the end of the week. Do you plan to review it?
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Patch looks good to me, other than a pedantic note for the news entry.
As an aside (not to be addressed in the PR): Apparently gethostbyname() and related functions are thread-safe on macOS. This is according to the manpage on macOS 10.15. I haven't checked in which version that changed. This allows avoiding the use of the gethostbyname lock as well.
@@ -0,0 +1,5 @@ |
---|
On macOS, :func:`socket.getaddrinfo` no longer uses an internal lock to |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The patch also removes the lock for FreeBSD, OpenBSD en NetBSD. Also note that the lock is only used when Python is build for an old enough version of these operating systems. In particular, the removed code was already not used in the installers for macOS on Python.org.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the NEWS entry to mention that python.org installer wasn't affected, and mention NetBSD, OpenBSD and FreeBSD. Does the updated NEWS entry look better?
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On macOS, socket.getaddrinfo() no longer uses an internal lock to prevent race conditions when calling getaddrinfo(). getaddrinfo is thread-safe is macOS 10.5, whereas Python 3.9 requires macOS 10.6 or newer.
The lock was also used on FreeBSD older than 5.3, OpenBSD older than 201311 and NetBSD older than 4.
As an aside (not to be addressed in the PR): Apparently gethostbyname() and related functions are thread-safe on macOS. This is according to the manpage on macOS 10.15. I haven't checked in which version that changed. This allows avoiding the use of the gethostbyname lock as well.
I chose to restrict my PR to the socket.getaddrinfo() lock on purpose:
#20177 (comment)
Would you mind to open a separated issue for gethostbyname()?
vstinner deleted the remove_getaddrinfo_lock branch
CuriousLearner added a commit to CuriousLearner/cpython that referenced this pull request
- 'master' of github.com:python/cpython: (497 commits) bpo-40061: Fix a possible refleak in _asynciomodule.c (pythonGH-19748) bpo-40798: Generate a different message for already removed elements (pythonGH-20483) closes bpo-29017: Update the bindings for Qt information with PySide2 (pythonGH-20149) bpo-39885: Make IDLE context menu cut and copy work again (pythonGH-18951) bpo-29882: Add an efficient popcount method for integers (python#771) Further de-linting of zoneinfo module (python#20499) bpo-40780: Fix failure of _Py_dg_dtoa to remove trailing zeros (pythonGH-20435) Indicate that abs() method accept argument that implement abs(), just like call() method in the docs (pythonGH-20509) bpo-39040: Fix parsing of email mime headers with whitespace between encoded-words. (pythongh-17620) bpo-40784: Fix sqlite3 deterministic test (pythonGH-20448) bpo-30064: Properly skip unstable loop.sock_connect() racing test (pythonGH-20494) Note the output ordering of combinatoric functions (pythonGH-19732) bpo-40474: Updated coverage.yml to better report coverage stats (python#19851) bpo-40806: Clarify that itertools.product immediately consumes its inpt (pythonGH-20492) bpo-1294959: Try to clarify the meaning of platlibdir (pythonGH-20332) bpo-37878: PyThreadState_DeleteCurrent() was not removed (pythonGH-20489) bpo-40777: Initialize PyDateTime_IsoCalendarDateType.tp_base at run-time (pythonGH-20493) bpo-40755: Add missing multiset operations to Counter() (pythonGH-20339) bpo-25920: Remove socket.getaddrinfo() lock on macOS (pythonGH-20177) bpo-40275: Fix test.support.threading_helper (pythonGH-20488) ...