Skip to content

subprocess.run gets in an infinite loop closing every possible file descriptor #127177

Closed
@edre

Description

@edre

Bug report

Bug description:

Under some weird circumstances, calling subprocess.run causes the child process to be locked in a loop closing all possible file descriptors. Perusing the code this is very likely some misfiring of _close_open_fds_safe. Paging @gpshead

The weird circumstances:

  • In a chroot, so the /proc/ filesystem is inaccessible.
  • Running in docker. Maybe affects the RLIMIT_NOFILE check?
  • Using musl? I reproduced this on an alpine container but not a debian one.

Reproduction setup:

== Dockerfile ==
FROM alpine:3.20
RUN apk add python3 rust strace
# Just make some static binary.
RUN echo 'int main(){}' > main.c
RUN mkdir /sb && gcc -static -o /sb/main main.c 
COPY sandbox.py .
CMD ["strace", "-f", "python3", "sandbox.py"]

== sandbox.py ==
import os, subprocess
os.chdir("/sb")
os.chroot("/sb")
subprocess.run("/main")

$ docker build . --tag sandbox:test && docker run sandbox:test 2>&1 | less
...
[pid    10] open("/proc/self/fd", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid    10] prlimit64(0, RLIMIT_NOFILE, NULL, {rlim_cur=1073741816, rlim_max=1073741816}) = 0
[pid    10] prlimit64(0, RLIMIT_NOFILE, NULL, {rlim_cur=1073741816, rlim_max=1073741816}) = 0
[pid    10] close(3)                    = -1 EBADF (Bad file descriptor)
[pid    10] close(5)                    = -1 EBADF (Bad file descriptor)
[pid    10] close(6)                    = -1 EBADF (Bad file descriptor)
[pid    10] close(7)                    = -1 EBADF (Bad file descriptor)
[pid    10] close(8)                    = -1 EBADF (Bad file descriptor)
[pid    10] close(9)                    = -1 EBADF (Bad file descriptor)
[pid    10] close(10)                   = -1 EBADF (Bad file descriptor)
[pid    10] close(11)                   = -1 EBADF (Bad file descriptor)
[pid    10] close(12)                   = -1 EBADF (Bad file descriptor)
...

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions