Mercurial > hg > nginx-tests
view lib/Test/Nginx/Stream.pm @ 1752:ba6e24e38f03
Tests: improved stop_daemons() to send signal again.
As was observed, it's possible that a signal to complete a uwsgi daemon
can be ignored while it is starting up, which results in tests hang due
to eternal waiting on child processes termination. Notably, it is seen
when running tests with a high number of prove jobs on a low-profile VM
against nginx with broken modules and/or configuration. To reproduce:
$ TEST_NGINX_GLOBALS=ERROR prove -j16 uwsgi*.t
Inspecting uwsgi under ktrace on FreeBSD confirms that a SIGTERM signal
is ignored at the very beginning of uwsgi startup. It is then replaced
with a default action after listen(), thus waiting until uwsgi is ready
to accept new TCP connections doesn't completely solve the hang window.
The fix is to retry sending a signal some time after waitpid(WNOHANG)
continuously demonstrated no progress with reaping a signaled process.
It is modelled after f13ead27f89c that improved stop() for nginx.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Wed, 29 Dec 2021 22:29:23 +0300 |
parents | 29d0961bc3f7 |
children | 5d3aee48ed8e |
line wrap: on
line source
package Test::Nginx::Stream; # (C) Andrey Zelenkov # (C) Nginx, Inc. # Module for nginx stream tests. ############################################################################### use warnings; use strict; use base qw/ Exporter /; our @EXPORT_OK = qw/ stream dgram /; use Test::More qw//; use IO::Select; use IO::Socket; use Test::Nginx; sub stream { return Test::Nginx::Stream->new(@_); } sub dgram { unshift(@_, "PeerAddr") if @_ == 1; return Test::Nginx::Stream->new( Proto => "udp", @_ ); } sub new { my $self = {}; bless $self, shift @_; unshift(@_, "PeerAddr") if @_ == 1; $self->{_socket} = IO::Socket::INET->new( Proto => "tcp", PeerAddr => '127.0.0.1', @_ ) or die "Can't connect to nginx: $!\n"; if ({@_}->{'SSL'}) { require IO::Socket::SSL; IO::Socket::SSL->start_SSL($self->{_socket}, @_) or die $IO::Socket::SSL::SSL_ERROR . "\n"; } $self->{_socket}->autoflush(1); return $self; } sub write { my ($self, $message, %extra) = @_; my $s = $self->{_socket}; local $SIG{PIPE} = 'IGNORE'; $s->blocking(0); while (IO::Select->new($s)->can_write($extra{write_timeout} || 1.5)) { my $n = $s->syswrite($message); log_out(substr($message, 0, $n)); last unless $n; $message = substr($message, $n); last unless length $message; } if (length $message) { $s->close(); } } sub read { my ($self, %extra) = @_; my ($s, $buf); $s = $self->{_socket}; $s->blocking(0); if (IO::Select->new($s)->can_read($extra{read_timeout} || 8)) { $s->sysread($buf, 1024); }; log_in($buf); return $buf; } sub io { my $self = shift; my ($data, %extra) = @_; my $length = $extra{length}; my $read = $extra{read}; $read = 1 if !defined $read && $self->{_socket}->socktype() == &SOCK_DGRAM; $self->write($data, %extra); $data = ''; while (1) { last if defined $read && --$read < 0; my $buf = $self->read(%extra); last unless defined $buf and length($buf); $data .= $buf; last if defined $length && length($data) >= $length; } return $data; } sub sockaddr { my $self = shift; return $self->{_socket}->sockaddr(); } sub sockhost { my $self = shift; return $self->{_socket}->sockhost(); } sub sockport { my $self = shift; return $self->{_socket}->sockport(); } ############################################################################### 1; ###############################################################################