Skip to content

Conversation

@yoney
Copy link
Contributor

@yoney yoney commented Sep 22, 2025

These changes make the mmap module thread-safe for FT-Python by protecting the mmap_object against race conditions and undefined behavior. The goal is to provide behavior similar to standard Python builds with the GIL enabled, rather than making mmap fully deterministic or generally recommended for multithreaded use.

cc: @mpage @colesbury @Yhg1s

@yoney yoney marked this pull request as ready for review September 22, 2025 15:01
Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

I think that it would be helpful in the long run to switch mmap to AC rather than manually adding lock_held functions everywhere.

@corona10
Copy link
Member

Same question from my side:
Why don t we switch to use AC with '@critical_section'?

@yoney
Copy link
Contributor Author

yoney commented Sep 24, 2025

Same question from my side: Why don t we switch to use AC with '@critical_section'?

@ZeroIntensity, @corona10: Thank you both for the reviews, I agree that using AC with @critical_section would be a better.

I wanted to ask for your preference: would you like to implement the switch in this PR, or should I hold off on this one and do the implementation separately, then continue with this PR after that? Alternatively, the switch could be done as a follow-up PR.

Thanks!

@ZeroIntensity
Copy link
Member

Let's do AC in this PR.

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM. I reviewed mmapmodule.c, but I didn't review Lib/test/test_free_threading/test_mmap.py.

@kumaraditya303 kumaraditya303 added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Oct 8, 2025
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @kumaraditya303 for commit 9d6b95d 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F139237%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Oct 8, 2025
@kumaraditya303 kumaraditya303 merged commit 7f155f9 into python:main Oct 9, 2025
117 checks passed
@kumaraditya303 kumaraditya303 added the needs backport to 3.14 bugs and security fixes label Oct 9, 2025
@miss-islington-app
Copy link

Thanks @yoney for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Sorry, @yoney and @kumaraditya303, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 7f155f9c46e2dd7dec5e12ebec3323dfd95fe1f0 3.14

kumaraditya303 pushed a commit to kumaraditya303/cpython that referenced this pull request Oct 9, 2025
@bedevere-app
Copy link

bedevere-app bot commented Oct 9, 2025

GH-139825 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Oct 9, 2025
@bedevere-bot
Copy link

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 FreeBSD Refleaks 3.x (tier-3) has failed when building commit 7f155f9.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/1613/builds/2130) and take a look at the build logs.
  4. Check if the failure is related to this commit (7f155f9) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/1613/builds/2130

Test leaking resources:

  • test_events: references
  • test_events: memory blocks

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/buildbot/buildarea/3.x.ware-freebsd.refleak/build/Lib/test/support/__init__.py", line 847, in gc_collect
    gc.collect()
    ~~~~~~~~~~^^
ResourceWarning: unclosed <socket.socket fd=9, family=2, type=1, proto=6, laddr=('127.0.0.1', 38818), raddr=('127.0.0.1', 38819)>
Task was destroyed but it is pending!
task: <Task pending name='Task-3140' coro=<BaseSelectorEventLoop._accept_connection2() done, defined at /buildbot/buildarea/3.x.ware-freebsd.refleak/build/Lib/asyncio/selector_events.py:217> wait_for=<Future pending cb=[Task.task_wakeup()]>>
Warning -- Unraisable exception
Exception ignored while calling deallocator <function _SelectorTransport.__del__ at 0x837dda750>:
Traceback (most recent call last):
  File "/buildbot/buildarea/3.x.ware-freebsd.refleak/build/Lib/asyncio/selector_events.py", line 873, in __del__
    _warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
    ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ResourceWarning: unclosed transport <_SelectorSocketTransport closing fd=9>
k


Traceback (most recent call last):
  File "/buildbot/buildarea/3.x.ware-freebsd.refleak/build/Lib/test/support/__init__.py", line 847, in gc_collect
    gc.collect()
    ~~~~~~~~~~^^
ResourceWarning: unclosed <socket.socket fd=9, family=2, type=1, proto=6, laddr=('127.0.0.1', 55024), raddr=('127.0.0.1', 55025)>
Task was destroyed but it is pending!
task: <Task pending name='Task-1740' coro=<BaseSelectorEventLoop._accept_connection2() done, defined at /buildbot/buildarea/3.x.ware-freebsd.refleak/build/Lib/asyncio/selector_events.py:217> wait_for=<Future pending cb=[Task.task_wakeup()]>>
Warning -- Unraisable exception
Exception ignored while calling deallocator <function _SelectorTransport.__del__ at 0x841981250>:
Traceback (most recent call last):
  File "/buildbot/buildarea/3.x.ware-freebsd.refleak/build/Lib/asyncio/selector_events.py", line 873, in __del__
    _warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
    ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ResourceWarning: unclosed transport <_SelectorSocketTransport closing fd=9>
k

w-hsiung pushed a commit to w-hsiung/cpython that referenced this pull request Oct 9, 2025
…) (python#139825)

* [3.14] pythongh-116738: make `mmap` module thread-safe (pythonGH-139237)
(cherry picked from commit 7f155f9)

Co-authored-by: Alper <[email protected]>
@yoney yoney deleted the ft_mmap branch October 19, 2025 03:02
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.

6 participants