safe_timeout 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 784f00b2fc676fe36b98359e8eebdf9177f2e9dd
4
+ data.tar.gz: 7ffbf4ed29ad028e4b29b79d24bb9d9feb8970e2
5
+ SHA512:
6
+ metadata.gz: 13b8a264e3449535013dda188788fe3828d76426e342a9742623fec65f4f7bfc39dd62b2611199f3b9203417e45b16a00968f97706d53cf79c5362efd560c8a8
7
+ data.tar.gz: d0789e190f7ebcff48514fbae29670f8b73f87e74803d5e28508cb8e5cdcc88f938ad5327d362b45cec4ab754c461b973dff47e22892c7b0d6cd0fbbac357323
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 David McCullars
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # SafeTimeout
2
+
3
+ A safer alternative to Ruby's Timeout that uses unix processes instead of threads
4
+
5
+ [![Circle CI](https://circleci.com/gh/david-mccullars/safe_timeout/tree/master.svg?style=svg)](https://circleci.com/gh/david-mccullars/safe_timeout/tree/master)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'safe_timeout'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install safe_timeout
22
+
23
+ ## Usage
24
+
25
+ SafeTimeout is a drop-in replacement for Ruby's Timeout.
26
+
27
+ SafeTimeout.timeout(2) do
28
+ # Something that may take a while
29
+ # Possible Timeout::Error raised
30
+ end
31
+
32
+ or
33
+
34
+ SafeTimeout.timeout(10, CustomTimeoutError) do
35
+ # Something that may take a while
36
+ # Possible CustomTimeoutError raised
37
+ end
38
+
39
+ If one wishes this to be even more of a drop-in replacement one
40
+ could add the following to the top of an application:
41
+
42
+ Timeout = SafeTimeout
43
+
44
+ ## Contributing
45
+
46
+ 1. Fork it ( https://github.com/[my-github-username]/safe_timeout/fork )
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
49
+ 4. Push to the branch (`git push origin my-new-feature`)
50
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+ task :default => :spec
data/circle.yml ADDED
@@ -0,0 +1,12 @@
1
+ dependencies:
2
+ override:
3
+ - 'rvm-exec 1.8.7 bundle install'
4
+ - 'rvm-exec 1.9.3 bundle install'
5
+ - 'rvm-exec 2.1.2 bundle install'
6
+ - 'rvm-exec 2.2.0 bundle install'
7
+ test:
8
+ override:
9
+ - 'rvm-exec 1.8.7 bundle exec rake'
10
+ - 'rvm-exec 1.9.3 bundle exec rake'
11
+ - 'rvm-exec 2.1.2 bundle exec rake'
12
+ - 'rvm-exec 2.2.0 bundle exec rake'
@@ -0,0 +1,15 @@
1
+ # Ruby's Timeout is broken and highly dangerous. To avoid this risk we
2
+ # instead use a child process to handle the timeout. We fork it and let
3
+ # it issue a SIGINT (Ctrl-C) to the parent if the timeout is reached.
4
+ module SafeTimeout
5
+
6
+ autoload :InterruptingChildProcess, 'safe_timeout/interrupting_child_process'
7
+
8
+ def self.timeout(sec, klass=nil, &block)
9
+ InterruptingChildProcess.new(
10
+ :timeout => sec,
11
+ :on_timeout => lambda { |_| raise(klass || Timeout::Error) }
12
+ ).start(&block)
13
+ end
14
+
15
+ end
@@ -0,0 +1,40 @@
1
+ module SafeTimeout
2
+ class InterruptingChildProcess
3
+
4
+ def initialize(options={})
5
+ @expiration = Time.now.to_f + options.fetch(:timeout)
6
+ @on_timeout = options.fetch(:on_timeout)
7
+ end
8
+
9
+ def start(&block)
10
+ Signal.trap("TRAP", &@on_timeout)
11
+ Kernel.fork { wait_for_expiration }
12
+ yield
13
+ ensure
14
+ stop rescue nil
15
+ end
16
+
17
+ def stop
18
+ # Tell that child to stop interrupting!
19
+ Process.kill("HUP", @child_pid)
20
+ end
21
+
22
+ def wait_for_expiration
23
+ Signal.trap("HUP") { exit 0 }
24
+
25
+ # If the parent dies unexpectedly and the child is never told to
26
+ # stop, it becomes an orphan and is given to the init process (1)
27
+ # or worse yet it becomes a zombie with parent 0. In either case,
28
+ # stop interrupting!
29
+ while Process.ppid > 1
30
+ sleep 0.1
31
+ if Time.now.to_f > @expiration
32
+ Process.kill("TRAP", Process.ppid)
33
+ return
34
+ end
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,3 @@
1
+ module SafeTimeout
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'safe_timeout/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "safe_timeout"
8
+ spec.version = SafeTimeout::VERSION
9
+ spec.authors = ["David McCullars"]
10
+ spec.email = ["david.mccullars@gmail.com"]
11
+ spec.summary = %q{A safer alternative to Ruby's Timeout that uses unix processes instead of threads.}
12
+ spec.description = %q{A safer alternative to Ruby's Timeout that uses unix processes instead of threads.}
13
+ spec.homepage = "https://github.com/david-mccullars/safe_timeout"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.2"
24
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe SafeTimeout do
4
+ context '.timeout' do
5
+
6
+ it 'should process its block' do
7
+ result = nil
8
+ expect do
9
+ result = SafeTimeout.timeout(5) { :success }
10
+ end.to_not raise_error
11
+ expect(result).to eq(:success)
12
+ end
13
+
14
+ it 'should timeout if block takes too long' do
15
+ result = nil
16
+ expect do
17
+ result = SafeTimeout.timeout(0.4) { sleep 3 and :failure }
18
+ end.to raise_error(Timeout::Error)
19
+ expect(result).to be_nil
20
+ end
21
+
22
+ it 'should not leave orphaned child process' do
23
+ child = fork do
24
+ SafeTimeout.timeout(5) { sleep 5 }
25
+ end
26
+ :loop until grand_child = all_processes[child].first
27
+ expect(grand_child).to be > child
28
+
29
+ Process.kill('TERM', child)
30
+ expect(is_process_still_running? grand_child).to be false
31
+ end
32
+
33
+ private
34
+
35
+ # Returns a hash of all running processes and their children
36
+ def all_processes
37
+ `ps -eo pid,ppid`.lines.reduce(Hash.new []) do |hash, line|
38
+ pid, ppid = line.split.map(&:to_i)
39
+ hash[ppid] = [] unless hash.key?(ppid)
40
+ hash[ppid] << pid
41
+ hash
42
+ end
43
+ end
44
+
45
+ # Wait for up to 1 second for process to quit and report on
46
+ # whether it did or not.
47
+ def is_process_still_running?(pid)
48
+ 10.times do
49
+ sleep 0.1
50
+ begin
51
+ Process.kill(0, pid)
52
+ rescue Errno::ESRCH
53
+ return false
54
+ end
55
+ end
56
+ true
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,15 @@
1
+ require 'bundler'
2
+ Bundler.require
3
+
4
+ RSpec.configure do |config|
5
+
6
+ config.expect_with :rspec do |expectations|
7
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
8
+ end
9
+
10
+ config.mock_with :rspec do |mocks|
11
+ mocks.verify_partial_doubles = true
12
+ mocks.syntax = :expect
13
+ end
14
+
15
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: safe_timeout
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - David McCullars
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ description: A safer alternative to Ruby's Timeout that uses unix processes instead
56
+ of threads.
57
+ email:
58
+ - david.mccullars@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - circle.yml
70
+ - lib/safe_timeout.rb
71
+ - lib/safe_timeout/interrupting_child_process.rb
72
+ - lib/safe_timeout/version.rb
73
+ - safe_timeout.gemspec
74
+ - spec/lib/safe_timeout_spec.rb
75
+ - spec/spec_helper.rb
76
+ homepage: https://github.com/david-mccullars/safe_timeout
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: A safer alternative to Ruby's Timeout that uses unix processes instead of
100
+ threads.
101
+ test_files:
102
+ - spec/lib/safe_timeout_spec.rb
103
+ - spec/spec_helper.rb