fix(inputs.phpfpm): Check error before continue processing (#15176)

This commit is contained in:
Pierre Fersing 2024-04-17 21:58:24 +02:00 committed by GitHub
parent 96e7b2b7e7
commit 31b2b505c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 6 deletions

View File

@ -29,7 +29,10 @@ func newFcgiClient(timeout time.Duration, h string, args ...interface{}) (*conn,
laddr := net.UnixAddr{Name: args[0].(string), Net: h}
con, err = net.DialUnix(h, nil, &laddr)
default:
err = errors.New("fcgi: we only accept int (port) or string (socket) params")
return nil, errors.New("fcgi: we only accept int (port) or string (socket) params")
}
if err != nil {
return nil, err
}
if timeout != 0 {
@ -38,11 +41,7 @@ func newFcgiClient(timeout time.Duration, h string, args ...interface{}) (*conn,
}
}
fcgi := &conn{
rwc: con,
}
return fcgi, err
return &conn{rwc: con}, nil
}
func (c *conn) Request(env map[string]string, requestData string) (retout []byte, reterr []byte, err error) {

View File

@ -187,6 +187,35 @@ func TestPhpFpmTimeout_From_Fcgi(t *testing.T) {
require.GreaterOrEqual(t, time.Since(start), timeout)
}
// TestPhpFpmCrashWithTimeout_From_Fcgi show issue #15175: when timeout is enabled
// and nothing is listenning on specified port, a nil pointer was dereferenced.
func TestPhpFpmCrashWithTimeout_From_Fcgi(t *testing.T) {
tcp, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err, "Cannot initialize test server")
tcpAddress := tcp.Addr().String()
// Yes close the tcp port now. The listenner is only used to find a free
// port and then make it free. This test hope that nothing will re-use the
// port in meantime.
tcp.Close()
const timeout = 200 * time.Millisecond
//Now we tested again above server
r := &phpfpm{
Urls: []string{"fcgi://" + tcpAddress + "/status"},
Timeout: config.Duration(timeout),
Log: &testutil.Logger{},
}
require.NoError(t, r.Init())
var acc testutil.Accumulator
require.Error(t, acc.GatherError(r.Gather))
require.Empty(t, acc.GetTelegrafMetrics())
}
func TestPhpFpmGeneratesMetrics_From_Socket(t *testing.T) {
// Create a socket in /tmp because we always have write permission and if the
// removing of socket fail when system restart /tmp is clear so