From ef39866fa6e6ac91cc64f18573dea22c3f75c6e1 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 19 Jan 2015 09:17:51 +0000 Subject: consolidate spawn fix for Ruby [Bug #8770] Ensure we can apply the workaround to dtas-sourceedit and our test cases while also simplifying the existing call sites a little. This will also make for less code churn in 3-5 years down the line when we drop <= 2.1 support. --- bin/dtas-sourceedit | 3 ++- lib/dtas.rb | 1 + lib/dtas/partstats.rb | 8 ++------ lib/dtas/process.rb | 15 +++------------ lib/dtas/spawn_fix.rb | 8 ++++++++ test/test_player_integration.rb | 3 ++- test/test_splitfx.rb | 5 +++-- 7 files changed, 21 insertions(+), 22 deletions(-) create mode 100644 lib/dtas/spawn_fix.rb diff --git a/bin/dtas-sourceedit b/bin/dtas-sourceedit index 1980141..6ced538 100755 --- a/bin/dtas-sourceedit +++ b/bin/dtas-sourceedit @@ -54,6 +54,7 @@ if st_in.file? || st_in.pipe? buf = $stdin.read commit_update.call(buf) else + include DTAS::SpawnFix tmp = tmpyaml tmp_path = tmp.path do_update = lambda { commit_update.call(File.read(tmp_path)) } @@ -69,7 +70,7 @@ else end trap(:CHLD) { sev.signal } - pid = Process.spawn(cmd) + pid = spawn(cmd) begin r = IO.select(rset) or next r[0].each do |io| diff --git a/lib/dtas.rb b/lib/dtas.rb index a711150..e2318c6 100644 --- a/lib/dtas.rb +++ b/lib/dtas.rb @@ -4,3 +4,4 @@ module DTAS # :nodoc: end require_relative 'dtas/compat_onenine' +require_relative 'dtas/spawn_fix' diff --git a/lib/dtas/partstats.rb b/lib/dtas/partstats.rb index 6c714b3..546b88b 100644 --- a/lib/dtas/partstats.rb +++ b/lib/dtas/partstats.rb @@ -10,6 +10,7 @@ require_relative 'sigevent' class DTAS::PartStats CMD = 'sox "$INFILE" -n $TRIMFX $SOXFX stats $STATSOPTS' include DTAS::Process + include DTAS::SpawnFix attr_reader :key_idx attr_reader :key_width @@ -56,12 +57,7 @@ class DTAS::PartStats env["INFILE"] = @infile env["TRIMFX"] = "trim #{trim_part.tbeg}s #{trim_part.tlen}s" opts = { pgroup: true, close_others: true, err: wr } - pid = begin - Process.spawn(env, CMD, opts) - rescue Errno::EINTR - # workaround for older Rubies https://bugs.ruby-lang.org/issues/8770 - retry - end + pid = spawn(env, CMD, opts) wr.close [ pid, rd ] end diff --git a/lib/dtas/process.rb b/lib/dtas/process.rb index c0ce9a3..56e88e8 100644 --- a/lib/dtas/process.rb +++ b/lib/dtas/process.rb @@ -7,6 +7,7 @@ require_relative 'xs' module DTAS::Process # :nodoc: PIDS = {} include DTAS::XS + include DTAS::SpawnFix def self.reaper begin @@ -47,12 +48,7 @@ module DTAS::Process # :nodoc: opts = { close_others: true, pgroup: true }.merge!(opts) env = env_expand(env, opts) - pid = begin - Process.spawn(env, cmd, opts) - rescue Errno::EINTR - # workaround for older Rubies https://bugs.ruby-lang.org/issues/8770 - retry - end + pid = spawn(env, cmd, opts) warn [ :spawn, pid, cmd ].inspect if $DEBUG @spawn_at = Time.now.to_f PIDS[pid] = self @@ -76,12 +72,7 @@ module DTAS::Process # :nodoc: opts[:err] = we end env = env_expand(env, opts) - pid = begin - Process.spawn(env, *cmd, opts) - rescue Errno::EINTR - # workaround for older Rubies https://bugs.ruby-lang.org/issues/8770 - retry - end + pid = spawn(env, *cmd, opts) w.close if err_str we.close diff --git a/lib/dtas/spawn_fix.rb b/lib/dtas/spawn_fix.rb new file mode 100644 index 0000000..dfcc884 --- /dev/null +++ b/lib/dtas/spawn_fix.rb @@ -0,0 +1,8 @@ +module DTAS::SpawnFix # :nodoc: + # workaround for older Rubies: https://bugs.ruby-lang.org/issues/8770 + def spawn(*args) + super(*args) + rescue Errno::EINTR + retry + end if RUBY_VERSION.to_f <= 2.1 +end diff --git a/test/test_player_integration.rb b/test/test_player_integration.rb index 2525ac5..033e313 100644 --- a/test/test_player_integration.rb +++ b/test/test_player_integration.rb @@ -3,11 +3,12 @@ require './test/player_integration' class TestPlayerIntegration < Testcase include PlayerIntegration + include DTAS::SpawnFix def test_cmd_rate env = ENV.to_hash.merge(@fmt.to_env) cmd = "sox -n $SOXFMT - synth 3 pinknoise | #@cmd" - pid = Process.spawn(env, cmd) + pid = spawn(env, cmd) t = Time.now _, _ = Process.waitpid2(pid) elapsed = Time.now - t diff --git a/test/test_splitfx.rb b/test/test_splitfx.rb index ea170ce..5fbc0ac 100644 --- a/test/test_splitfx.rb +++ b/test/test_splitfx.rb @@ -6,6 +6,7 @@ require 'thread' require_relative 'helper' class TestSplitfx < Testcase + include DTAS::SpawnFix def tmp_err(path) err = $stderr.dup @@ -56,9 +57,9 @@ class TestSplitfx < Testcase # compare results with expected output res_cmd = "sox 1.flac 2.flac -ts32 -c2 -r44100 result.s32 stats" - res_pid = Process.spawn(res_cmd, err: 'b.txt') + res_pid = spawn(res_cmd, err: 'b.txt') exp_cmd = "sox foo.flac -ts32 -c2 -r44100 expect.s32 trim 4 stats" - exp_pid = Process.spawn(exp_cmd, err: 'a.txt') + exp_pid = spawn(exp_cmd, err: 'a.txt') _, s = Process.waitpid2(res_pid) assert s.success?, "#{res_cmd}: #{s.inspect}" _, s = Process.waitpid2(exp_pid) -- cgit v1.2.3-24-ge0c7