python / python/cpython

Python `io.open` intermittently throw `PermissionError` on windows

Đang mở
#136,965 13 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

OS-windows pending topic-IO type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Bug report

Bug description:

The io.open method intermittently raises a PermissionError on the Windows operating system. I've attached a Python script that reproduces this issue on version 3.12.8, and 3.11.9 (didn't test for any other version)

My investigation suggests that the problem arises from consecutive write operations performed within very short timeframes where each operation does fresh io.open to write updates -- For example, when attempting to write 10 updates back-to-back to a file with fresh io.open each time.

On Windows, explorer.exe runs in the background to update indexing and metadata upon file modification. If explorer.exe has already acquired a handle to the file, any new request to write to it will result in a PermissionError.

While we could implement a retry mechanism with backoff for io.open in our code, I'm curious why Python doesn't natively handle such background process contention.

import time
import threading
import random
import multiprocessing
import os

# Sample message text
MESSAGE = (
    r"file://C:\Users\sample\Desktop\temp\tmpgzy8f_lt\929cec70297d4938b31545f858d6b38c.txt,"
    r"gs://sample-bucket/loadwintest/hdahsdhahdahsdhahd/temp/dahsdhahdhadhahdh/dhahsdahdhahdhahdahdhahdha.txt,"
    r"2025-07-21T10:44:43.263442Z,2025-07-21T11:00:55.588213Z,dahshdahdhahdhahshah+hfQ==,25000000000,25000000000,OK,"
)

# Output file path
output_file = "winman/test_output11.txt"


# Writer process – gets messages from queue and writes to file
def writer(queue, end_time):
    while time.time() < end_time:
        msg = queue.get()
        with open(output_file, "at", encoding='utf-8', newline='\n') as f:
            f.write(msg + '\n')


# Thread that puts messages into the queue
def thread_worker(queue, thread_id, process_id, end_time):
    while time.time() < end_time:
        notify_msg = f"[P{process_id}-T{thread_id}] {MESSAGE}"
        queue.put(notify_msg)
        time.sleep(random.choice([0.3, 0.5, 0.4]))


# Process that spawns multiple threads
def process_worker(queue, num_threads, process_id, end_time):
    threads = []
    for tid in range(num_threads):
        t = threading.Thread(target=thread_worker, args=(queue, tid, process_id, end_time))
        threads.append(t)
        t.start()

    for t in threads:
        t.join()


def main():
    runtime_seconds = 600
    end_time = time.time() + runtime_seconds

    num_processes = 4
    threads_per_process = 4

    queue = multiprocessing.Queue()

    # Start writer process
    writer_proc = multiprocessing.Process(target=writer, args=(queue, end_time))
    writer_proc.start()

    # Start worker processes
    processes = []
    for pid in range(num_processes):
        p = multiprocessing.Process(target=process_worker, args=(queue, threads_per_process, pid, end_time))
        p.start()
        processes.append(p)

    # Wait for all worker processes
    for p in processes:
        p.join()

    # Terminate writer after work is done
    writer_proc.terminate()
    writer_proc.join()


if __name__ == "__main__":
    main()
CPython versions tested on:

3.12

Operating systems tested on:

Windows

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách chạy trình tái hiện Python đính kèm trên Windows với các phiên bản CPython được báo cáo và xác nhận liệu PermissionError không liên tục có thể được tái hiện hay không. Sau đó, kiểm tra điểm vào io.open và hành vi mở tệp liên quan trên Windows; công việc cần xác định được nguyên nhân có thể tái hiện và một bản sửa lỗi có phạm vi rõ ràng hoặc một giới hạn được ghi lại.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
operating-systems
Loại issue
Lỗi
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
20/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.