-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a thread-safe
Airbrake::Context
Fixes #658 (Is Airbrake.merge_context thread safe?) This makes sure any operations on the context hash are thread-safe. We do synchronize calls around the code that attaches the context object to the error report but the context object is not thread-safe itself.
- Loading branch information
Showing
7 changed files
with
128 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module Airbrake | ||
# Represents a thread-safe Airbrake context object, which carries arbitrary | ||
# information added via {Airbrake.merge_context} calls. | ||
# | ||
# @example | ||
# Airbrake::Context.current.merge!(foo: 'bar') | ||
# | ||
# @api private | ||
# @since v5.2.1 | ||
class Context | ||
# Returns current, thread-local, context. | ||
# @return [self] | ||
def self.current | ||
Thread.current[:airbrake_context] ||= new | ||
end | ||
|
||
def initialize | ||
@mutex = Mutex.new | ||
@context = {} | ||
end | ||
|
||
# Merges the given context with the current one. | ||
# | ||
# @param [Hash{Object=>Object}] other | ||
# @return [void] | ||
def merge!(other) | ||
@mutex.synchronize do | ||
@context.merge!(other) | ||
end | ||
end | ||
|
||
# @return [Hash] duplicated Hash context | ||
def to_h | ||
@mutex.synchronize do | ||
@context.dup | ||
end | ||
end | ||
|
||
# @return [Hash] clears (resets) the current context | ||
def clear | ||
@mutex.synchronize do | ||
@context.clear | ||
end | ||
end | ||
|
||
# @return [Boolean] checks whether the context has any data | ||
def empty? | ||
@context.empty? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
RSpec.describe Airbrake::Context do | ||
subject(:context) { described_class.current } | ||
|
||
before { described_class.current.clear } | ||
|
||
after { described_class.current.clear } | ||
|
||
describe "#merge!" do | ||
it "merges the given context with the current one" do | ||
context.merge!(apples: 'oranges') | ||
expect(context.to_h).to match(apples: 'oranges') | ||
end | ||
end | ||
|
||
describe "#clear" do | ||
it "clears the context" do | ||
context.merge!(apples: 'oranges') | ||
context.clear | ||
expect(context.to_h).to be_empty | ||
end | ||
end | ||
|
||
describe "#to_h" do | ||
it "returns a hash representation of the context" do | ||
expect(context.to_h).to be_a(Hash) | ||
end | ||
end | ||
|
||
describe "#empty?" do | ||
context "when the context has data" do | ||
it "returns true" do | ||
context.merge!(apples: 'oranges') | ||
expect(context).not_to be_empty | ||
end | ||
end | ||
|
||
context "when the context has NO data" do | ||
it "returns false" do | ||
expect(context).to be_empty | ||
end | ||
end | ||
end | ||
|
||
context "when another thread is spawned" do | ||
it "doesn't clash with other threads' contexts" do | ||
described_class.current.merge!(apples: 'oranges') | ||
th = Thread.new do | ||
described_class.current.merge!(foos: 'bars') | ||
end | ||
th.join | ||
expect(described_class.current.to_h).to match(apples: 'oranges') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters