python / python/cpython

Make `os.scandir` retry on system calls failing with EINTR

Open
#130,209 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

extension-modules type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

In a long-running script, I hit:

[…]
  File "/Users/gsnedders/ct.py", line 104, in worker
    with os.scandir(path) as it:
         ~~~~~~~~~~^^^^^^
InterruptedError: [Errno 4] Interrupted system call: '/Users/gsnedders/Library/Containers/com.apple.ScreenTimeWidgetApplication.ScreenTimeWidgetExtension/Data/Library/Application Scripts'

No repro case is provided, because that would require making syscalls fail with EINTR reliably somehow.

PEP 475 (Retry system calls failing with EINTR) implies to me that os.scandir should be retrying, rather than just failing here.

It does appear that os.scandir doesn't always retry, looking at the source:

https://github.com/python/cpython/blob/ae3064608935367c860182dc1b364631082ecdff/Modules/posixmodule.c#L16489

The iterator itself also seems to not retry:

https://github.com/python/cpython/blob/ae3064608935367c860182dc1b364631082ecdff/Modules/posixmodule.c#L16290

CPython versions tested on:

3.13

Operating systems tested on:

macOS

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read PEP 475 and the linked os.scandir implementation in Modules/posixmodule.c, including the iterator code at the referenced lines. Check how system calls returning EINTR are handled and verify that os.scandir retries instead of raising InterruptedError. Done means both the initial call and iterator behavior match the intended retry policy.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
operating-systems
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.