-
Notifications
You must be signed in to change notification settings - Fork 157
FIX: Ensure socket is closed when error is raised while opening socket #328
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -59,7 +59,8 @@ def initialize( | |||||
json_serializer: nil, | ||||||
custom_labels: nil, | ||||||
logger: Logger.new(STDERR), | ||||||
log_level: Logger::WARN | ||||||
log_level: Logger::WARN, | ||||||
process_queue_once_and_stop: false | ||||||
) | ||||||
@logger = logger | ||||||
@logger.level = log_level | ||||||
|
@@ -86,6 +87,7 @@ def initialize( | |||||
@json_serializer = json_serializer == :oj ? PrometheusExporter::OjCompat : JSON | ||||||
|
||||||
@custom_labels = custom_labels | ||||||
@process_queue_once_and_stop = process_queue_once_and_stop | ||||||
end | ||||||
|
||||||
def custom_labels=(custom_labels) | ||||||
|
@@ -144,7 +146,7 @@ def process_queue | |||||
@socket.write("\r\n") | ||||||
rescue => e | ||||||
logger.warn "Prometheus Exporter is dropping a message: #{e}" | ||||||
@socket = nil | ||||||
close_socket! | ||||||
raise | ||||||
end | ||||||
end | ||||||
|
@@ -170,6 +172,11 @@ def worker_loop | |||||
end | ||||||
|
||||||
def ensure_worker_thread! | ||||||
if @process_queue_once_and_stop | ||||||
worker_loop | ||||||
return | ||||||
end | ||||||
|
||||||
unless @worker_thread&.alive? | ||||||
@mutex.synchronize do | ||||||
return if @worker_thread&.alive? | ||||||
|
@@ -234,8 +241,7 @@ def ensure_socket! | |||||
|
||||||
nil | ||||||
rescue StandardError | ||||||
@socket = nil | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously, we were just assigning the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need an explicit ensure close_socket! in worker_loop? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No the worker loop is meant to ensure that a socket is opened and reuse the socket across loops. Is there a reason you think we should close the socket each time we process a message? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not so much for each time we process a message, but I'm tracing the flow right now and I do see other places e.g. process_queue, where there's prometheus_exporter/lib/prometheus_exporter/client.rb Lines 146 to 147 in cf7bf84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch and I've made the change to call |
||||||
@socket_started = nil | ||||||
close_socket! | ||||||
@socket_pid = nil | ||||||
raise | ||||||
end | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module PrometheusExporter | ||
VERSION = "2.1.1" | ||
VERSION = "2.2.0" | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: