Skip to content

Commit

Permalink
Merge pull request #2773 from DataDog/add-processor-run-mutex
Browse files Browse the repository at this point in the history
Protect Processor::Context#run with a mutex
  • Loading branch information
lloeki authored Apr 13, 2023
2 parents 7fc111a + e0c9ba1 commit ef4ab52
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/datadog/appsec/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,28 @@ def initialize(processor)
@time_ext_ns = 0.0
@timeouts = 0
@events = []
@run_mutex = Mutex.new
end

def run(input, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT)
@run_mutex.lock

start_ns = Core::Utils::Time.get_time(:nanosecond)

# this WAF::Context#run call is not thread safe as it mutates the context
# TODO: remove multiple assignment
_code, res = _ = @context.run(input, timeout)
# @type var res: WAF::Result
_code, res = @context.run(input, timeout)

stop_ns = Core::Utils::Time.get_time(:nanosecond)

# these updates are not thread safe and should be protected
@time_ns += res.total_runtime
@time_ext_ns += (stop_ns - start_ns)
@timeouts += 1 if res.timeout

res
ensure
@run_mutex.unlock
end

def finalize
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/appsec/processor.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module Datadog

@context: WAF::Context

@run_mutex: ::Thread::Mutex

def initialize: (Processor processor) -> void
def run: (data input, ?::Integer timeout) -> WAF::Result
def finalize: () -> void
Expand Down
2 changes: 1 addition & 1 deletion vendor/rbs/libddwaf/0/datadog/appsec/waf.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ module Datadog
def initialize: (Handle handle) -> void
def finalize: () -> void

def run: (data input, ?::Integer timeout) -> ::Array[top]
def run: (data input, ?::Integer timeout) -> [::Symbol, Result]

private

Expand Down

0 comments on commit ef4ab52

Please sign in to comment.