GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — mmap.mmap() should not necessarily clone the file descriptor · Issue #78502 · python/cpython

mmap.mmap() should not necessarily clone the file descriptor #78502

New issue
Copy link
New issue
Copy link
Closed
Closed
mmap.mmap() should not necessarily clone the file descriptor#78502
Copy link
Assignees
serhiy-storchaka
Labels
3.13only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

manuels
mannequin
opened on Aug 2, 2018
Issue body actions
BPO 34321
Nosy @tiran, @MojoVampire, @ZackerySpytz, @manuels
PRs
  • gh-78502: Add a trackfd parameter to mmap.mmap() #25425
  • 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 = None
    closed_at = None
    created_at = <Date 2018-08-02.13:22:03.631>
    labels = ['type-feature', 'library', '3.10']
    title = 'mmap.mmap() should not necessarily clone the file descriptor'
    updated_at = <Date 2021-04-15.16:45:08.950>
    user = 'https://github.com/manuels'

    bugs.python.org fields:

    activity = <Date 2021-04-15.16:45:08.950>
    actor = 'ZackerySpytz'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2018-08-02.13:22:03.631>
    creator = 'manuels'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 34321
    keywords = ['patch']
    message_count = 3.0
    messages = ['322953', '323069', '386013']
    nosy_count = 4.0
    nosy_names = ['christian.heimes', 'josh.r', 'ZackerySpytz', 'manuels']
    pr_nums = ['25425']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue34321'
    versions = ['Python 3.10']

    Linked PRs

    • gh-78502: Add a trackfd parameter to mmap.mmap() on Windows #138238

    Activity

    Next
    1. manuels commented on Aug 2, 2018

      @manuels
      manuelsmannequin
      on Aug 2, 2018
      MannequinAuthor
      More actions

      mmap.mmap(fileno, length, flags, prot, access, offset) always clones the file descriptor that should be used [1].

      The cloning of the file descriptor seems to be done to ensure that the file cannot be closed behind mmap's back, but if you are mmap()'ing a lot of memory regions of a file this can cause a 'Too many open files' error.

      I would suggest to add an option to mmap.mmap() that tells it not to clone the file descriptor. This can cause an issue if the file is closed before accessing the mmapped region, so this fact should also be pointed out in the documentation.

      [1] https://github.com/python/cpython/blob/master/Modules/mmapmodule.c#L1159

    2. manuels
      added
      3.7 (EOL)end of lifeend of life
      3.8 (EOL)end of lifeend of life
      stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancementA feature request or enhancement
      on Aug 2, 2018
    3. MojoVampire commented on Aug 3, 2018

      @MojoVampire
      MojoVampiremannequin
      on Aug 3, 2018
      Mannequin
      More actions

      Why would it "cause an issue if the file is closed before accessing the mmapped region"? As shown in your own link, the constructor performs the mmap call immediately after the descriptor is duplicated, with the GIL held; any race condition that could close the file before the mmap occurs could equally well close it before the descriptor is duplicated.

      The possible issues aren't tied to accessing the memory (once the mapping has been performed, the file descriptor can be safely closed in general), but rather, to the size and resize methods of mmap objects (the former using the fd to fstat the file, the latter using it to ftruncate the file). As long as you don't use size/resize, nothing else depends on the file descriptor after construction has completed. The size method in particular seems like a strange wart on the API; it returns the total file size, not the size of the mapping (len(mapping) gets the size of the actual mapping).

    4. tiran
      added
      3.10 (EOL)end of lifeend of life
      and removed
      3.7 (EOL)end of lifeend of life
      3.8 (EOL)end of lifeend of life
      on Jan 30, 2021
    5. tiran commented on Jan 31, 2021

      @tiran
      tiran
      on Jan 31, 2021
      Member
      More actions

      This issue came up in another discussion. I have given it some thought. mmap.mmap() dups the FD because its close() and __exit__() methods close(2) the fd. The size() and resize() methods use the fd to determine the size of the underlying file or to resize the file.

      The easiest way to solve the issue while avoind footguns, is an option to not track the fd at all, e.g. "trackfd" with default "True". mmap(..., trackfd=False) would neither dup the fd nor store the fd in its internal struct. In untracked fd case, size() and resize() would no longer work. That's totally fine for mappings without PROT_WRITE.

      It's safe to close the fd after mmap call, see https://man7.org/linux/man-pages/man2/mmap.2.html

      After the mmap() call has returned, the file descriptor, fd, can
      be closed immediately without invalidating the mapping.

    6. ezio-melotti
      transferred this issue fromon Apr 10, 2022
    7. erlend-aasland
      added
      3.13only security fixesonly security fixes
      and removed
      3.10 (EOL)end of lifeend of life
      on Jan 5, 2024
    8. encukou
      added a commit that references this issue on Jan 16, 2024

      gh-78502: Add a trackfd parameter to mmap.mmap() (GH-25425)

      8fd287b
    9. encukou commented on Jan 16, 2024

      @encukou
      encukou
      on Jan 16, 2024
      Member
      More actions

      Thank you manuels for the report, Chrisitian for the proposal, Zackery for the initial PR, and all the reviewers for the polish!

    10. 1 remaining item

    11. encukou commented on Jan 16, 2024

      @encukou
      encukou
      on Jan 16, 2024
      Member
      More actions

      Oh, I suppose I shouldn't close this yet: it would be good to add it for Windows too.
      But Windows code is separate, and I don't have a Windows box set up yet.

    12. encukou
      reopened this on Jan 16, 2024
    13. kulikjak
      added a commit that references this issue on Jan 22, 2024

      pythongh-78502: Add a trackfd parameter to mmap.mmap() (pythonGH-25425)

      fcd786b
    14. aisk
      added a commit that references this issue on Feb 11, 2024

      pythongh-78502: Add a trackfd parameter to mmap.mmap() (pythonGH-25425)

      7024b8d
    15. Glyphack
      added a commit that references this issue on Sep 2, 2024

      pythongh-78502: Add a trackfd parameter to mmap.mmap() (pythonGH-25425)

      94a648d
    16. gmgunter
      added 2 commits that reference this issue on Sep 13, 2024

      Replace memmap usage with direct file I/O

      dfb2cb6

      Replace memmap usage with direct file I/O (#82)

      621cfe5
    17. serhiy-storchaka
      added a commit that references this issue on Aug 29, 2025

      WIP: pythongh-78502: Add support of trackfd in mmap() on Windows

      dc787bc
    18. serhiy-storchaka
      self-assigned this
      on Aug 29, 2025
    19. serhiy-storchaka
      added a commit that references this issue on Aug 29, 2025

      pythongh-78502: AAdd a trackfd parameter to mmap.mmap() on Windows

      d1ce256
    20. serhiy-storchaka commented on Aug 29, 2025

      @serhiy-storchaka
      serhiy-storchaka
      on Aug 29, 2025
      Member
      More actions

      #138238 adds it for Windows.

    21. serhiy-storchaka
      added 2 commits that reference this issue on Aug 29, 2025

      pythongh-78502: AAdd a trackfd parameter to mmap.mmap() on Windows

      558beef

      gh-78502: Add a trackfd parameter to mmap.mmap() on Windows (GH-138238)

      7274d07
    22. serhiy-storchaka
      closed this as completedon Sep 3, 2025
    23. lkollar
      added a commit that references this issue on Sep 9, 2025

      pythongh-78502: Add a trackfd parameter to mmap.mmap() on Windows (py…

      1a3e61d
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    • @serhiy-storchaka
      serhiy-storchaka

    Labels

    3.13only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.