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

Synchronize access to @client_threads in LogStash::Outputs::Tcp #1270

Closed

Conversation

lmars
Copy link

@lmars lmars commented Apr 15, 2014

Logstash was consistently failing with the following error when a client connected to a TCP output:

NoMethodError: undefined method `write' for nil:NilClass
       register at /opt/logstash/lib/logstash/outputs/tcp.rb:92
           each at org/jruby/RubyArray.java:1613
       register at /opt/logstash/lib/logstash/outputs/tcp.rb:91
           call at org/jruby/RubyProc.java:271
         encode at /opt/logstash/lib/logstash/codecs/json.rb:45
        receive at /opt/logstash/lib/logstash/outputs/tcp.rb:143
         handle at /opt/logstash/lib/logstash/outputs/base.rb:86
     initialize at (eval):21
           call at org/jruby/RubyProc.java:271
         output at /opt/logstash/lib/logstash/pipeline.rb:266
   outputworker at /opt/logstash/lib/logstash/pipeline.rb:225
  start_outputs at /opt/logstash/lib/logstash/pipeline.rb:152

Logstash at the time was receiving a heavy stream of events, so this is likely the result of a race condition accessing @client_threads which is not thread safe, so this change synchronizes access to it across threads.

@colinsurprenant colinsurprenant added this to the 1.4.3 milestone Jul 11, 2014
@colinsurprenant colinsurprenant self-assigned this Jul 11, 2014
@colinsurprenant
Copy link
Contributor

yes, makes sense. see inline comments.

@@ -75,6 +75,7 @@ def register
@logger.info("Starting tcp output listener", :address => "#{@host}:#{@port}")
@server_socket = TCPServer.new(@host, @port)
@client_threads = []
@client_mutex = Mutex.new
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer @client_threads_lock to be consistent with the tcp input plugin which does something similar

Copy link
Author

Choose a reason for hiding this comment

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

variable name changed: e99e803

end
end
end
@client_threads_lock.synchronize do
Copy link
Contributor

Choose a reason for hiding this comment

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

to avoid any possible race condition, the whole Thread.start should be wrapped into a synchronize block. Without this, there is a possibility of the client thread finishing faster thus trying to @client_threads.delete(Thread.current) before client_thread was added into @client_threads.

Copy link
Author

Choose a reason for hiding this comment

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

sure: 0544cec

@colinsurprenant
Copy link
Contributor

LGTM

Did you test it in your environment? adding specs for this would be really great!

@lmars
Copy link
Author

lmars commented Jul 15, 2014

@colinsurprenant I don't really have the time right now to add a spec for this, but I have been using my original patch for a couple of months in a multi region environment (~20 servers) and it's working fine.

@colinsurprenant
Copy link
Contributor

Ok, I'll add an issue for this. Do you think you can test this patch revision in your environment too?

@lmars
Copy link
Author

lmars commented Jul 15, 2014

@colinsurprenant sure I'll re-patch later today and let you know.

@colinsurprenant
Copy link
Contributor

@lmars great! Thanks!

@jordansissel jordansissel modified the milestones: v1.5.0, 1.4.3 Nov 11, 2014
@suyograo
Copy link
Contributor

@lmars any update on the testing?

@lmars
Copy link
Author

lmars commented Nov 11, 2014

@suyograo I have been using the latest patch for a few months without issue

@jsvd
Copy link
Member

jsvd commented Nov 21, 2014

@lmars since we're moving the plugins to separate repositories, would you mind reopening this PR in https://github.com/logstash-plugins/logstash-output-tcp and rebasing against the current master?

@lmars
Copy link
Author

lmars commented Nov 21, 2014

@lmars lmars closed this Nov 21, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants