How can I trigger ignore_user_abort? How does the server know that this socket cannot be used? Old Wang and diogin said whether the server needs to actively interact with the socket to determine whether the socket can be used?
In addition, we also found that php provides the connection_status and connection_aborted methods, both of which can detect the current connection status. So the line of code for logging can be changed:
According to the manual connection processing, we can print the current connection status.
There is still a program to interact with the socket. We use echo and remember to include flush later to eliminate the impact of flush.
RST
We use wireshark to capture packets and check the interaction process between the client and the server.
In this process, only 14 packets are sent. we can see that when the server sends 22222 for the first time, the client returns RST. No subsequent packet request is made.
As a result, the interaction process between the client and the server is:
When the server sends 2222 for the first time in a loop, the client returns an RST because the connection has been disconnected, but the request is successful. When the server tries to write data to the socket for the second time, the socket will not be transmitted over the network, and the connection status is abort. So the above situation occurs. for the first time, the status of 222 is 0, and for the second time, abort appears.
Verify strace
We can also use strace php-s xxx for verification.
The strace log is as follows:
。。。close(5) = 0lstat("/tmp/test.log", {st_mode=S_IFREG|0644, st_size=49873651, ...}) = 0open("/tmp/test.log", O_WRONLY|O_CREAT|O_APPEND, 0666) = 5fstat(5, {st_mode=S_IFREG|0644, st_size=49873651, ...}) = 0lseek(5, 0, SEEK_CUR) = 0lseek(5, 0, SEEK_CUR) = 0flock(5, LOCK_EX) = 0write(5, "1 connection status: 0abort:0\n", 30) = 30close(5) = 0sendto(4, "HTTP/1.0 200 OK\r\nConnection: clo"..., 89, 0, NULL, 0) = 89sendto(4, "111111111", 9, 0, NULL, 0) = 9rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0nanosleep({3, 0}, 0x7fff60a40290) = 0sendto(4, "22222", 5, 0, NULL, 0) = 5open("/tmp/test.log", O_WRONLY|O_CREAT|O_APPEND, 0666) = 5fstat(5, {st_mode=S_IFREG|0644, st_size=49873681, ...}) = 0lseek(5, 0, SEEK_CUR) = 0lseek(5, 0, SEEK_CUR) = 0flock(5, LOCK_EX) = 0write(5, "2 connection status: 0abort:0\n", 30) = 30close(5) = 0rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0nanosleep({1, 0}, 0x7fff60a40290) = 0sendto(4, "22222", 5, 0, NULL, 0) = -1 EPIPE (Broken pipe)--- SIGPIPE {si_signo=SIGPIPE, si_code=SI_USER, si_pid=2819, si_uid=0} ---open("/tmp/test.log", O_WRONLY|O_CREAT|O_APPEND, 0666) = 5fstat(5, {st_mode=S_IFREG|0644, st_size=49873711, ...}) = 0lseek(5, 0, SEEK_CUR) = 0lseek(5, 0, SEEK_CUR) = 0flock(5, LOCK_EX) = 0write(5, "2 connection status: 1abort:1\n", 30) = 30close(5) = 0rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0nanosleep({1, 0}, 0x7fff60a40290) = 0open("/tmp/test.log", O_WRONLY|O_CREAT|O_APPEND, 0666) = 5fstat(5, {st_mode=S_IFREG|0644, st_size=49873741, ...}) = 0lseek(5, 0, SEEK_CUR) = 0lseek(5, 0, SEEK_CUR) = 0flock(5, LOCK_EX) = 0write(5, "2 connection status: 1abort:1\n", 30) = 30close(5) 。。。
We can see where status changes from 0 to 1.
...sendto(4, "22222", 5, 0, NULL, 0) = 5...write(5, "2 connection status: 0abort:0\n", 30) = 30close(5) = 0rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0nanosleep({1, 0}, 0x7fff60a40290) = 0sendto(4, "22222", 5, 0, NULL, 0) = -1 EPIPE (Broken pipe)--- SIGPIPE {si_signo=SIGPIPE, si_code=SI_USER, si_pid=2819, si_uid=0} ---open("/tmp/test.log", O_WRONLY|O_CREAT|O_APPEND, 0666) = 5fstat(5, {st_mode=S_IFREG|0644, st_size=49873711, ...}) = 0lseek(5, 0, SEEK_CUR) = 0lseek(5, 0, SEEK_CUR) = 0flock(5, LOCK_EX) = 0write(5, "2 connection status: 1abort:1\n", 30) = 30close(5) = 0
The Broken pipe is displayed when 2222 is sent to the socket for the second time. This is what the program tells us that this socket can no longer be used. By the way, connection_status in php will be set to 1. Subsequent write operations will not be executed.
Summary
Under normal circumstances, if the client exception is introduced, the program on the server will continue to be executed until two interactions with IO are performed. The server finds that the client has been disconnected, and a user_abort will be triggered at this time. if ignore_user_abort is not set, the php-fpm program will be interrupted.
Now, the problem is fixed.