python / python/cpython

Signal handler sometimes not called

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

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

interpreter-core type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
36k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Bug report

Below is a program repoducing the issue. Two Python processes P (as Parent) and CH (as child) are exchanging SIGUSR1 signals. One process waits for a signal, the other one sends it. Then they switch the roles and the signal si sent the other way. This is repeated in a loop. After few seconds the loop halts, usually before reaching 1000 iterations (on my computer). I believe that a signal was delivered to the process, but the corresponding handler was not called.

import fcntl, os, subprocess, signal, sys, time

label: str      # either P (parent) or CH (child)

def selfpipe(signo: int) -> int:
    rfd, wfd = os.pipe()
    wflags = fcntl.fcntl(wfd, fcntl.F_GETFL)
    fcntl.fcntl(wfd, fcntl.F_SETFL, wflags | os.O_NONBLOCK)

    msg = f"{label} handler\n".encode('ascii')
    def signal_handler(*_):
        # write() is "async-signal-safe"
        os.write(1, msg)
        os.write(wfd, b"\x00")

    signal.signal(signo, signal_handler)
    return rfd

def main():
    global label

    is_child = sys.argv[1:2] == ["CH"]
    label = "CH:" if is_child else "P:"
    rfd = selfpipe(signal.SIGUSR1)
    if is_child:
        other = os.getppid()
    else:
        child = subprocess.Popen(["python3", sys.argv[0], "CH"])
        other = child.pid

    if is_child:
        # parent starts with wait, child starts with signal
        os.kill(other, signal.SIGUSR1)
    i = 0
    while True:
        print(f"{label} wait")
        os.read(rfd, 1)
        print(f"{label} signal {i}")
        os.kill(other, signal.SIGUSR1)
        time.sleep(0.02)    # reduce CPU load
        i += 1

if __name__ == "__main__":
    main()

Your environment

Linux x86 PC, tested on Python 3.7.14, 3.8.14 and 3.10.7

Notes

  • I made a C program exchanging signals the same way and it does not halt

  • It is known that if a signal is pending (delivered, but not processed yet), sending another signal of the same type has no effect. That explains "lost" signals in some situations. Tthis cannot be such case, because the next signal comes only after the current signal has been acted upon.

  • Despite both processes executing the same loop, the test always hangs with the last signal sent from P to CH, not in the other direction.

CH: handler
CH: wait
CH: signal 521
P: handler
P: wait
P: signal 522  <-- always P (never CH) in my tests
CH: wait
P: wait   <-- last line before halt
  • When terminating the test with ctrl-C, the signal handler gets called! That proves the signal was delivered.
^CCH: handler  <--- note the message from the signal handler
Traceback (most recent call last):
  File "/tmp/pp/sigdemo.py", line 44, in <module>
Traceback (most recent call last):
  File "/tmp/pp/sigdemo.py", line 44, in <module>
    main()
  File "/tmp/pp/sigdemo.py", line 37, in main
    main()
  File "/tmp/pp/sigdemo.py", line 37, in main
    os.read(rfd, 1)
KeyboardInterrupt
    os.read(rfd, 1)
KeyboardInterrupt

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

Chạy reproducer được cung cấp thông qua các điểm vào selfpipe() và main() của nó trên các phiên bản Python và môi trường Linux đã mô tả, sau đó so sánh nó với chương trình C được đề cập trong các ghi chú. Truy vết việc xử lý SIGUSR1 xung quanh os.read(), os.kill() và trình xử lý tín hiệu; hoàn thành khi tiến trình cha và tiến trình con tiếp tục trao đổi tín hiệu thay vì dừng 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ó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
42/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.