Skip to content
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 and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rglob crashes if scandir does not handle exception #957

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sMezaOrellana
Copy link

@sMezaOrellana sMezaOrellana commented Nov 26, 2024

If an exception occurres inside the scandir function implementation of a FilesystemEntry class, rglob crashes

Looking at the pathlib implementation of rglob it calls glob and at somepoint scandir() is called.

for path in self.fs.path("/").rglob("*"): # exception occures here
	# stuff

suppose for whatever reason scandir fails (i.e. a malformed inode, mft record, etc) should this loop stop?

Another way would be to use rglob not inside a for loop but using the next() method and checking for exceptions this way something akin to:

reader = read()
while True:
    try:
        item = next(reader)
    except StopIteration:
        break
    except Exception as e:
        log error
        continue
    do_stuff(item)

https://stackoverflow.com/questions/11366064/handle-an-exception-thrown-in-a-generator

this seems a bit ugly tho.

This pr ensures that scandir will handle the exception and will return an empty generator allowing rglob to continue. Ideally some logging should be here as aswell as a warning.

This is also a design change, as whether rglob should continue or not is a design choice. So just let me know if this is the expected behaviour of rglob

@twiggler
Copy link
Contributor

twiggler commented Nov 27, 2024

Hi @sMezaOrellana , thank you very much for your contribution!

One point of concern with the proposed solution is that it "absorbs" exceptions. There is no mechanism for the caller to specify how to deal with errors. Take https://docs.python.org/3/library/os.html#os.walk for example; here, the caller can optionally supply a callback which handles any errors which occur in scandir. Also note that this functionality in walk no longer works as intended if we absorb the exception in scandir.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants