Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic: don't send events if client is nil #15568

Merged
merged 6 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
*Affecting all Beats*

TLS or Beats that accept connections over TLS and validate client certificates. {pull}14146[14146]
- Fix panic in the Logstash output when trying to send events to closed connection. {pull}15568[15568]

*Auditbeat*

Expand Down
21 changes: 20 additions & 1 deletion libbeat/outputs/logstash/async.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
package logstash

import (
"errors"
"net"
"sync"
"time"

"github.com/elastic/beats/libbeat/beat"
Expand All @@ -37,6 +39,8 @@ type asyncClient struct {
win *window

connect func() error

mutex sync.Mutex
}

type msgRef struct {
Expand Down Expand Up @@ -113,7 +117,11 @@ func (c *asyncClient) Connect() error {
}

func (c *asyncClient) Close() error {
c.mutex.Lock()
defer c.mutex.Unlock()

logp.Debug("logstash", "close connection")

if c.client != nil {
err := c.client.Close()
c.client = nil
Expand Down Expand Up @@ -197,12 +205,23 @@ func (c *asyncClient) publishWindowed(
}

func (c *asyncClient) sendEvents(ref *msgRef, events []publisher.Event) error {
client := c.getClient()
if client == nil {
return errors.New("connection closed")
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Close method can be called from another thread. Better use an atomic pointer or mutex to load c.client into a local variable. The v2.AsyncClient should be thread safe (just uses net.Conn.Close() to close the underlying connection/FD).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

window := make([]interface{}, len(events))
for i := range events {
window[i] = &events[i].Content
}
ref.count.Inc()
return c.client.Send(ref.callback, window)
return client.Send(ref.callback, window)
}

func (c *asyncClient) getClient() *v2.AsyncClient {
c.mutex.Lock()
client := c.client
c.mutex.Unlock()
return client
}

func (r *msgRef) callback(seq uint32, err error) {
Expand Down
2 changes: 1 addition & 1 deletion testing/environments/snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ services:
- "indices.id_field_data.enabled=true"

logstash:
image: docker.elastic.co/logstash/logstash:8.0.0-SNAPSHOT
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The image is normally maintained by RE. @mikemadden42 please create a follow up PR restoring 8.0.0-SNAPSHOT. We can retrigger CI every now and then.

image: docker.elastic.co/logstash/logstash@sha256:e01cf165142edf8d67485115b938c94deeda66153e9516aa2ce69ee417c5fc33
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9600/_node/stats"]
retries: 600
Expand Down