-
Notifications
You must be signed in to change notification settings - Fork 14
feat: add with_timeout() function #200
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
Draft
jay7x
wants to merge
1
commit into
main
Choose a base branch
from
with_timeout
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+50
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,50 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'timeout' | ||
|
|
||
| # Control the specified block execution timeout. | ||
| # | ||
| # If code in the block specified is executing longer than timeout speficied, | ||
| # then execution is cancelled and 'bolt/execution-expired' kind of an Error is raised. | ||
| # | ||
| # > **Note:** Not available in apply block | ||
| Puppet::Functions.create_function(:with_timeout) do | ||
| # @param timeout Timeout in seconds (0 disables timeout). | ||
| # @param block The block to control execution timeout for. | ||
| # @return [Any] The block return value. | ||
| # @example Raise error if the block execution takes longer that 1 minute | ||
| # $result = with_timeout(60) || { | ||
| # run_task('deploy', $target) | ||
| # } | ||
| # out::verbose('Deploy is not timed out') | ||
| # @example Ensure the block execution takes no longer that 1 minute | ||
| # $result = catch_errors(['bolt/execution-expired']) || { | ||
| # with_timeout(60) || { | ||
| # run_task('deploy', $target) | ||
| # } | ||
| # } | ||
| # if $result =~ Error { | ||
| # fail_plan("Deploy task timed out", 'deploy/timed-out') | ||
| # } else { | ||
| # out::verbose('Deploy is not timed out') | ||
| # } | ||
| dispatch :with_timeout do | ||
| param 'Integer[0]', :timeout | ||
| block_param 'Callable[0, 0]', :block | ||
| end | ||
|
|
||
| def with_timeout(timeout, &) | ||
| unless Puppet[:tasks] | ||
| raise Puppet::ParseErrorWithIssue | ||
| .from_issue_and_stack(Bolt::PAL::Issues::PLAN_OPERATION_NOT_SUPPORTED_WHEN_COMPILING, | ||
| action: 'with_timeout') | ||
| end | ||
|
|
||
| # Send Analytics Report | ||
| Puppet.lookup(:bolt_executor).report_function_call(self.class.name) | ||
|
|
||
| Timeout.timeout(timeout, &) | ||
| rescue Timeout::Error | ||
| raise Bolt::Error.new('Execution expired', 'bolt/execution-expired', details: { timeout: timeout }) | ||
| end | ||
| end | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
We perhaps shouldn't use
Timeout.timeout. It's considered dangerous. It works by raisingTimeout::ErrorviaThread#raise, which can interrupt execution at any point, including inside ensure blocks, mutex operations, etc. IfTimeout::Errorfires inside arun_taskcall's internalensureblock (like while it's closing an SSH connection), the cleanup code gets interrupted and the reraisedBolt::Errormasks the real problem.In order to make this safe, you'd either need to guard a bunch of stuff in the internals of OpenBolt, or perhaps you could do this:
But I'm not 100% confident this doesn't have edge cases and problems of its own.