发送SIGTERM时,子进程会关闭mysql连接吗? [英] When SIGTERM is sent, does the child process close mysql connection?

查看:142
本文介绍了发送SIGTERM时,子进程会关闭mysql连接吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请想象一些子进程是由pcntl_fork()生成的,它们分别使用PDO打开mysql连接. 如果父进程向子进程发送带有posix_kill()的SIGTERM信号,则子进程将立即被杀死.

因此,我对此有疑问. 当子进程终止时,它也会自动关闭mysql连接吗? 还是应该调用exit()函数?

我知道,如果发送了SIGKILL,则子进程会保持连接. 但是,我不知道SIGTERM的情况.

解决方案

当进程完成时(由于退出或使用信号终止),操作系统将自动关闭其保持打开状态的所有文件和连接.通过使用MySQL协议关闭连接(假设有一个),它不是完全干净的.它很简单地放在TCP/IP级别,而另一端的服务器只是发现它正在与一扇关门对话.这并非总是立即发生的,有时要花一些时间,直到服务器通知讨论伙伴已消失.发生这种情况时,它会认为该连接已断开,并从一边清理所有东西.

请勿在使用fork()之前在父进程中打开MySQL连接. fork()复制用于管理连接本地端的数据结构,结果是不可预测的.甚至,当子进程完成后(无论如何),它都会关闭连接(或操作系统将其断开),MySQL服务器也会关闭其末端,而父进程发现它正在与任何人交谈.

在使用fork()之前,先在父进程中关闭MySQL连接,然后根据需要在父进程和子进程中打开单独的连接.

另外,在与父服务器之间的所有MySQL通信之间进行包装:

pcntl_sigprocmask(SIG_BLOCK, array(SIGCHLD));

pcntl_sigprocmask(SIG_UNBLOCK, array(SIGCHLD));

否则,当子进程完成时,将使用SIGCHLD信号通知父进程.收到的信号使它从休眠状态恢复(如果在信号到达时恰巧在sleep()调用中停止了该信号). MySQL库使用sleep()作为MySQL协议的一部分与服务器进行通信.如果这样的sleep()强行返回了比应有的时间早(由于接收到信号),则MySQL库会感到困惑,并且最终会报告实际上不正确的奇怪错误(例如"MySQL服务器已消失").

请查看此答案以获取详细说明.

Please imagine that some child processes are generated by pcntl_fork() and they open mysql connection using PDO respectively. If parent process send a SIGTERM signal with posix_kill() to a child process, the child process will be killed immediately.

So, I have a question about this. When the child process is terminated,will it also close the mysql connection automatically? Or should I call exit() function?

I know the child process keep the connection if SIGKILL is sent. However,I don't know the case of SIGTERM.

解决方案

When a process completes (either because it exits or it is terminated using a signal) all the files and connections it keeps open are automatically closed by the operating system. It is not closed clean, by using the MySQL protocol for closing connections (assuming there is one). It is simple dropped at the TCP/IP level and the server at the other side just discovers it is talking to a closed door. That doesn't always happen instantly, sometimes it takes some time until the server notices the discussion partner is gone. When this happens, it considers the connection as being dropped and cleans the things up on its side.

Do not open the MySQL connection in the parent process before using fork(). fork() duplicates the data structures used to manage the local side of the connection and the results are unpredictable. Even more, when the child completes (no matter how), it closes the connection (or the OS drops it), the MySQL server also closes its end and the parent process discovers it is talking to nobody.

Close the MySQL connection in the parent process before using fork() then open separate connections in the parent and in the child process, as needed.

Also, wrap any MySQL communication with the server in the parent process between:

pcntl_sigprocmask(SIG_BLOCK, array(SIGCHLD));

and

pcntl_sigprocmask(SIG_UNBLOCK, array(SIGCHLD));

Otherwise, when a child process completes, the parent process is notified with the SIGCHLD signal. A received signal resumes it from sleeping (if it happened to be stopped in a sleep() call when the signal arrives). The MySQL library uses sleep() as part of the MySQL protocol for communication with the server. If such a sleep() forcibly returns earlier than it should (because of a received signal), the MySQL library gets confused and it ends up reporting strange errors (like "MySQL server is gone away") that are actually not correct.

Take a look at this answer for a detailed explanation.

这篇关于发送SIGTERM时,子进程会关闭mysql连接吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆