Skip to content

Commit

Permalink
Found an issue with thread's not inheriting on_warning, made on_warni…
Browse files Browse the repository at this point in the history
…ng not thread safe to fix this
  • Loading branch information
sunblaze committed Mar 20, 2015
1 parent 1704356 commit 50564ae
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -65,9 +65,10 @@ end

## Thread-safe

This gem is tested to be thread safe with one known exception.
This gem is tested to be thread safe with a couple known exceptions.

`PedantMysql2.ignore` is not thread safe and should only be called during intialization of your app. Changing this within a thread while another is updating it could be problematic.
`PedantMysql2.ignore` is not thread safe and should only be called during initialization of your app. Changing this within a thread while another is updating it could be problematic.
`PedantMysql2.on_warning=` is not thread safe, this should also be called only during initialization.

If you find any other parts that are not thread-safe, please create an issue or PR.

Expand Down
14 changes: 9 additions & 5 deletions lib/pedant_mysql2.rb
Expand Up @@ -32,11 +32,11 @@ def warn(warning)
end

def on_warning
Thread.current[:__pedant_mysql2_on_warning]
Thread.current[:__pedant_mysql2_on_warning] || @_on_warning
end

def on_warning=(new_proc)
Thread.current[:__pedant_mysql2_on_warning] = new_proc
@_on_warning = new_proc
end

protected
Expand All @@ -51,19 +51,23 @@ def ignored?(warning)

def setup_capture
Thread.current[:__pedant_mysql2_warnings] = []
self.on_warning = lambda { |warning| Thread.current[:__pedant_mysql2_warnings] << warning }
self.thread_on_warning = lambda { |warning| Thread.current[:__pedant_mysql2_warnings] << warning }
end

def captured_warnings
Thread.current[:__pedant_mysql2_warnings]
end

def backup_warnings
[captured_warnings, on_warning]
[captured_warnings, Thread.current[:__pedant_mysql2_on_warning]]
end

def restore_warnings(warnings)
Thread.current[:__pedant_mysql2_warnings], self.on_warning = *warnings
Thread.current[:__pedant_mysql2_warnings], self.thread_on_warning = *warnings
end

def thread_on_warning=(new_proc)
Thread.current[:__pedant_mysql2_on_warning] = new_proc
end
end
end
11 changes: 11 additions & 0 deletions spec/adapter_spec.rb
Expand Up @@ -125,6 +125,17 @@ def method_missing(method_name,*args)
thread.join
end

it 'should inherit on_warning from parent thread' do
PedantMysql2.silence_warnings!
thread = Thread.new do
expect {
execute_with_warning
}.to_not raise_error
end

thread.join
end

describe MysqlWarning do

subject do
Expand Down

0 comments on commit 50564ae

Please sign in to comment.