-
Notifications
You must be signed in to change notification settings - Fork 380
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 Processor::Context#run with a mutex #2773
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 |
---|---|---|
|
@@ -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 | ||
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. Just a final question -- does 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. Nope, that one is fine. Context initialization and finalization happen on the same thread and should outlive any transient thread that would theoretically use the same context (you'd get much bigger problems otherwise, that a simple synchronize would not solve). It's all theoretical anyway because no code is currently allowing getting the context from another thread, so this is mostly bulletproofing following a discussion around |
||
|
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.
Minor: Consider using
@run_mutex.synchronize
-- I think that one's a bit easier to maintain and "break" with future changes.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.
Rationale:
defer
;)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.
Added a comment to clarify the intent!
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.
Thanks!