This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: os.path.ismount() always returns false for mount --bind on same filesystem
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Alex Richman, Mikko Korkalo, Oliver Smith, R0b0t1, cheryl.sabella, christian.heimes, eryksun, miss-islington, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2017-03-03 07:09 by Oliver Smith, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 11238 merged serhiy.storchaka, 2018-12-19 13:15
PR 11560 merged miss-islington, 2019-01-15 08:56
PR 11560 merged miss-islington, 2019-01-15 08:56
PR 11560 merged miss-islington, 2019-01-15 08:56
Messages (12)
msg288863 - (view) Author: ollieparanoid (Oliver Smith) Date: 2017-03-03 07:09
After mounting a folder to another folder on the same filesystem with mount --bind, os.path.ismount() still returns False on the destination folder (although there is a mountpoint).

A shell script to reproduce this is below.

(Maybe this can be fixed by using /proc/mounts (if available, may not be the case eg. for chroots) for verifying, if the destination folder is really a mountpoint on POSIX/Linux. Although I am not sure how consistent that is through POSIX.)


---
#!/bin/sh
# Output:
# contents of /tmp/destination (should have test.py -> obviously mounted):
# test.py
# os.path.ismount(): False

# create source and destination folders
source=/tmp/source
destination=/tmp/destination
mkdir -p $source $destination

# add the python script in the source folder
echo "import os.path" >> $source/test.py
echo "print('os.path.ismount(): ' + str(os.path.ismount('$destination')))" >> $source/test.py

# do the mount --bind
sudo mount --bind $source $destination
echo "contents of $destination (should have test.py -> obviously mounted):"
ls $destination

# show the python bug
python3 $source/test.py

# clean up
sudo umount $destination
rm $source/test.py
rm -d $source $destination
msg301053 - (view) Author: Alex Richman (Alex Richman) Date: 2017-08-31 17:30
Can confirm, ran into this issue.  It's because os.path.ismount() works by checking if the path's parent is on a different device (e.g. st_dev is the same for 'path/' and 'path/..'), which obviously it is for a bind mount on the same filesystem.

It is actually documented that this is how it works (https://docs.python.org/2/library/os.path.html#os.path.ismount) but it's more of a passing comment than a warning, and who reads the docs for such an apparently simple function anyway? ;)

Agree that it should be fixed by parsing /proc/mounts instead of the current mess, perhaps using getmntent(3) and friends.
msg331908 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2018-12-15 22:05
It's also inconsistent. ismount() is true for a bind mount to the parent directory (e.g. dir/mount -> dir).
msg331921 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-12-16 09:03
I do not think that we should use /proc/mounts or getmntent ,because they are not portable. os.path.ismount() uses the traditional way to detect mountpoints which is not able to detect bind mounts.

But what is the problem with getting False for bind mounts on the same filesystem?
msg331922 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2018-12-16 09:51
> what is the problem with getting False for bind mounts 
> on the same filesystem?

Probably there's no problem if it's consistently false for all bind mounts on the same file system, but ismount() is true for a bind mount to the parent directory on the same file system, since the inodes match.
msg332137 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-12-19 13:19
I am not sure that we need to change ismount(), but its behavior should be documented.
msg333652 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-01-15 08:55
New changeset 32ebd8508d4807a7c85d2ed8e9c3b44ecd6de591 by Serhiy Storchaka in branch 'master':
bpo-29707: Document that os.path.ismount() is not able to reliable detect bind mounts. (GH-11238)
https://github.com/python/cpython/commit/32ebd8508d4807a7c85d2ed8e9c3b44ecd6de591
msg333653 - (view) Author: miss-islington (miss-islington) Date: 2019-01-15 09:01
New changeset a4aade2cf82dfa889c2bdad9fa0aa874f43c0bf8 by Miss Islington (bot) in branch '3.7':
bpo-29707: Document that os.path.ismount() is not able to reliable detect bind mounts. (GH-11238)
https://github.com/python/cpython/commit/a4aade2cf82dfa889c2bdad9fa0aa874f43c0bf8
msg339751 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2019-04-09 13:53
Can this be closed as a documentation only change?  Thanks!
msg339752 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2019-04-09 14:02
Yes, it can be closed as doc-only fix.
msg361367 - (view) Author: Mikko Korkalo (Mikko Korkalo) Date: 2020-02-04 20:36
I disagree about whether this should be fixed or not.
It's definitely a bug. If you ask whether a bind mount destination is a mount, it should return true.

I wrote a logic that does bind mounting. The logic cannot use ismount() because it does not work for me, it would keep remounting the same bind mount.

Not fixing this just causes people to write even less portable hackery on their own. I have to parse /proc/mounts or something manually, which obviously should be the job of ismount().
Python has a lot of platform-specific underlying implementations anyway.
msg392763 - (view) Author: R0b0t1 (R0b0t1) Date: 2021-05-03 04:01
https://bugs.python.org/issue29707#msg331921
> But what is the problem with getting False for bind mounts on the same filesystem?

When doing directory traversal it is important to not duplicate listings. It seems this can cause duplication. I'm replying to echo Mikko Korkalo's sentiment, this really should not have been closed.

This should likely be addressed along with https://bugs.python.org/issue23407.
History
Date User Action Args
2022-04-11 14:58:43adminsetgithub: 73893
2021-05-03 04:01:51R0b0t1setnosy: + R0b0t1
messages: + msg392763
2020-02-04 20:36:01Mikko Korkalosetnosy: + Mikko Korkalo
messages: + msg361367
2019-04-09 14:02:55christian.heimessetstatus: open -> closed

nosy: + christian.heimes
messages: + msg339752

resolution: fixed
stage: patch review -> resolved
2019-04-09 13:53:06cheryl.sabellasetnosy: + cheryl.sabella
messages: + msg339751
2019-01-15 09:01:18miss-islingtonsetnosy: + miss-islington
messages: + msg333653
2019-01-15 08:56:21miss-islingtonsetpull_requests: + pull_request11201
2019-01-15 08:56:13miss-islingtonsetpull_requests: + pull_request11200
2019-01-15 08:56:04miss-islingtonsetpull_requests: + pull_request11199
2019-01-15 08:55:45serhiy.storchakasetmessages: + msg333652
2018-12-19 13:19:16serhiy.storchakasetmessages: + msg332137
2018-12-19 13:15:41serhiy.storchakasetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request10470
2018-12-16 09:51:48eryksunsetmessages: + msg331922
2018-12-16 09:03:22serhiy.storchakasetmessages: + msg331921
2018-12-16 01:12:56eryksunsetnosy: + serhiy.storchaka
2018-12-15 22:05:31eryksunsetmessages: + msg331908
2018-12-15 21:17:11eryksunsetnosy: + eryksun
stage: needs patch

versions: + Python 3.8
2017-08-31 17:30:18Alex Richmansetnosy: + Alex Richman
messages: + msg301053
2017-03-03 07:09:34Oliver Smithcreate