Skip to content

gh-150452: use PyMutex in socket module#150453

Open
KowalskiThomas wants to merge 3 commits into
python:mainfrom
KowalskiThomas:kowalski/refactor-use-pymutex-in-socket-module
Open

gh-150452: use PyMutex in socket module#150453
KowalskiThomas wants to merge 3 commits into
python:mainfrom
KowalskiThomas:kowalski/refactor-use-pymutex-in-socket-module

Conversation

@KowalskiThomas
Copy link
Copy Markdown
Contributor

@KowalskiThomas KowalskiThomas commented May 26, 2026

@kumaraditya303
Copy link
Copy Markdown
Contributor

The change is not user-facing, news isn't needed. This PR alone is sufficient, you can close the other one and I'll backport this.

Comment thread Modules/socketmodule.c Outdated
Comment thread Modules/socketmodule.c
#endif
#else /* not HAVE_GETHOSTBYNAME_R */
#ifdef USE_GETHOSTBYNAME_LOCK
PyThread_acquire_lock(netdb_lock, 1);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, PyThread_acquire_lock() is implemented with _PyMutex_LockTimed(lock, -1, _Py_LOCK_DONT_DETACH), whereas PyMutex_Lock() uses _PY_LOCK_DETACH. Is it ok to "Detach/release the GIL while waiting on the lock"? I think so, but I'm not sure. I'm not a PyMutex expert :-)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just had a look and I would say so given both call sites are within a Py_BEGIN_ALLOW_THREADS / Py_END_ALLOW_THREADS block, so as far as I know we already aren't running with the GIL held.

Is that right or am I missing something? I'm clearly not a PyMutex expert either.

Co-authored-by: Victor Stinner <vstinner@python.org>
@KowalskiThomas KowalskiThomas marked this pull request as ready for review May 29, 2026 15:53
@KowalskiThomas KowalskiThomas requested a review from vstinner May 29, 2026 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

socket module should use PyMutex instead of Lock

3 participants