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

in_forward: Fixed a problem where connections are not closed on fluentd's shutdown. #861

Closed
Closed
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
7 changes: 6 additions & 1 deletion lib/fluent/plugin/in_forward.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def start

@lsock = listen
@loop.attach(@lsock)
@connections = []

@usock = SocketUtil.create_udp_socket(@bind)
@usock.bind(@bind, @port)
Expand All @@ -82,11 +83,15 @@ def shutdown
@usock.close
@thread.join
@lsock.close
Copy link
Author

Choose a reason for hiding this comment

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

By the way, I have another question about #shutdown. Should watchers be detached after {@usock,@lsock}.close in #shutdown? Otherwise, requests sent from clients between @loop.watchers.each { |w| w.detach if w.attached? } and @lsock.close would be lost.

@connections.each { |c| c.close }
end

def listen
log.info "listening fluent socket on #{@bind}:#{@port}"
s = Coolio::TCPServer.new(@bind, @port, Handler, @linger_timeout, log, method(:on_message))
s = Coolio::TCPServer.new(@bind, @port, Handler, @linger_timeout, log, method(:on_message)) do |connection|
@connections.reject! { |conn| conn.closed? }
Copy link
Member

Choose a reason for hiding this comment

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

Should this block guard using Mutex?

Copy link
Author

Choose a reason for hiding this comment

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

I think it doesn't need to be guarded.
(I would like to confirm just in case; does this block mean do |connection| ... end, not { |conn| conn.closed? }, right?)

@connections is used for closing accepted connections at shutdown, so already closed connections don't need to be included in. I think it is safe that several processes or threads come in this block at the same time.

Copy link
Member

Choose a reason for hiding this comment

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

It looks needed for me too to guard @connections.reject! using Mutex.
I mean that:

@mutex.synchronize do
  @connections.reject!{ |conn| conn.closed? }
  @connections << connection
end

Connection callback may run twice immediately if many connection arrived at a time.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for your comments.

Surely, I think I should guard it by mutex, but please let me ask you a question.
Can this callback be run simultaneously by threads?
It seems that only one thread(@thread) handles waiting connections, communicating with accepted connections and replying heartbeat request. So only one thread can run this callback at a time, cannot it?

@connections << connection
end
s.listen(@backlog) unless @backlog.nil?
s
end
Expand Down