[K9VULN-2502] Decouple timeout watchdog from JsRuntime
#596
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What problem are you trying to solve?
We will be adding the ability to gracefully recover from a JavaScript execution that causes v8 to run out of memory.
To do this, we need to provide a callback (NearHeapLimitCallback) to v8, which is a fundamentally different model from the condvar/mutex we use for our timeout watchdog.
We should thus decouple the timeout logic from the
JsRuntime
so that we can handle the increased complexity in an encapsulated way. (But in my opinion, this would be a good refactor irrespective--it's a design smell that our scoped_execute function needs to manually deal with internal implementation details like notifying condvars).What is your solution?
Remove all timeout logic from the ddsa
JsRuntime
and introduce a new, more generic "ResourceWatchdog". This will eventually contain the out-of-memory v8 callback implementation.Note
There is no change to runtime behavior or thread synchronization behavior
This PR is just a structural refactor
Notable Implementation Details
Watchdog state now stores termination reason
We need to distinguish between a termination from the v8 callback (out of memory) and our thread (timeout). This requires a mutex to both perform the termination and signal the reason for termination. While channels are probably a better way to encapsulate that communication, I'd like to keep using a Mutex to take advantage of Condvar's
wait_timeout
api.We now need to make sure that this
WatchdogState
is manually cleared after each execution. This is extensively tested with all possible state transition permutations here.Caller is no longer concerned with implementation details
Calls needing resource limitation just need make a call through the watchdog. Simple:
(See
execute
function signature here)Alternatives considered
What the reviewer should know
spawn_timeout_thread
is a copy/paste of the prior implementation with no behavior change.