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

Protect buffer operations in force_flush #1181

Closed
wants to merge 2 commits into from

Conversation

repeatedly
Copy link
Member

force_flush is called from another thread, so accessing buffer should be guarded.

See: #1129

force_flush is called from another thread, so accessing buffer
should be guarded.
@@ -291,7 +291,8 @@ def format_stream(tag, es)
#end

def enqueue_buffer(force = false)
@buffer.keys.each {|key|
buf_keys = @buffer.synchronize { @buffer.keys }
Copy link
Member

Choose a reason for hiding this comment

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

Do @buffer.keys.dup in critical sections.
Your code iterates the object in @buffer itself, and doesn't protect anything in synchronize.

Copy link
Member Author

Choose a reason for hiding this comment

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

@buffer.keys returns an array from internal @map hash keys.
What does '@buffer itself' mean?

Copy link
Member

Choose a reason for hiding this comment

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

I tested on pry:

hash = {a: 1, b: 2, c: 3, d: 4}
=> {:a=>1, :b=>2, :c=>3, :d=>4}
t = Thread.new{ i = 0; hash.each{|k,v| p(k: v); sleep(5) if i == 1; i += 1 } }; sleep 1; hash[:e] = 5; p hash; sleep 3; p hash
{:k=>1}
{:k=>2}
RuntimeError: can't add a new key into hash during iteration
from (pry):12:in `__pry__'
{:k=>3}
{:k=>4}

Copy link
Member

Choose a reason for hiding this comment

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

Ah, no, it's not what i want to test...

Copy link
Member

Choose a reason for hiding this comment

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

I confirmed that you're right. Sorry to bother you.

@repeatedly repeatedly closed this May 23, 2017
@repeatedly repeatedly deleted the guard-buffer-in-force_flush branch March 9, 2018 23:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants