你能解释一下 python 中的以下信号处理行为吗?

发布于 2024-10-05 02:50:08 字数 952 浏览 4 评论 0原文

我有以下程序:

import socket
import sys
import threading
import signal

class serve(threading.Thread):
    def __init__(self):
        super(serve, self).__init__()
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.host = ''
        self.port = int(sys.argv[1])
    def run(self):
            self.s.bind((self.host, self.port))
            self.s.listen(1)
            conn, addr = self.s.accept()
            # Call blocks in the following recv
            data = conn.recv(1000000)
            conn.close()
            self.s.close()

def handler(signum, frame):
    print "I am the handler: "

signal.signal(signal.SIGHUP, handler)

background = serve()
background.start()
background.join()

有一个客户端程序连接到此程序但不发送任何数据。问题是当发送 SIGHUP 时,会引发“中断系统调用”异常。知道为什么吗?它发生在 python 2.6+ 和 FreeBSD 上。我怀疑它与 http://bugs.python.org/issue1975 有关。

I have the following program:

import socket
import sys
import threading
import signal

class serve(threading.Thread):
    def __init__(self):
        super(serve, self).__init__()
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.host = ''
        self.port = int(sys.argv[1])
    def run(self):
            self.s.bind((self.host, self.port))
            self.s.listen(1)
            conn, addr = self.s.accept()
            # Call blocks in the following recv
            data = conn.recv(1000000)
            conn.close()
            self.s.close()

def handler(signum, frame):
    print "I am the handler: "

signal.signal(signal.SIGHUP, handler)

background = serve()
background.start()
background.join()

There is a client program that connects to this but does not send any data. The problem is when a SIGHUP is sent, and "Interrupted System call" exception is being thrown. Any idea why? It is happening in python 2.6+ and on FreeBSD. I suspect it is related to http://bugs.python.org/issue1975.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水微香 2024-10-12 02:50:08

如果信号到达时正在执行系统调用,则系统调用为 中断。我相信这部分是为了防止升级攻击,部分是为了在调用信号处理程序时保持进程处于一致状态。它还允许您唤醒挂在系统调用上的进程。

要在处理信号后重新启动系统调用,请使用 signal.siginterrupt< /code>设置信号处理程序后:

signal.signal(signal.SIGHUP, handler)
signal.siginterrupt(signal.SIGHUP, false)

If a system call is executing when a signal arrives, the system call is interrupted. I believe this is in part to prevent escalation attacks and in part to keep the process in a consistent state when the signal handler is invoked. It also would allow you to wake up a process that's hung on a system call.

To instead restart system calls after a signal is handled, use signal.siginterrupt after you set a signal handler:

signal.signal(signal.SIGHUP, handler)
signal.siginterrupt(signal.SIGHUP, false)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文